com.webull.openapi.http.HttpResponse Maven / Gradle / Ivy
/*
* Copyright 2022 Webull
*
* 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
*
* https://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.
*/
package com.webull.openapi.http;
import com.google.gson.reflect.TypeToken;
import com.webull.openapi.common.Headers;
import com.webull.openapi.execption.ClientException;
import com.webull.openapi.execption.ErrorCode;
import com.webull.openapi.http.common.HttpStatus;
import com.webull.openapi.http.exception.HttpServerException;
import com.webull.openapi.logger.Logger;
import com.webull.openapi.logger.LoggerFactory;
import com.webull.openapi.serialize.JsonSerializer;
import okhttp3.Response;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HttpResponse implements Closeable {
private static final Logger logger = LoggerFactory.getLogger(HttpResponse.class);
private HttpServerException exception;
private final Response response;
private final int statusCode;
private final String statusMessage;
private final String requestId;
private final HashMap headers = new HashMap<>();
private final InputStream body;
public HttpResponse(Response response) {
this.response = response;
this.statusCode = response.code();
this.statusMessage = response.message();
this.body = response.body() != null ? response.body().byteStream() : null;
Map> resultHeaders = response.headers().toMultimap();
for (Map.Entry> entry : resultHeaders.entrySet()) {
this.headers.put(entry.getKey(), String.join(";", entry.getValue()));
}
this.requestId = this.headers.get(Headers.REQUEST_ID);
if (this.statusCode < HttpStatus.OK || this.statusCode >= HttpStatus.MULTIPLE_CHOICES) {
String responseBody = this.getResponseBody();
Map responseMap = new HashMap<>();
try {
responseMap = JsonSerializer.fromJson(responseBody, new TypeToken
© 2015 - 2025 Weber Informatics LLC | Privacy Policy