Skip to content

Instantly share code, notes, and snippets.

@jrd281
Created September 28, 2018 20:49
Show Gist options
  • Save jrd281/de8fa530fb77ee1b8c17af96fd4a20b4 to your computer and use it in GitHub Desktop.
Save jrd281/de8fa530fb77ee1b8c17af96fd4a20b4 to your computer and use it in GitHub Desktop.
AlexaEmailClient
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.IOException;
public class AlexaEmailClient {
static final Logger logger = LogManager.getLogger(AlexaEmailClient.class);
private String _requestApiAccessToken;
private String _requestApiEndpoint;
private String _emailEndpoint = "/v2/accounts/~current/settings/Profile.email";
public AlexaEmailClient( String requestApiAccessToken, String requestApiEndpoint) {
this._requestApiEndpoint = requestApiEndpoint;
this._requestApiAccessToken = requestApiAccessToken;
}
public String getEmail() throws IOException {
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
String requestUrl = _requestApiEndpoint + _emailEndpoint;
logger.info("Request will be made to the following URL: {}", requestUrl);
HttpGet httpGet = new HttpGet(requestUrl);
httpGet.addHeader("Authorization", "Bearer " + _requestApiAccessToken);
httpGet.addHeader("Content-Type", "application/json");
logger.info("Request with token {}", _requestApiAccessToken);
logger.info("Sending request for User email");
String returnString = "";
try {
HttpResponse addressResponse = closeableHttpClient.execute(httpGet);
int statusCode = addressResponse.getStatusLine().getStatusCode();
logger.info("The Customer Profile API responded with a status code of {}", statusCode);
if (statusCode == HttpStatus.SC_OK) {
HttpEntity httpEntity = addressResponse.getEntity();
String responseBody = EntityUtils.toString(httpEntity);
returnString = responseBody;
logger.info("The responsebody was : {}", responseBody);
} else if (statusCode == HttpStatus.SC_FORBIDDEN) {
logger.info("Failed to authorize with a status code of {}", statusCode);
} else {
String errorMessage = "Email query failed with status code of " + statusCode;
logger.info(errorMessage);
}
} catch (IOException e) {
logger.info("IO Exception {}", e.getMessage());
}
return returnString;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment