Skip to content

Commit

Permalink
Add a sample project with an Spring Rest Template Transport implement…
Browse files Browse the repository at this point in the history
…ation.
  • Loading branch information
harry-peirse committed Aug 2, 2019
1 parent 79b895e commit 9e960ec
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 7 deletions.
14 changes: 8 additions & 6 deletions build.gradle
Expand Up @@ -4,17 +4,19 @@ plugins {
id 'de.marcphilipp.nexus-publish' version '0.2.0'
id 'io.codearte.nexus-staging' version '0.21.0'
id 'com.gradle.build-scan' version '2.3'
id "io.freefair.lombok" version "3.8.1"
id 'io.freefair.lombok' version '3.8.1'
}

allprojects {
repositories {
mavenLocal()
mavenCentral()
}
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenLocal()
mavenCentral()
}

dependencies {
implementation 'com.google.code.gson:gson:2.8.5'
implementation 'org.slf4j:slf4j-api:1.7.26'
Expand Down
24 changes: 24 additions & 0 deletions samples/spring/build.gradle
@@ -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"
}
@@ -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 {

}
@@ -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);
}
}
@@ -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);
}
}
4 changes: 3 additions & 1 deletion settings.gradle
@@ -1 +1,3 @@
rootProject.name = 'checkout-sdk-java'
rootProject.name = 'checkout-sdk-java'

include 'samples:spring'

0 comments on commit 9e960ec

Please sign in to comment.