/* * Copyright 2024, The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import {assertDefined} from 'common/assert_utils'; export type HttpRequestHeaderType = Array<[string, string]>; export enum HttpRequestStatus { UNSENT, UNAUTH, SUCCESS, ERROR, } export interface HttpResponse { status: HttpRequestStatus; type: XMLHttpRequestResponseType; //eslint-disable-line no-undef text: string; body: any; getHeader: (name: string) => string | undefined; } export class HttpRequest { static async get( path: string, headers: HttpRequestHeaderType, type?: XMLHttpRequest['responseType'], ): Promise { return await HttpRequest.call('GET', path, headers, type); } static async post( path: string, headers: HttpRequestHeaderType, jsonRequest?: object, ): Promise { return await HttpRequest.call( 'POST', path, headers, undefined, jsonRequest, ); } private static async call( method: string, path: string, headers: HttpRequestHeaderType, type?: XMLHttpRequest['responseType'], jsonRequest?: object, ): Promise { const req = new XMLHttpRequest(); let status: HttpRequestStatus | undefined; await new Promise((resolve) => { req.onreadystatechange = async () => { if (req.readyState !== XMLHttpRequest.DONE) { return; } if (req.status === XMLHttpRequest.UNSENT) { status = HttpRequestStatus.UNSENT; } else if (req.status === 200) { status = HttpRequestStatus.SUCCESS; } else if (req.status === 403) { status = HttpRequestStatus.UNAUTH; } else { status = HttpRequestStatus.ERROR; } resolve(); }; req.responseType = type || ''; req.open(method, path); headers.forEach(([header, value]) => { req.setRequestHeader(header, value); }); if (jsonRequest) { const json = JSON.stringify(jsonRequest); req.setRequestHeader('Content-Type', 'application/json;charset=UTF-8'); req.send(json); } else { req.send(); } }); const hasResponseText = req.responseType === '' || req.responseType === 'text'; return { status: assertDefined(status), type: req.responseType, text: hasResponseText ? req.responseText : '', body: req.response, getHeader: (name: string) => req.getResponseHeader(name) ?? undefined, }; } }