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

Commitd88c039

Browse files
author
Jesse Michael
committed
listAccounts functionality
1 parent114f9d9 commitd88c039

File tree

7 files changed

+199
-2
lines changed

7 files changed

+199
-2
lines changed

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
importjava.util.List;
99

1010
importcom.brightcove.castlabs.client.request.*;
11+
importcom.brightcove.castlabs.client.response.ListAccountsResponse;
1112
importcom.fasterxml.jackson.core.JsonProcessingException;
1213
importcom.google.common.collect.Lists;
1314
importorg.apache.commons.io.IOUtils;
@@ -19,6 +20,7 @@
1920
importorg.apache.http.client.config.RequestConfig;
2021
importorg.apache.http.client.entity.UrlEncodedFormEntity;
2122
importorg.apache.http.client.methods.CloseableHttpResponse;
23+
importorg.apache.http.client.methods.HttpGet;
2224
importorg.apache.http.client.methods.HttpPost;
2325
importorg.apache.http.entity.StringEntity;
2426
importorg.apache.http.impl.client.CloseableHttpClient;
@@ -358,6 +360,42 @@ public void setFairplayConfiguration(final FairplayRequest request, final String
358360
}
359361
}
360362

363+
/**
364+
* List accounts available to a merchant
365+
*
366+
* @param merchantUuid UUID for the merchant
367+
* @return response from Castlabs
368+
* @throws CastlabsException error reported by Castlabs
369+
* @throws IOException network error while communicating with Castlabs REST API
370+
*/
371+
publicListAccountsResponselistAccounts(finalStringmerchantUuid)
372+
throwsIOException,CastlabsException {
373+
374+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/rest/config/v1/" +merchantUuid +"/account/list");
375+
finalHttpGethttpRequest =newHttpGet(uri);
376+
377+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
378+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)) {
379+
finalintstatusCode =httpResponse.getStatusLine().getStatusCode();
380+
finalHttpEntityresponseEntity =httpResponse.getEntity();
381+
if (responseEntity ==null) {
382+
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
383+
}
384+
385+
finalStringresponseBody =IOUtils.toString(responseEntity.getContent());
386+
if (StringUtils.isBlank(responseBody)) {
387+
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
388+
}
389+
390+
if (statusCode !=HttpStatus.SC_OK) {
391+
thrownewCastlabsException("Unexpected status code from Castlabs: " +statusCode +". Response body: " +responseBody);
392+
}
393+
394+
finalListAccountsResponseresponse =objectMapper.readValue(responseBody,ListAccountsResponse.class);
395+
returnresponse;
396+
}
397+
}
398+
361399
privateHttpPostcreateHttpPostRequest(finalStringuri,finalObjectbody)throwsJsonProcessingException,UnsupportedEncodingException {
362400
finalHttpPostrequest =newHttpPost(uri);
363401
request.addHeader("Content-Type","application/json");

‎src/main/java/com/brightcove/castlabs/client/request/FairplayRequest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void setProviderPrivateKey(String providerPrivateKey) {
2626

2727
@Override
2828
publicStringtoString() {
29-
return"UpdateAuthorizationSettingsRequest{" +
29+
return"FairplayRequest{" +
3030
"providerCertificate='" +providerCertificate +'\'' +
3131
", providerPrivateKey='" +providerPrivateKey +'\'' +
3232
", applicationSecretKey='" +applicationSecretKey +'\'' +

‎src/main/java/com/brightcove/castlabs/client/request/SharedSecretRequest.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void setSecret(String secret) {
3030

3131
@Override
3232
publicStringtoString() {
33-
return"UpdateAuthorizationSettingsRequest{" +
33+
return"SharedSecretRequest{" +
3434
"enabled='" +enabled +'\'' +
3535
", description='" +description +'\'' +
3636
", secret='" +secret +'\'' +
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
packagecom.brightcove.castlabs.client.response;
2+
3+
importjava.util.List;
4+
5+
publicclassAccount {
6+
7+
privateStringid;
8+
privateBooleanenabled;
9+
privateBooleanapi;
10+
privateStringlogin;
11+
privateStringfirstName;
12+
privateStringlastName;
13+
privateList<String>permissions;
14+
15+
publicStringgetID() {
16+
returnid;
17+
}
18+
19+
publicvoidsetID(Stringid) {
20+
this.id =id;
21+
}
22+
23+
publicBooleangetEnabled() {
24+
returnenabled;
25+
}
26+
27+
publicvoidsetEnabled(Booleanenabled) {
28+
this.enabled =enabled;
29+
}
30+
31+
publicBooleangetAPI() {
32+
returnapi;
33+
}
34+
35+
publicvoidsetAPI(Booleanapi) {
36+
this.api =api;
37+
}
38+
39+
publicStringgetLogin() {
40+
returnlogin;
41+
}
42+
43+
publicvoidsetLogin(Stringlogin) {
44+
this.login =login;
45+
}
46+
47+
publicStringgetFirstName() {returnfirstName; }
48+
49+
publicvoidsetFirstName(StringfirstName) {
50+
this.firstName =firstName;
51+
}
52+
53+
publicStringgetLastName() {
54+
returnlastName;
55+
}
56+
57+
publicvoidsetLastName(StringlastName) {
58+
this.lastName =lastName;
59+
}
60+
61+
publicList<String>getPermissions() {
62+
returnpermissions;
63+
}
64+
65+
publicvoidsetPermissions(List<String>permissions) {
66+
this.permissions =permissions;
67+
}
68+
69+
@Override
70+
publicStringtoString() {
71+
return"Account{" +
72+
"id='" +id +'\'' +
73+
", enabled='" +enabled +'\'' +
74+
", api='" +api +'\'' +
75+
", login='" +login +'\'' +
76+
", firstName='" +firstName +'\'' +
77+
", lastName='" +lastName +'\'' +
78+
", permissions='" +permissions +'\'' +
79+
'}';
80+
}
81+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecom.brightcove.castlabs.client.response;
2+
3+
importjava.util.List;
4+
5+
publicclassListAccountsResponse {
6+
7+
privateList<Account>accounts;
8+
9+
publicList<Account>getAccounts() {
10+
returnaccounts;
11+
}
12+
13+
publicvoidsetAccounts(List<Account>accounts) {
14+
this.accounts =accounts;
15+
}
16+
17+
@Override
18+
publicStringtoString() {
19+
return"ListAccountsResponse{" +
20+
"accounts='" +accounts +'\'' +
21+
'}';
22+
}
23+
24+
}

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

Lines changed: 34 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.brightcove.castlabs.client.response.ListAccountsResponse;
1617
importcom.google.common.collect.Lists;
1718
importorg.apache.commons.io.IOUtils;
1819
importorg.hamcrest.core.StringContains;
@@ -511,6 +512,39 @@ public void testItCanHandleCastlabsErrorsWhileSettingFairplayConfiguration() thr
511512
mockServerClient.verify(expectedRequest,VerificationTimes.once());
512513
}
513514

515+
@Test
516+
publicvoidtestItCanMakeAListAccounts()throwsException {
517+
finalStringmerchantId ="merchX";
518+
finalHttpRequestexpectedRequest =
519+
request().withMethod("GET").withPath("frontend/rest/config/v1/" +merchantId +"/account/list")
520+
.withQueryStringParameter("ticket",exampleTicket);
521+
finalStringmockResponse =getTestResourceAsString("sample_list_account_response.json");
522+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(200).withBody(mockResponse));
523+
524+
finalListAccountsResponseresponse =castlabsClient.listAccounts(merchantId);
525+
526+
assertNotNull(response);
527+
assertEquals(response.getAccounts().size(),2);
528+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
529+
}
530+
531+
@Test
532+
publicvoidtestItCanHandleCastlabsErrorsWhenMakingAListAccountsRequest()throwsException {
533+
finalStringmerchantId ="merchX";
534+
finalHttpRequestexpectedRequest =
535+
request().withMethod("GET").withPath("frontend/rest/config/v1/" +merchantId +"/account/list")
536+
.withQueryStringParameter("ticket",exampleTicket);
537+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(403));
538+
539+
try {
540+
castlabsClient.listAccounts(merchantId);
541+
fail("Expected a CastlabsException to be returned");
542+
}catch(CastlabsExceptione) {
543+
assertThat(e.getMessage(),StringContains.containsString("Unexpected status code from Castlabs: 403"));
544+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
545+
}
546+
}
547+
514548
privateStringgetTestResourceAsString(Stringfilename)throwsIOException {
515549
finalStringpath =this.getClass().getClassLoader().getResource(filename).getFile();
516550
returnIOUtils.toString(newFileInputStream(path));
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"accounts": [
3+
{
4+
"id":"[uuid]",
5+
"enabled":true,
6+
"api":false,
7+
"login":"[login]",
8+
"firstName":"[firstName]",
9+
"lastName":"[lastName]",
10+
"permissions": ["[permission1]","[permission2]"]
11+
},
12+
{
13+
"id":"[uuid]",
14+
"enabled":false,
15+
"api":true,
16+
"login":"[merchant::login]",
17+
"permissions": ["[permission]"]
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp