Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpRequest.js « Network « src - git.mdns.eu/nextcloud/passwords-client.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ed5f88d785e5f11c5b0569df0cda231bc84a3b24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import ResponseDecodingError from '../Exception/ResponseDecodingError';
import NetworkError from '../Exception/NetworkError';
import HttpError from '../Exception/Http/HttpError';
import ResponseContentTypeError from '../Exception/ResponseContentTypeError';
import HttpResponse from './HttpResponse';

export default class HttpRequest {

    /**
     *
     * @param {String} [url=null]
     */
    constructor(url = null) {
        this._url = url;
        this._data = null;
        this._userAgent = null;
        this._responseType = 'application/json';
    }

    /**
     *
     * @returns {(String|null)}
     */
    getUrl() {
        return this._url;
    }

    /**
     *
     * @param {Session} value
     * @returns {HttpRequest}
     */
    setUrl(value) {
        this._url = value;

        return this;
    }

    /**
     *
     * @param {Object} value
     * @return {HttpRequest}
     */
    setData(value) {
        this._data = value;

        return this;
    }

    /**
     *
     * @param {String} value
     * @return {HttpRequest}
     */
    setUserAgent(value) {
        this._userAgent = value;

        return this;
    }

    /**
     *
     * @returns {Promise<HttpResponse>}
     */
    async send() {
        let options = this._getRequestOptions();
        let httpResponse = await this._executeRequest(this._url, options);
        let expectedContentType = options.headers.get('content-type');
        let contentType = httpResponse.headers.get('content-type');

        let response = new HttpResponse()
            .setContentType(contentType)
            .setHeaders(httpResponse.headers)
            .setHttpStatus(httpResponse.status)
            .setHttpResponse(httpResponse);

        if(expectedContentType !== null && contentType && contentType.indexOf(expectedContentType) === -1) {
            throw new ResponseContentTypeError(expectedContentType, contentType, httpResponse);
        } else if(contentType && contentType.indexOf('application/json') !== -1) {
            await this._processJsonResponse(httpResponse, response);
        } else {
            await this._processBinaryResponse(httpResponse, response);
        }

        return response;
    }

    /**
     *
     * @return {{redirect: string, headers: Headers, method: string, credentials: string}}
     * @private
     */
    _getRequestOptions() {
        let headers = this._getRequestHeaders();
        let method = 'GET';
        let options = {method, headers, credentials: 'omit', redirect: 'error'};
        if(this._data !== null) {
            options.body = JSON.stringify(this._data);
            method = 'POST';
        }
        options.method = method;

        return options;
    }

    /**
     *
     * @return {Headers}
     * @private
     */
    _getRequestHeaders() {
        let headers = new Headers();

        headers.append('accept', this._responseType);

        if(this._data !== null) {
            headers.append('content-type', 'application/json');
        }

        if(this._userAgent !== null) {
            headers.append('user-agent', this._userAgent);
        }

        return headers;
    }

    /**
     *
     * @param {String} url
     * @param {Object} options
     * @returns {Promise<Response>}
     * @private
     */
    async _executeRequest(url, options) {
        try {
            let request = new Request(url, options);

            return await fetch(request);
        } catch(e) {
            throw e;
        }
    }

    /**
     *
     * @param {Response} httpResponse
     * @param {HttpResponse} response
     * @private
     */
    async _processJsonResponse(httpResponse, response) {
        if(!httpResponse.ok) {
            throw this._getHttpError(httpResponse);
        }

        try {
            let json = await httpResponse.json();
            response.setData(json);
        } catch(e) {
            throw new ResponseDecodingError(response, e);
        }
    }

    /**
     *
     * @param {Response} httpResponse
     * @param {HttpResponse} response
     * @private
     */
    async _processBinaryResponse(httpResponse, response) {
        if(!httpResponse.ok) {
            throw this._getHttpError(httpResponse);
        }

        try {
            let blob = await httpResponse.blob();
            response.setData(blob);
        } catch(e) {
            throw new ResponseDecodingError(response, e);
        }
    }

    /**
     *
     * @param {Response} response
     * @private
     */
    _getHttpError(response) {
        if(response.status > 99) {
            return new HttpError(response);
        }

        return new NetworkError(response);
    }
}