-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Apache Http Client for http calls, as PATCH is supported. Add new instruments endpoints support.
- Loading branch information
1 parent
67a891a
commit deb16b6
Showing
16 changed files
with
325 additions
and
137 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
src/main/java/com/checkout/ApacheHttpClientTransport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
package com.checkout; | ||
|
||
import com.checkout.common.CheckoutUtils; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.http.Header; | ||
import org.apache.http.HttpEntityEnclosingRequest; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.client.methods.*; | ||
import org.apache.http.entity.ContentType; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.impl.client.HttpClients; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.MalformedURLException; | ||
import java.net.URI; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
@Slf4j | ||
public class ApacheHttpClientTransport implements Transport { | ||
|
||
private final URI baseUri; | ||
private final CloseableHttpClient httpClient; | ||
|
||
public ApacheHttpClientTransport(String baseUri, HttpClientBuilder httpClientBuilder) { | ||
this.baseUri = URI.create(baseUri); | ||
if (httpClientBuilder != null) { | ||
httpClient = httpClientBuilder.build(); | ||
} else { | ||
httpClient = HttpClients.createDefault(); | ||
} | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Response> invoke(String httpMethod, String path, ApiCredentials apiCredentials, String jsonRequest, String idempotencyKey) { | ||
return CompletableFuture.supplyAsync(() -> { | ||
CloseableHttpResponse response = null; | ||
HttpUriRequest request = null; | ||
switch (httpMethod) { | ||
case "GET": | ||
request = new HttpGet(getRequestUrl(path)); | ||
break; | ||
case "PUT": | ||
request = new HttpPut(getRequestUrl(path)); | ||
break; | ||
case "POST": | ||
request = new HttpPost(getRequestUrl(path)); | ||
break; | ||
case "DELETE": | ||
request = new HttpDelete(getRequestUrl(path)); | ||
break; | ||
case "PATCH": | ||
request = new HttpPatch(getRequestUrl(path)); | ||
break; | ||
default: | ||
throw new UnsupportedOperationException("Unsupported HTTP Method: " + httpMethod); | ||
} | ||
request.setHeader("user-agent", "checkout-sdk-java/" + CheckoutUtils.getVersionFromManifest()); | ||
request.setHeader("Accept", "application/json;charset=UTF-8"); | ||
request.setHeader("Content-Type", "application/json;charset=UTF-8"); | ||
request.setHeader("Authorization", apiCredentials.getAuthorizationHeader()); | ||
if (idempotencyKey != null) { | ||
request.setHeader("Cko-Idempotency-Key", idempotencyKey); | ||
} | ||
|
||
log.info("Request: " + Arrays.toString(request.getAllHeaders())); | ||
|
||
try { | ||
if (jsonRequest != null && request instanceof HttpEntityEnclosingRequest) { | ||
((HttpEntityEnclosingRequestBase) request).setEntity(new StringEntity(jsonRequest, ContentType.APPLICATION_JSON)); | ||
} | ||
|
||
log.info(httpMethod + " " + request.getURI()); | ||
|
||
response = httpClient.execute(request); | ||
|
||
log.info("Response: " + Arrays.toString(response.getAllHeaders())); | ||
|
||
final int statusCode = response.getStatusLine().getStatusCode(); | ||
final String requestId = Optional.ofNullable(response.getFirstHeader("Cko-Request-Id")) | ||
.map(Header::getValue).orElse("NO_REQUEST_ID_SUPPLIED"); | ||
|
||
if (statusCode != 404) { | ||
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()))) { | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = bufferedReader.readLine()) != null) { | ||
stringBuilder.append(line).append("\n"); | ||
} | ||
String jsonBody = stringBuilder.toString(); | ||
return new Response(statusCode, jsonBody, requestId); | ||
} | ||
} else { | ||
return new Response(statusCode, null, requestId); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} finally { | ||
if (response != null) { | ||
try { | ||
response.close(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private String getRequestUrl(String path) { | ||
try { | ||
return baseUri.resolve(path).toURL().toString(); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 0 additions & 115 deletions
115
src/main/java/com/checkout/HttpUrlConnectionTransport.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.checkout.instruments; | ||
|
||
import com.checkout.common.Address; | ||
import com.checkout.common.Phone; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class AccountHolder { | ||
private Address billingAddress; | ||
private Phone phone; | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/checkout/instruments/CustomerRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.checkout.instruments; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class CustomerRequest { | ||
private String id; | ||
@SerializedName("default") | ||
private boolean isDefault; | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/checkout/instruments/CustomerResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.checkout.instruments; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class CustomerResponse { | ||
private String id; | ||
private String email; | ||
private String name; | ||
@SerializedName("default") | ||
private boolean isDefault; | ||
} |
Oops, something went wrong.