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

Commitd6f44f1

Browse files
thomshuttstuarthicks
authored andcommitted
Add the new Castlabs 'Download Keys' call (#7)
1 parent8e59929 commitd6f44f1

File tree

4 files changed

+205
-0
lines changed

4 files changed

+205
-0
lines changed

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
importjava.io.IOException;
77
importjava.io.UnsupportedEncodingException;
8+
importjava.util.ArrayList;
89
importjava.util.List;
910

1011
importcom.brightcove.castlabs.client.request.*;
12+
importcom.brightcove.castlabs.client.response.DownloadKeysResponse;
1113
importcom.brightcove.castlabs.client.response.ListAccountsResponse;
1214
importcom.fasterxml.jackson.core.JsonProcessingException;
1315
importcom.google.common.collect.Lists;
@@ -396,6 +398,48 @@ public ListAccountsResponse listAccounts(final String merchantUuid)
396398
}
397399
}
398400

401+
/**
402+
* Fetch all of a merchant's keys
403+
*
404+
* @param merchantUuid UUID for the merchant
405+
* @return list of responses from Castlabs
406+
* @throws CastlabsException error reported by Castlabs
407+
* @throws IOException network error while communicating with Castlabs REST API
408+
*/
409+
publicList<DownloadKeysResponse>downloadKeys(finalStringmerchantUuid)
410+
throwsIOException,CastlabsException {
411+
412+
finalStringuri =this.getUrlWithTicket(this.ingestionBaseUrl +"frontend/download-api/ingestion/query/v1/keys/" +merchantUuid +"/download");
413+
finalHttpGethttpRequest =newHttpGet(uri);
414+
415+
finalCloseableHttpClienthttpclient =HttpClients.createDefault();
416+
try (finalCloseableHttpResponsehttpResponse =httpclient.execute(httpRequest)) {
417+
finalintstatusCode =httpResponse.getStatusLine().getStatusCode();
418+
finalHttpEntityresponseEntity =httpResponse.getEntity();
419+
if (responseEntity ==null) {
420+
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
421+
}
422+
423+
finalStringresponseBody =IOUtils.toString(responseEntity.getContent());
424+
if (StringUtils.isBlank(responseBody)) {
425+
thrownewCastlabsException("Empty response entity from Castlabs. HTTP Status: " +httpResponse.getStatusLine().getStatusCode());
426+
}
427+
428+
if (statusCode !=HttpStatus.SC_OK) {
429+
thrownewCastlabsException("Unexpected status code from Castlabs: " +statusCode +". Response body: " +responseBody);
430+
}
431+
432+
// The response body is a newline-separated list of JSON objects, so we have to parse them individually
433+
finalList<DownloadKeysResponse>responses =newArrayList<>();
434+
finalString[]keys =responseBody.split("\n");
435+
for(Stringkey :keys) {
436+
responses.add(objectMapper.readValue(key,DownloadKeysResponse.class));
437+
}
438+
439+
returnresponses;
440+
}
441+
}
442+
399443
privateHttpPostcreateHttpPostRequest(finalStringuri,finalObjectbody)throwsJsonProcessingException,UnsupportedEncodingException {
400444
finalHttpPostrequest =newHttpPost(uri);
401445
request.addHeader("Content-Type","application/json");
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
packagecom.brightcove.castlabs.client.response;
2+
3+
publicclassDownloadKeysResponse {
4+
5+
publicStringuuid;
6+
publicStringtype;
7+
publicStringkeyId;
8+
publicStringstreamType;
9+
publicStringassetId;
10+
publicStringvariantId;
11+
publicStringkeyRotationId;
12+
publicStringingestChannel;
13+
publicStringingestRegion;
14+
publicStringauditChanged;
15+
publicStringauditChangedBy;
16+
17+
publicStringgetUuid() {
18+
returnuuid;
19+
}
20+
21+
publicvoidsetUuid(Stringuuid) {
22+
this.uuid =uuid;
23+
}
24+
25+
publicStringgetType() {
26+
returntype;
27+
}
28+
29+
publicvoidsetType(Stringtype) {
30+
this.type =type;
31+
}
32+
33+
publicStringgetKeyId() {
34+
returnkeyId;
35+
}
36+
37+
publicvoidsetKeyId(StringkeyId) {
38+
this.keyId =keyId;
39+
}
40+
41+
publicStringgetStreamType() {
42+
returnstreamType;
43+
}
44+
45+
publicvoidsetStreamType(StringstreamType) {
46+
this.streamType =streamType;
47+
}
48+
49+
publicStringgetAssetId() {
50+
returnassetId;
51+
}
52+
53+
publicvoidsetAssetId(StringassetId) {
54+
this.assetId =assetId;
55+
}
56+
57+
publicStringgetVariantId() {
58+
returnvariantId;
59+
}
60+
61+
publicvoidsetVariantId(StringvariantId) {
62+
this.variantId =variantId;
63+
}
64+
65+
publicStringgetKeyRotationId() {
66+
returnkeyRotationId;
67+
}
68+
69+
publicvoidsetKeyRotationId(StringkeyRotationId) {
70+
this.keyRotationId =keyRotationId;
71+
}
72+
73+
publicStringgetIngestChannel() {
74+
returningestChannel;
75+
}
76+
77+
publicvoidsetIngestChannel(StringingestChannel) {
78+
this.ingestChannel =ingestChannel;
79+
}
80+
81+
publicStringgetIngestRegion() {
82+
returningestRegion;
83+
}
84+
85+
publicvoidsetIngestRegion(StringingestRegion) {
86+
this.ingestRegion =ingestRegion;
87+
}
88+
89+
publicStringgetAuditChanged() {
90+
returnauditChanged;
91+
}
92+
93+
publicvoidsetAuditChanged(StringauditChanged) {
94+
this.auditChanged =auditChanged;
95+
}
96+
97+
publicStringgetAuditChangedBy() {
98+
returnauditChangedBy;
99+
}
100+
101+
publicvoidsetAuditChangedBy(StringauditChangedBy) {
102+
this.auditChangedBy =auditChangedBy;
103+
}
104+
105+
@Override
106+
publicStringtoString() {
107+
return"DownloadKeysResponse{" +
108+
"uuid='" +uuid +'\'' +
109+
", type='" +type +'\'' +
110+
", keyId='" +keyId +'\'' +
111+
", streamType='" +streamType +'\'' +
112+
", assetId='" +assetId +'\'' +
113+
", variantId='" +variantId +'\'' +
114+
", keyRotationId='" +keyRotationId +'\'' +
115+
", ingestChannel='" +ingestChannel +'\'' +
116+
", ingestRegion='" +ingestRegion +'\'' +
117+
", auditChanged='" +auditChanged +'\'' +
118+
", auditChangedBy='" +auditChangedBy +'\'' +
119+
'}';
120+
}
121+
122+
}

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
importjava.io.FileInputStream;
1111
importjava.io.IOException;
12+
importjava.util.List;
1213
importjava.util.concurrent.TimeUnit;
1314

1415
importcom.brightcove.castlabs.client.request.*;
1516
importcom.brightcove.castlabs.client.response.AddSubMerchantAccountResponse;
17+
importcom.brightcove.castlabs.client.response.DownloadKeysResponse;
1618
importcom.brightcove.castlabs.client.response.ListAccountsResponse;
1719
importcom.google.common.collect.Lists;
1820
importorg.apache.commons.io.IOUtils;
@@ -545,6 +547,41 @@ public void testItCanHandleCastlabsErrorsWhenMakingAListAccountsRequest() throws
545547
}
546548
}
547549

550+
@Test
551+
publicvoidtestItCanDownloadKeys()throwsException {
552+
finalStringmerchantId ="merchX";
553+
finalHttpRequestexpectedRequest =
554+
request().withMethod("GET").withPath("/frontend/download-api/ingestion/query/v1/keys/" +merchantId +"/download")
555+
.withQueryStringParameter("ticket",exampleTicket);
556+
finalStringmockResponse =getTestResourceAsString("sample_download_keys_response");
557+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(200).withBody(mockResponse));
558+
559+
finalList<DownloadKeysResponse>response =castlabsClient.downloadKeys(merchantId);
560+
561+
assertNotNull(response);
562+
assertEquals(2,response.size());
563+
assertEquals("10204c7b2c154b47a5af2ee41ecebf01",response.get(0).keyId);
564+
assertEquals("9fa85148ebc441c7a2f361c7927be915",response.get(1).keyId);
565+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
566+
}
567+
568+
@Test
569+
publicvoidtestItCanHandleCastlabsErrorsWhenDownloadingKeys()throwsException {
570+
finalStringmerchantId ="merchX";
571+
finalHttpRequestexpectedRequest =
572+
request().withMethod("GET").withPath("/frontend/download-api/ingestion/query/v1/keys/" +merchantId +"/download")
573+
.withQueryStringParameter("ticket",exampleTicket);
574+
mockServerClient.when(expectedRequest).respond(response().withStatusCode(403));
575+
576+
try {
577+
castlabsClient.downloadKeys(merchantId);
578+
fail("Expected a CastlabsException to be returned");
579+
}catch(CastlabsExceptione) {
580+
assertEquals(e.getMessage(),"Empty response entity from Castlabs. HTTP Status: 403");
581+
mockServerClient.verify(expectedRequest,VerificationTimes.once());
582+
}
583+
}
584+
548585
privateStringgetTestResourceAsString(Stringfilename)throwsIOException {
549586
finalStringpath =this.getClass().getClassLoader().getResource(filename).getFile();
550587
returnIOUtils.toString(newFileInputStream(path));
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
{"uuid":"0000f5ac-e520-3b7f-b9f1-2201210887e1","type":"CENC","keyId":"10204c7b2c154b47a5af2ee41ecebf01","streamType":"VIDEO_AUDIO","assetId":"10204c7b-2c15-4b47-a5af-2ee41ecebf0a","variantId":"playready","ingestChannel":"API","ingestRegion":"US_WEST_1","auditChanged":"2016-03-15T11:20:40.880Z","auditChangedBy":"332cf57f-0941-482c-82cd-d1c8bc32cb51"}
2+
{"uuid":"0001a754-09a7-3457-8608-3ddf7cfd1705","type":"CENC","keyId":"9fa85148ebc441c7a2f361c7927be915","streamType":"VIDEO_AUDIO","assetId":"13cb7cfd-4b42-4f9c-a67c-3577fb431c43","variantId":"widevine","ingestChannel":"API","ingestRegion":"US_WEST_1","auditChanged":"2016-02-12T15:02:58.964Z","auditChangedBy":"332cf57f-0941-482c-82cd-d1c8bc32cb51"}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp