token_manager = $token_manager; $this->request_handler = $request_handler; $this->logger = new NullLogger(); } // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.Missing -- Token_Manager and Request_Handler throw a long list of typed exceptions that simply propagate out. /** * Dispatches the request, retrying once with fresh tokens if the first attempt is rejected as stale. * * @param Request $request The base request. * @param WP_User $user The WP user. * * @return Response The parsed response. */ public function send( Request $request, WP_User $user ): Response { try { return $this->do_send( $request, $user ); } catch ( Unauthorized_Exception $exception ) { $this->logger->debug( 'Token send: 401 received for user {user_id}; clearing stored JWTs and retrying once.', [ 'user_id' => $user->ID ] ); $this->token_manager->clear_tokens( (string) $user->ID ); return $this->do_send( $request, $user ); } } /** * Fetches the access token, attaches `Authorization: Bearer `, and dispatches via the AI Request_Handler. * * @param Request $request The base request. * @param WP_User $user The WP user. * * @return Response The parsed response. */ private function do_send( Request $request, WP_User $user ): Response { $token = $this->token_manager->get_or_request_access_token( $user ); $decorated = $request->with_added_headers( [ 'Authorization' => "Bearer $token" ] ); return $this->request_handler->handle( $decorated ); } // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.Missing }