Search in sources :

Example 1 with InstagramConfigurePhotoResult

use of org.brunocvcunha.instagram4j.requests.payload.InstagramConfigurePhotoResult in project instagram4j by brunocvcunha.

the class InstagramUploadPhotoRequest method execute.

@Override
public InstagramConfigurePhotoResult execute() throws ClientProtocolException, IOException {
    if (uploadId == null) {
        uploadId = String.valueOf(System.currentTimeMillis());
    }
    HttpPost post = createHttpRequest();
    post.setEntity(createMultipartEntity());
    try (CloseableHttpResponse response = api.getClient().execute(post)) {
        api.setLastResponse(response);
        int resultCode = response.getStatusLine().getStatusCode();
        String content = EntityUtils.toString(response.getEntity());
        log.info("Photo Upload result: " + resultCode + ", " + content);
        post.releaseConnection();
        StatusResult result = parseResult(resultCode, content);
        if (!result.getStatus().equalsIgnoreCase("ok")) {
            throw new RuntimeException("Error happened in photo upload: " + result.getMessage());
        }
        InstagramConfigurePhotoResult configurePhotoResult = api.sendRequest(new InstagramConfigurePhotoRequest(imageFile, uploadId, caption));
        log.info("Configure photo result: " + configurePhotoResult);
        if (!configurePhotoResult.getStatus().equalsIgnoreCase("ok")) {
            throw new IllegalArgumentException("Failed to configure image: " + configurePhotoResult.getMessage());
        }
        StatusResult exposeResult = api.sendRequest(new InstagramExposeRequest());
        log.info("Expose result: " + exposeResult);
        if (!exposeResult.getStatus().equalsIgnoreCase("ok")) {
            throw new IllegalArgumentException("Failed to expose image: " + exposeResult.getMessage());
        }
        return configurePhotoResult;
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StatusResult(org.brunocvcunha.instagram4j.requests.payload.StatusResult) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) InstagramExposeRequest(org.brunocvcunha.instagram4j.requests.internal.InstagramExposeRequest) InstagramConfigurePhotoRequest(org.brunocvcunha.instagram4j.requests.internal.InstagramConfigurePhotoRequest) InstagramConfigurePhotoResult(org.brunocvcunha.instagram4j.requests.payload.InstagramConfigurePhotoResult)

Aggregations

CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 InstagramConfigurePhotoRequest (org.brunocvcunha.instagram4j.requests.internal.InstagramConfigurePhotoRequest)1 InstagramExposeRequest (org.brunocvcunha.instagram4j.requests.internal.InstagramExposeRequest)1 InstagramConfigurePhotoResult (org.brunocvcunha.instagram4j.requests.payload.InstagramConfigurePhotoResult)1 StatusResult (org.brunocvcunha.instagram4j.requests.payload.StatusResult)1