Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Added support for linking accounts to sub-merchants#3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
thomshutt merged 4 commits intomasterfromlink-account
Nov 2, 2016
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,13 +4,17 @@
package com.brightcove.castlabs.client;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.brightcove.castlabs.client.request.LinkAccountToSubMerchantRequest;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand DownExpand Up@@ -162,7 +166,7 @@ protected String getUrlWithTicket(final String url) throws CastlabsException, IO
/**
* Ingest one or more keys into the Castlabs keystore.
*
* @param request Request parameters to pass to Castlabs
* @param requestRequest parameters to pass to Castlabs
* @param merchantId
* @return response from Castlabs
* @throws CastlabsException error reported by Castlabs
Expand All@@ -172,22 +176,10 @@ public IngestAssetsResponse ingestKeys(final IngestKeysRequest request, final St
throws CastlabsException, IOException {

final String uri = this.getUrlWithTicket(this.ingestionBaseUrl + "frontend/api/keys/v2/ingest/" + merchantId);
final HttpPost ingestRequest = new HttpPost(uri);
ingestRequest.addHeader("Content-Type", "application/json");
ingestRequest.setHeader("Accept", "application/json");

if (this.connectionTimeoutSeconds > 0) {
final int connectionTimeout = connectionTimeoutSeconds * 1000;
final RequestConfig requestConfig =
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(connectionTimeout).build();
ingestRequest.setConfig(requestConfig);
}
ingestRequest.setEntity(new StringEntity(objectMapper.writeValueAsString(request)));
final HttpPost httpRequest = createHttpPostRequest(uri, request);

final CloseableHttpClient httpclient = HttpClients.createDefault();
try (final CloseableHttpResponse ingestResponse = httpclient.execute(ingestRequest)) {
try (final CloseableHttpResponse ingestResponse = httpclient.execute(httpRequest)) {
if (ingestResponse != null) {
final int statusCode = ingestResponse.getStatusLine().getStatusCode();
if (200 != statusCode) {
Expand All@@ -212,7 +204,7 @@ public IngestAssetsResponse ingestKeys(final IngestKeysRequest request, final St
/**
* Add a sub merchant account to Castlabs.
*
* @param request Request parameters to pass to Castlabs
* @param requestRequest parameters to pass to Castlabs

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Is your IDE adding the whitespace here?

Copy link
ContributorAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yep, it lines it up with the@param on the line below

* @param merchantUuid UUID for the merchant that the sub-merchant is being created off
* @return response from Castlabs
* @throws CastlabsException error reported by Castlabs
Expand All@@ -222,38 +214,75 @@ public AddSubMerchantAccountResponse addSubMerchantAccount(final AddSubMerchantA
throws IOException, CastlabsException {

final String uri = this.getUrlWithTicket(this.ingestionBaseUrl + "frontend/rest/reselling/v1/reseller/" + merchantUuid + "/submerchant/add");
final HttpPost addResellerRequest = new HttpPost(uri);
addResellerRequest.addHeader("Content-Type", "application/json");
addResellerRequest.setHeader("Accept", "application/json");

if (this.connectionTimeoutSeconds > 0) {
final int connectionTimeout = connectionTimeoutSeconds * 1000;
final RequestConfig requestConfig =
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(connectionTimeout).build();
addResellerRequest.setConfig(requestConfig);
}
addResellerRequest.setEntity(new StringEntity(objectMapper.writeValueAsString(request)));
final HttpPost httpRequest = createHttpPostRequest(uri, request);

final CloseableHttpClient httpclient = HttpClients.createDefault();
try (final CloseableHttpResponse httpResponse = httpclient.execute(addResellerRequest)){
try (final CloseableHttpResponse httpResponse = httpclient.execute(httpRequest)){
final HttpEntity responseEntity = httpResponse.getEntity();
if (responseEntity == null) {
throw new CastlabsException("Empty response entity from Castlabs. HTTP Status: " + httpResponse.getStatusLine().getStatusCode());
}

final String responseBody = IOUtils.toString(responseEntity.getContent());
if(StringUtils.isBlank(responseBody)) {
if(StringUtils.isBlank(responseBody)) {
throw new CastlabsException("Empty response entity from Castlabs. HTTP Status: " + httpResponse.getStatusLine().getStatusCode());
}

final AddSubMerchantAccountResponse response = objectMapper.readValue(responseBody, AddSubMerchantAccountResponse.class);
if(response.getSubMerchantUuid() == null) {
if(response.getSubMerchantUuid() == null) {
throw new CastlabsException("Unexpected response from Castlabs: " + responseBody);
}
return response;
}
}

/**
* Link an existing user/API account to a sub-merchant account.
*
* @param request Request parameters to pass to Castlabs
* @param resellerUuid UUID for the merchant that the sub-merchant was created off
* @throws CastlabsException error reported by Castlabs
* @throws IOException network error while communicating with Castlabs REST API
*/
public void linkAccountToSubMerchant(final LinkAccountToSubMerchantRequest request, final String resellerUuid)
throws IOException, CastlabsException {

final String uri = this.getUrlWithTicket(this.ingestionBaseUrl + "frontend/rest/reselling/v1/reseller/" + resellerUuid + "/submerchant/linkAccount");
final HttpPost httpRequest = createHttpPostRequest(uri, request);

final CloseableHttpClient httpclient = HttpClients.createDefault();
try (final CloseableHttpResponse httpResponse = httpclient.execute(httpRequest)) {
final int statusCode = httpResponse.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_NO_CONTENT) {
final HttpEntity responseEntity = httpResponse.getEntity();

String responseBody = "";
if (responseEntity != null) {
responseBody = IOUtils.toString(responseEntity.getContent());
}

throw new CastlabsException("Unexpected status code from Castlabs: " + statusCode + ". Response body: " + responseBody);
}
}
}

private HttpPost createHttpPostRequest(final String uri, final Object body) throws JsonProcessingException, UnsupportedEncodingException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I approve of this refactoring 👍

final HttpPost request = new HttpPost(uri);
request.addHeader("Content-Type", "application/json");
request.setHeader("Accept", "application/json");
request.setEntity(new StringEntity(objectMapper.writeValueAsString(body)));

if (this.connectionTimeoutSeconds > 0) {
final int connectionTimeout = connectionTimeoutSeconds * 1000;
final RequestConfig requestConfig =
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
.setConnectTimeout(connectionTimeout)
.setSocketTimeout(connectionTimeout).build();
request.setConfig(requestConfig);
}

return request;
}

}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
package com.brightcove.castlabs.client.request;

import java.util.List;

public class LinkAccountToSubMerchantRequest {

private String subMerchantUuid;
private List<String> accountUuids;

public String getSubMerchantUuid() {
return subMerchantUuid;
}

public void setSubMerchantUuid(String subMerchantUuid) {
this.subMerchantUuid = subMerchantUuid;
}

public List<String> getAccountUuids() {
return accountUuids;
}

public void setAccountUuids(List<String> accountUuids) {
this.accountUuids = accountUuids;
}

@Override
public String toString() {
return "LinkAccountToSubMerchantRequest{" +
"subMerchantUuid='" + subMerchantUuid + '\'' +
", accountUuids=" + accountUuids +
'}';
}

}
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,6 +13,7 @@

import com.brightcove.castlabs.client.request.*;
import com.brightcove.castlabs.client.response.AddSubMerchantAccountResponse;
import com.google.common.collect.Lists;
import org.apache.commons.io.IOUtils;
import org.hamcrest.core.StringContains;
import org.junit.*;
Expand DownExpand Up@@ -332,6 +333,50 @@ public void testItCanHandleHttpErrorsWhenMakingASubMerchantCreationRequest() thr
}
}

@Test
public void testItCanLinkAnAccountToASubMerchant() throws Exception {
final String merchantId = "merchX";
final HttpRequest expectedRequest =
request().withMethod("POST").withPath("/frontend/rest/reselling/v1/reseller/" + merchantId + "/submerchant/linkAccount")
.withQueryStringParameter("ticket", exampleTicket)
.withHeader("accept", "application/json")
.withHeader("content-type", "application/json");
mockServerClient.when(expectedRequest).respond(response().withStatusCode(204));
LinkAccountToSubMerchantRequest request = new LinkAccountToSubMerchantRequest();
request.setSubMerchantUuid("0491caa4-8392-4b42-badc-f38a00134d77");
request.setAccountUuids(Lists.newArrayList("332cf57f-0941-482c-82cd-d1c8bc32cb77"));

// Castlabs doesn't return any response body, so we're effectively asserting "No Exception" here
castlabsClient.linkAccountToSubMerchant(request, "merchX");

mockServerClient.verify(expectedRequest, VerificationTimes.once());
}

@Test
public void testItCanHandleCastlabsErrorsWhileLinkingAnAccountToASubMerchant() throws Exception {
final String merchantId = "merchX";
final HttpRequest expectedRequest =
request().withMethod("POST").withPath("/frontend/rest/reselling/v1/reseller/" + merchantId + "/submerchant/linkAccount")
.withQueryStringParameter("ticket", exampleTicket)
.withHeader("accept", "application/json")
.withHeader("content-type", "application/json");
mockServerClient.when(expectedRequest).respond(response().withStatusCode(418).withBody("I'm a teapot"));
LinkAccountToSubMerchantRequest request = new LinkAccountToSubMerchantRequest();
request.setSubMerchantUuid("0491caa4-8392-4b42-badc-f38a00134d77");
request.setAccountUuids(Lists.newArrayList("332cf57f-0941-482c-82cd-d1c8bc32cb77"));

try {
castlabsClient.linkAccountToSubMerchant(request, "merchX");
fail("Expected a CastlabsException to be returned");
} catch(CastlabsException e) {
assertThat(e.getMessage(), StringContains.containsString("418"));
assertThat(e.getMessage(), StringContains.containsString("I'm a teapot"));
mockServerClient.verify(expectedRequest, VerificationTimes.once());
}

mockServerClient.verify(expectedRequest, VerificationTimes.once());
}

private String getTestResourceAsString(String filename) throws IOException {
final String path = this.getClass().getClassLoader().getResource(filename).getFile();
return IOUtils.toString(new FileInputStream(path));
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp