com.bitpay.sdk.client.HttpResponseProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bitpay_sdk Show documentation
Show all versions of bitpay_sdk Show documentation
Full implementation of the BitPay Payment Gateway. This library implements BitPay's Cryptographically
Secure RESTful API.
The newest version!
/*
* Copyright (c) 2019 BitPay.
* All rights reserved.
*/
package com.bitpay.sdk.client;
import com.bitpay.sdk.exceptions.BitPayApiException;
import com.bitpay.sdk.exceptions.BitPayExceptionProvider;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
public class HttpResponseProvider {
public static HttpResponse fromApacheHttpResponse(org.apache.http.HttpResponse apacheHttpResponse)
throws BitPayApiException {
try {
final HttpEntity entity = apacheHttpResponse.getEntity();
String body = EntityUtils.toString(entity, "UTF-8");
return new HttpResponse(
apacheHttpResponse.getStatusLine().getStatusCode(),
body,
Arrays.stream(apacheHttpResponse.getAllHeaders())
.collect(Collectors.toMap(Header::getName, Header::getValue)),
apacheHttpResponse.getLocale().toString(),
apacheHttpResponse.getStatusLine().getProtocolVersion().toString()
);
} catch (IOException e) {
BitPayExceptionProvider.throwApiExceptionWithMessage(e.getMessage());
throw new RuntimeException(e.getMessage());
}
}
}