'application/json', 'Accept' => 'application/json', ); if ( $authenticated ) { $api_key = self::get_api_key(); if ( $api_key ) { $headers['Authorization'] = 'Bearer ' . $api_key; } } return $headers; } /** * Parse an API response. * * @param array|WP_Error $response The raw response. * @return array|WP_Error Parsed response data or WP_Error. */ private static function parse_response( $response ) { if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); // Handle 401: delete stored API key to force re-login. if ( 401 === $code ) { delete_option( 'aiblogpress_api_key' ); delete_option( 'aiblogpress_user_id' ); delete_option( 'aiblogpress_scope' ); return new WP_Error( 'abpw_unauthorized', isset( $data['error'] ) ? $data['error'] : __( 'Authentication expired. Please log in again.', 'abp-writer-ai-blog-generator' ), array( 'status' => 401 ) ); } // Handle other error codes. if ( $code < 200 || $code >= 300 ) { $message = isset( $data['error'] ) ? $data['error'] : __( 'An unexpected error occurred.', 'abp-writer-ai-blog-generator' ); return new WP_Error( 'abpw_api_error', $message, array( 'status' => $code ) ); } return $data; } /** * Make an authenticated POST request. * * @param string $endpoint API endpoint path. * @param array $body Request body data. * @return array|WP_Error */ public static function post( $endpoint, $body = array() ) { $response = wp_remote_post( self::build_url( $endpoint ), array( 'headers' => self::build_headers( true ), 'body' => wp_json_encode( $body ), 'timeout' => self::get_timeout( $endpoint ), ) ); return self::parse_response( $response ); } /** * Make an authenticated GET request. * * @param string $endpoint API endpoint path. * @param array $query_params Query parameters. * @return array|WP_Error */ public static function get( $endpoint, $query_params = array() ) { $url = self::build_url( $endpoint ); if ( ! empty( $query_params ) ) { $url = add_query_arg( $query_params, $url ); } $response = wp_remote_get( $url, array( 'headers' => self::build_headers( true ), 'timeout' => self::get_timeout( $endpoint ), ) ); return self::parse_response( $response ); } /** * Make an authenticated PATCH request. * * @param string $endpoint API endpoint path. * @param array $body Request body data. * @return array|WP_Error */ public static function patch( $endpoint, $body = array() ) { $response = wp_remote_request( self::build_url( $endpoint ), array( 'method' => 'PATCH', 'headers' => self::build_headers( true ), 'body' => wp_json_encode( $body ), 'timeout' => self::get_timeout( $endpoint ), ) ); return self::parse_response( $response ); } /** * Make an authenticated DELETE request. * * @param string $endpoint API endpoint path. * @return array|WP_Error */ public static function delete( $endpoint ) { $response = wp_remote_request( self::build_url( $endpoint ), array( 'method' => 'DELETE', 'headers' => self::build_headers( true ), 'timeout' => self::DEFAULT_TIMEOUT, ) ); return self::parse_response( $response ); } /** * Make an unauthenticated POST request (for register/login). * * @param string $endpoint API endpoint path. * @param array $body Request body data. * @return array|WP_Error */ public static function post_unauthenticated( $endpoint, $body = array() ) { $response = wp_remote_post( self::build_url( $endpoint ), array( 'headers' => self::build_headers( false ), 'body' => wp_json_encode( $body ), 'timeout' => self::DEFAULT_TIMEOUT, ) ); return self::parse_response( $response ); } }