- Notifications
You must be signed in to change notification settings - Fork1
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| @@ -162,7 +166,7 @@ protected String getUrlWithTicket(final String url) throws CastlabsException, IO | ||
| /** | ||
| * Ingest one or more keys into the Castlabs keystore. | ||
| * | ||
| * @param requestRequest parameters to pass to Castlabs | ||
| * @param merchantId | ||
| * @return response from Castlabs | ||
| * @throws CastlabsException error reported by Castlabs | ||
| @@ -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 httpRequest = createHttpPostRequest(uri, request); | ||
| final CloseableHttpClient httpclient = HttpClients.createDefault(); | ||
| try (final CloseableHttpResponse ingestResponse = httpclient.execute(httpRequest)) { | ||
| if (ingestResponse != null) { | ||
| final int statusCode = ingestResponse.getStatusLine().getStatusCode(); | ||
| if (200 != statusCode) { | ||
| @@ -212,7 +204,7 @@ public IngestAssetsResponse ingestKeys(final IngestKeysRequest request, final St | ||
| /** | ||
| * Add a sub merchant account to Castlabs. | ||
| * | ||
| * @param requestRequest parameters to pass to Castlabs | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Is your IDE adding the whitespace here? ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| @@ -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 httpRequest = createHttpPostRequest(uri, request); | ||
| final CloseableHttpClient httpclient = HttpClients.createDefault(); | ||
| 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)) { | ||
| 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) { | ||
| 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff 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 + | ||
| '}'; | ||
| } | ||
| } |