Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a sample project with an Spring Rest Template Transport implement…
…ation.
- Loading branch information
1 parent
79b895e
commit 9e960ec
Showing
6 changed files
with
158 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
plugins { | ||
id 'java' | ||
id 'io.freefair.lombok' | ||
id 'org.springframework.boot' version '2.1.6.RELEASE' | ||
} | ||
|
||
apply plugin: 'io.spring.dependency-management' | ||
|
||
sourceCompatibility = 1.12 | ||
targetCompatibility = 1.12 | ||
|
||
dependencies { | ||
compile rootProject | ||
implementation 'org.springframework.boot:spring-boot-starter-web' | ||
|
||
testCompile 'junit:junit:4.12' | ||
testCompile 'org.slf4j:slf4j-simple:1.7.26' | ||
} | ||
|
||
test { | ||
environment "CHECKOUT_PUBLIC_KEY", System.getenv("CHECKOUT_PUBLIC_KEY") | ||
environment "CHECKOUT_SECRET_KEY", System.getenv("CHECKOUT_SECRET_KEY") | ||
systemProperty "org.slf4j.simpleLogger.defaultLogLevel", "DEBUG" | ||
} |
13 changes: 13 additions & 0 deletions
13
samples/spring/src/main/java/com/checkout/sample/spring/Application.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.sample.spring; | ||
|
||
import com.checkout.*; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
samples/spring/src/main/java/com/checkout/sample/spring/CheckoutConfig.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,38 @@ | ||
package com.checkout.sample.spring; | ||
|
||
import com.checkout.*; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
public class CheckoutConfig { | ||
@Value("checkout.secretKey") | ||
private String secretKey; | ||
@Value("checkout.publicKey") | ||
private String publicKey; | ||
|
||
@Bean | ||
public CheckoutConfiguration checkoutConfiguration() { | ||
return new CheckoutConfiguration(secretKey, true, publicKey); | ||
} | ||
|
||
@Bean | ||
public Serializer serializer() { | ||
return new GsonSerializer(); | ||
} | ||
|
||
@Bean | ||
public Transport transport(CheckoutConfiguration checkoutConfiguration, RestTemplate restTemplate) { | ||
return new RestTemplateTransport(checkoutConfiguration.getUri(), restTemplate); | ||
} | ||
|
||
@Bean | ||
public ApiClient apiClient(Serializer serializer, Transport transport) { | ||
return new ApiClientImpl(serializer, transport); | ||
} | ||
|
||
@Bean | ||
public CheckoutApi checkoutApi(ApiClient apiClient, CheckoutConfiguration checkoutConfiguration) { | ||
return new CheckoutApiImpl(apiClient, checkoutConfiguration); | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
samples/spring/src/main/java/com/checkout/sample/spring/RestTemplateTransport.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,72 @@ | ||
package com.checkout.sample.spring; | ||
|
||
import com.checkout.ApiCredentials; | ||
import com.checkout.Transport; | ||
import com.checkout.common.CheckoutUtils; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.StringHttpMessageConverter; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
public class RestTemplateTransport implements Transport { | ||
|
||
private static final RestTemplate DEFAULT_REST_TEMPLATE = new RestTemplate(); | ||
|
||
static { | ||
DEFAULT_REST_TEMPLATE.getMessageConverters().add(0, new StringHttpMessageConverter(StandardCharsets.UTF_8)); | ||
} | ||
|
||
private final URI baseUri; | ||
private final RestTemplate restTemplate; | ||
|
||
public RestTemplateTransport(String baseUri) { | ||
this(baseUri, DEFAULT_REST_TEMPLATE); | ||
} | ||
|
||
public RestTemplateTransport(String baseUri, RestTemplate restTemplate) { | ||
this.baseUri = URI.create(baseUri); | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
@Override | ||
public CompletableFuture<Response> invoke(String httpMethod, String path, ApiCredentials apiCredentials, String jsonRequest, String idempotencyKey) { | ||
RequestEntity.BodyBuilder requestBuilder = RequestEntity | ||
.method(HttpMethod.resolve(httpMethod), getRequestUri(path)) | ||
.contentType(MediaType.APPLICATION_JSON_UTF8) | ||
.accept(MediaType.APPLICATION_JSON_UTF8) | ||
.acceptCharset(StandardCharsets.UTF_8) | ||
.header("user-agent", "checkout-sdk-java/" + CheckoutUtils.getVersionFromManifest()) | ||
.header("Authorization", apiCredentials.getAuthorizationHeader()); | ||
if (idempotencyKey != null) { | ||
requestBuilder = requestBuilder.header("Ck-Idempotency-Key", idempotencyKey); | ||
} | ||
RequestEntity<?> request; | ||
if (jsonRequest != null) { | ||
request = requestBuilder.body(jsonRequest); | ||
} else { | ||
request = requestBuilder.build(); | ||
} | ||
|
||
return CompletableFuture.supplyAsync(() -> { | ||
ResponseEntity<String> response = restTemplate.exchange(request, String.class); | ||
return new Response(response.getStatusCodeValue(), response.getBody(), extractRequestId(response)); | ||
}); | ||
} | ||
|
||
private String extractRequestId(ResponseEntity<String> response) { | ||
return Optional.ofNullable(response.getHeaders().get("Ck-Request-Id")) | ||
.flatMap(list -> list.stream().findFirst()) | ||
.orElse("NO_REQUEST_ID_SUPPLIED"); | ||
} | ||
|
||
private URI getRequestUri(String path) { | ||
return baseUri.resolve(path); | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
rootProject.name = 'checkout-sdk-java' | ||
rootProject.name = 'checkout-sdk-java' | ||
|
||
include 'samples:spring' |