public async sendRequest()

in src/plugins/kubectlGeneration/aksDocsRag/client.ts [60:135]


    public async sendRequest(config: RequestConfig): Promise<Errorable<{ response: CommandResponse }>> {
        await this.refreshToken();
        const { message } = config;

        let rawResponse = undefined;
        try {
            rawResponse = await fetch(aksDocsRAGEndpoint, {
                method: "POST",
                headers: {
                    "Access-Control-Allow-Origin": "*",
                    Authorization: `Bearer ${this.authToken}`,
                    "App-Tenant-Id": this.sessionProvider.selectedTenant.id,
                    "User-Data-Boundary": this.isEUBoundary(this.sessionProvider.selectedTenant.countryCode)
                        ? "EU"
                        : "Global",
                },
                body: JSON.stringify({
                    IsLocalEvaluation: false,
                    Question: message,
                    Scenario: aksDocsRAGScenario,
                    Intent: aksDocsRAGIntent,
                }),
            });
        } catch (err: unknown) {
            return {
                succeeded: false,
                error: `Unable to fetch data from AKS Docs RAG endpoint: ${getErrorMessage(err)}`,
            };
        }

        if (!rawResponse) {
            return { succeeded: false, error: `Unable to fetch data from AKS Docs RAG endpoint` };
        }

        const statusCode = rawResponse?.status ?? 0; // Use a default value for statusCode
        let data: DataResponse;
        try {
            data = (await rawResponse.json()) as DataResponse;
        } catch (error) {
            return {
                succeeded: false,
                error: `Failed to parse JSON response from AKS Docs RAG endpoint: ${getErrorMessage(error)}`,
            };
        }

        // Handle various error scenarios based on status code and response data
        switch (statusCode) {
            case 424:
                return { succeeded: false, error: "Error 424: Harmful content detected from RAGS endpoint" };
            case 0:
                return { succeeded: false, error: "Error: Invalid status code received from RAGS endpoint" };
            default:
                if (statusCode >= 400) {
                    return {
                        succeeded: false,
                        error: `Error ${statusCode}: ${rawResponse.statusText} from RAGS endpoint`,
                    };
                } else if (!data) {
                    return { succeeded: false, error: "Error: No response data received from RAGS endpoint" };
                }
                break;
        }

        // Process successful response
        const responseData = data?.Response;

        const parsedResponse = JSON.parse(responseData) as CommandResponse;
        if (parsedResponse.status.toLowerCase() === "error") {
            return { succeeded: false, error: parsedResponse.message };
        }

        return {
            succeeded: true,
            result: { response: parsedResponse },
        };
    }