"application/json", "Content-Type" => "application/json", ]; private string $version = 'v1'; private string $base_url = 'https://app.chatsale.io/api'; private string $register_url = 'https://chat.chatsale.io/'; public function __construct() { } public function register(array $user, string $prompt, array $appearance): ?array { $response = wp_remote_post( $this->makeRegisterUrl(self::REGISTER_ENDPOINT), [ 'headers' => $this->headers, 'body' => json_encode([ 'appearance' => $appearance, 'prompt' => $prompt, 'user' => $user, 'register_type' => "wordpress" ]) ] ); if (wp_remote_retrieve_response_code($response) != 201 && wp_remote_retrieve_response_code($response) != 200) { return ["error" => 'Email already exists. Please login or use another email address.']; } else { $body = wp_remote_retrieve_body($response); return json_decode($body, true); } } public function update_assistant($assistantId, $assistantSettings) :?array { $url = str_replace('assistant_id', $assistantId, self::UPDATE_ASSISTANT_ENDPOINT); $response = wp_remote_request( $this->makeRegisterUrl($url), [ 'method' => 'PUT', 'headers' => $this->headers, 'body' => json_encode([ 'appearance' => $assistantSettings['appearance'], 'prompt' => $assistantSettings['prompt'], 'closing_message' => $assistantSettings['base']['closing_message'], 'welcome_message' => $assistantSettings['base']['welcome_message'], 'role' => $assistantSettings['base']['role'], 'questions' => $assistantSettings['base']['questions'] ]) ] ); if (wp_remote_retrieve_response_code($response) != 201 && wp_remote_retrieve_response_code($response) != 200) { return ["error" => 'Email already exists. Please login or use another email address.']; } else { $body = wp_remote_retrieve_body($response); return json_decode($body, true); } } public function auth(string $email, string $password): array { $response = wp_remote_post( $this->makeUrl(self::AUTH_ENDPOINT), [ 'headers' => $this->headers, 'body' => json_encode([ 'email' => $email, 'password' => $password, ]) ] ); if (wp_remote_retrieve_response_code($response) != 201 && wp_remote_retrieve_response_code($response) != 200) { if (wp_remote_retrieve_response_code($response) == 403) { return ["error" => 'Incorrect password or email']; } $error = json_decode(wp_remote_retrieve_body($response)); return ["error" => $error['error']]; } else { $body = wp_remote_retrieve_body($response); return json_decode($body, true); } } private function makeRegisterUrl(string $endpoint): string { return implode('/',[$this->register_url , $endpoint]); } private function makeUrl(string $endpoint): string { return implode('/',[$this->base_url , $this->version , $endpoint]); } }