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

Commit333e5ae

Browse files
authored
Merge pull request#3 from zencoder/link-account
Added support for linking accounts to sub-merchants
2 parents8fca52c +849eb5b commit333e5ae

File tree

3 files changed

+140
-32
lines changed

3 files changed

+140
-32
lines changed

‎src/main/java/com/brightcove/castlabs/client/CastlabsClient.java‎

Lines changed: 61 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
packagecom.brightcove.castlabs.client;
55

66
importjava.io.IOException;
7+
importjava.io.UnsupportedEncodingException;
78
importjava.util.List;
89

10+
importcom.brightcove.castlabs.client.request.LinkAccountToSubMerchantRequest;
11+
importcom.fasterxml.jackson.core.JsonProcessingException;
912
importcom.google.common.collect.Lists;
1013
importorg.apache.commons.io.IOUtils;
1114
importorg.apache.commons.lang.StringUtils;
1215
importorg.apache.http.Header;
1316
importorg.apache.http.HttpEntity;
17+
importorg.apache.http.HttpStatus;
1418
importorg.apache.http.NameValuePair;
1519
importorg.apache.http.client.config.RequestConfig;
1620
importorg.apache.http.client.entity.UrlEncodedFormEntity;
@@ -162,7 +166,7 @@ protected String getUrlWithTicket(final String url) throws CastlabsException, IO
162166
/**
163167
* Ingest one or more keys into the Castlabs keystore.
164168
*
165-
* @param request Request parameters to pass to Castlabs
169+
* @param requestRequest parameters to pass to Castlabs
166170
* @param merchantId
167171
* @return response from Castlabs
168172
* @throws CastlabsException error reported by Castlabs
@@ -172,22 +176,10 @@ public IngestAssetsResponse ingestKeys(final IngestKeysRequest request, final St
172176
throwsCastlabsException,IOException {
173177

174178
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/api/keys/v2/ingest/" +merchantId);
175-
finalHttpPostingestRequest =newHttpPost(uri);
176-
ingestRequest.addHeader("Content-Type","application/json");
177-
ingestRequest.setHeader("Accept","application/json");
178-
179-
if (this.connectionTimeoutSeconds >0) {
180-
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
181-
finalRequestConfigrequestConfig =
182-
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
183-
.setConnectTimeout(connectionTimeout)
184-
.setSocketTimeout(connectionTimeout).build();
185-
ingestRequest.setConfig(requestConfig);
186-
}
187-
ingestRequest.setEntity(newStringEntity(objectMapper.writeValueAsString(request)));
179+
finalHttpPosthttpRequest =createHttpPostRequest(uri,request);
188180

189181
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
190-
try (finalCloseableHttpResponseingestResponse =httpclient.execute(ingestRequest)) {
182+
try (finalCloseableHttpResponseingestResponse =httpclient.execute(httpRequest)) {
191183
if (ingestResponse !=null) {
192184
finalintstatusCode =ingestResponse.getStatusLine().getStatusCode();
193185
if (200 !=statusCode) {
@@ -212,7 +204,7 @@ public IngestAssetsResponse ingestKeys(final IngestKeysRequest request, final St
212204
/**
213205
* Add a sub merchant account to Castlabs.
214206
*
215-
* @param request Request parameters to pass to Castlabs
207+
* @param requestRequest parameters to pass to Castlabs
216208
* @param merchantUuid UUID for the merchant that the sub-merchant is being created off
217209
* @return response from Castlabs
218210
* @throws CastlabsException error reported by Castlabs
@@ -222,38 +214,75 @@ public AddSubMerchantAccountResponse addSubMerchantAccount(final AddSubMerchantA
222214
throwsIOException,CastlabsException {
223215

224216
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/reselling/v1/reseller/" +merchantUuid +"/submerchant/add");
225-
finalHttpPostaddResellerRequest =newHttpPost(uri);
226-
addResellerRequest.addHeader("Content-Type","application/json");
227-
addResellerRequest.setHeader("Accept","application/json");
228-
229-
if (this.connectionTimeoutSeconds >0) {
230-
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
231-
finalRequestConfigrequestConfig =
232-
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
233-
.setConnectTimeout(connectionTimeout)
234-
.setSocketTimeout(connectionTimeout).build();
235-
addResellerRequest.setConfig(requestConfig);
236-
}
237-
addResellerRequest.setEntity(newStringEntity(objectMapper.writeValueAsString(request)));
217+
finalHttpPosthttpRequest =createHttpPostRequest(uri,request);
238218

239219
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
240-
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(addResellerRequest)){
220+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)){
241221
finalHttpEntityresponseEntity =httpResponse.getEntity();
242222
if (responseEntity ==null) {
243223
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
244224
}
245225

246226
finalStringresponseBody =IOUtils.toString(responseEntity.getContent());
247-
if(StringUtils.isBlank(responseBody)) {
227+
if(StringUtils.isBlank(responseBody)) {
248228
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
249229
}
250230

251231
finalAddSubMerchantAccountResponseresponse =objectMapper.readValue(responseBody,AddSubMerchantAccountResponse.class);
252-
if(response.getSubMerchantUuid() ==null) {
232+
if(response.getSubMerchantUuid() ==null) {
253233
thrownewCastlabsException("Unexpected response from Castlabs: " +responseBody);
254234
}
255235
returnresponse;
256236
}
257237
}
258238

239+
/**
240+
* Link an existing user/API account to a sub-merchant account.
241+
*
242+
* @param request Request parameters to pass to Castlabs
243+
* @param resellerUuid UUID for the merchant that the sub-merchant was created off
244+
* @throws CastlabsException error reported by Castlabs
245+
* @throws IOException network error while communicating with Castlabs REST API
246+
*/
247+
publicvoidlinkAccountToSubMerchant(finalLinkAccountToSubMerchantRequestrequest,finalStringresellerUuid)
248+
throwsIOException,CastlabsException {
249+
250+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/reselling/v1/reseller/" +resellerUuid +"/submerchant/linkAccount");
251+
finalHttpPosthttpRequest =createHttpPostRequest(uri,request);
252+
253+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
254+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)) {
255+
finalintstatusCode =httpResponse.getStatusLine().getStatusCode();
256+
257+
if (statusCode !=HttpStatus.SC_NO_CONTENT) {
258+
finalHttpEntityresponseEntity =httpResponse.getEntity();
259+
260+
StringresponseBody ="";
261+
if (responseEntity !=null) {
262+
responseBody =IOUtils.toString(responseEntity.getContent());
263+
}
264+
265+
thrownewCastlabsException("Unexpected status code from Castlabs: " +statusCode +". Response body: " +responseBody);
266+
}
267+
}
268+
}
269+
270+
privateHttpPostcreateHttpPostRequest(finalStringuri,finalObjectbody)throwsJsonProcessingException,UnsupportedEncodingException {
271+
finalHttpPostrequest =newHttpPost(uri);
272+
request.addHeader("Content-Type","application/json");
273+
request.setHeader("Accept","application/json");
274+
request.setEntity(newStringEntity(objectMapper.writeValueAsString(body)));
275+
276+
if (this.connectionTimeoutSeconds >0) {
277+
finalintconnectionTimeout =connectionTimeoutSeconds *1000;
278+
finalRequestConfigrequestConfig =
279+
RequestConfig.custom().setConnectionRequestTimeout(connectionTimeout)
280+
.setConnectTimeout(connectionTimeout)
281+
.setSocketTimeout(connectionTimeout).build();
282+
request.setConfig(requestConfig);
283+
}
284+
285+
returnrequest;
286+
}
287+
259288
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
packagecom.brightcove.castlabs.client.request;
2+
3+
importjava.util.List;
4+
5+
publicclassLinkAccountToSubMerchantRequest {
6+
7+
privateStringsubMerchantUuid;
8+
privateList<String>accountUuids;
9+
10+
publicStringgetSubMerchantUuid() {
11+
returnsubMerchantUuid;
12+
}
13+
14+
publicvoidsetSubMerchantUuid(StringsubMerchantUuid) {
15+
this.subMerchantUuid =subMerchantUuid;
16+
}
17+
18+
publicList<String>getAccountUuids() {
19+
returnaccountUuids;
20+
}
21+
22+
publicvoidsetAccountUuids(List<String>accountUuids) {
23+
this.accountUuids =accountUuids;
24+
}
25+
26+
@Override
27+
publicStringtoString() {
28+
return"LinkAccountToSubMerchantRequest{" +
29+
"subMerchantUuid='" +subMerchantUuid +'\'' +
30+
", accountUuids=" +accountUuids +
31+
'}';
32+
}
33+
34+
}

‎src/test/java/com/brightcove/castlabs/client/CastlabsClientTest.java‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
importcom.brightcove.castlabs.client.request.*;
1515
importcom.brightcove.castlabs.client.response.AddSubMerchantAccountResponse;
16+
importcom.google.common.collect.Lists;
1617
importorg.apache.commons.io.IOUtils;
1718
importorg.hamcrest.core.StringContains;
1819
importorg.junit.*;
@@ -332,6 +333,50 @@ public void testItCanHandleHttpErrorsWhenMakingASubMerchantCreationRequest() thr
332333
}
333334
}
334335

336+
@Test
337+
publicvoidtestItCanLinkAnAccountToASubMerchant()throwsException {
338+
finalStringmerchantId ="merchX";
339+
finalHttpRequestexpectedRequest =
340+
request().withMethod("POST").withPath("/frontend/rest/reselling/v1/reseller/" +merchantId +"/submerchant/linkAccount")
341+
.withQueryStringParameter("ticket",exampleTicket)
342+
.withHeader("accept","application/json")
343+
.withHeader("content-type","application/json");
344+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(204));
345+
LinkAccountToSubMerchantRequestrequest =newLinkAccountToSubMerchantRequest();
346+
request.setSubMerchantUuid("0491caa4-8392-4b42-badc-f38a00134d77");
347+
request.setAccountUuids(Lists.newArrayList("332cf57f-0941-482c-82cd-d1c8bc32cb77"));
348+
349+
// Castlabs doesn't return any response body, so we're effectively asserting "No Exception" here
350+
castlabsClient.linkAccountToSubMerchant(request,"merchX");
351+
352+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
353+
}
354+
355+
@Test
356+
publicvoidtestItCanHandleCastlabsErrorsWhileLinkingAnAccountToASubMerchant()throwsException {
357+
finalStringmerchantId ="merchX";
358+
finalHttpRequestexpectedRequest =
359+
request().withMethod("POST").withPath("/frontend/rest/reselling/v1/reseller/" +merchantId +"/submerchant/linkAccount")
360+
.withQueryStringParameter("ticket",exampleTicket)
361+
.withHeader("accept","application/json")
362+
.withHeader("content-type","application/json");
363+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(418).withBody("I'm a teapot"));
364+
LinkAccountToSubMerchantRequestrequest =newLinkAccountToSubMerchantRequest();
365+
request.setSubMerchantUuid("0491caa4-8392-4b42-badc-f38a00134d77");
366+
request.setAccountUuids(Lists.newArrayList("332cf57f-0941-482c-82cd-d1c8bc32cb77"));
367+
368+
try {
369+
castlabsClient.linkAccountToSubMerchant(request,"merchX");
370+
fail("Expected a CastlabsException to be returned");
371+
}catch(CastlabsExceptione) {
372+
assertThat(e.getMessage(),StringContains.containsString("418"));
373+
assertThat(e.getMessage(),StringContains.containsString("I'm a teapot"));
374+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
375+
}
376+
377+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
378+
}
379+
335380
privateStringgetTestResourceAsString(Stringfilename)throwsIOException {
336381
finalStringpath =this.getClass().getClassLoader().getResource(filename).getFile();
337382
returnIOUtils.toString(newFileInputStream(path));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp