Skip to content

Commit 2214fba

Browse files
author
Jevgeni Koltšin
committed
s3 integration initial commit
-example how to use it. - api switched production -> staging
1 parent 179845c commit 2214fba

11 files changed

Lines changed: 172 additions & 6 deletions

File tree

src/main/java/com/creatubbles/api/CreatubblesAPI.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
11
package com.creatubbles.api;
22

3+
import com.creatubbles.api.core.Creation;
34
import com.creatubbles.api.core.Gallery;
5+
import com.creatubbles.api.core.Image;
6+
import com.creatubbles.api.request.amazon.UploadS3ImageRequest;
7+
import com.creatubbles.api.request.auth.SignInRequest;
8+
import com.creatubbles.api.request.creation.UpdateCreationRequest;
9+
import com.creatubbles.api.request.creation.UploadCreationRequest;
10+
import com.creatubbles.api.response.auth.SignInResponse;
411
import com.creatubbles.api.response.auth.SignUpResponse;
12+
import com.creatubbles.api.response.creation.UpdateCreationResponse;
13+
import com.creatubbles.api.response.creation.UploadCreationResponse;
514
import com.creatubbles.api.response.creator.CreateCreatorResponse;
615
import com.creatubbles.api.response.gallery.CreateUserGalleryResponse;
716
import com.creatubbles.api.response.user.UserProfileResponse;
@@ -12,12 +21,17 @@
1221
import org.glassfish.jersey.client.JerseyClient;
1322
import org.glassfish.jersey.client.JerseyClientBuilder;
1423

24+
import java.io.File;
25+
import java.io.IOException;
26+
import java.nio.file.Files;
27+
1528
public class CreatubblesAPI {
1629
public final static Gson GSON = new GsonBuilder()
1730
.registerTypeAdapter(SignUpResponse.class, new SignUpResponse())
1831
.registerTypeAdapter(UserProfileResponse.class, new UserProfileResponse())
1932
.registerTypeAdapter(CreateCreatorResponse.class, new CreateCreatorResponse())
2033
.registerTypeAdapter(CreateUserGalleryResponse.class, new CreateUserGalleryResponse())
34+
.registerTypeAdapter(CreateUserGalleryResponse.class, new CreateUserGalleryResponse())
2135
.registerTypeAdapter(Gallery.class, new Gallery())
2236
.create();
2337

@@ -29,4 +43,21 @@ public class CreatubblesAPI {
2943
public static String buildURL(final String endPoint) {
3044
return EndPoints.URL_BASE + endPoint;
3145
}
46+
47+
public static void main(String[] args) throws IOException {
48+
SignInResponse response = new SignInRequest("jevgeni.koltsin@gmail.com", "ccttbb").execute().getResponse();
49+
System.out.println(response.access_token);
50+
UploadCreationResponse uploadResponse = new UploadCreationRequest(response.access_token).execute().getResponse();
51+
//TODO: last screenshot
52+
File file = new File("C:/dev/1.png");
53+
byte[] data = Files.readAllBytes(file.toPath());
54+
String fileName = System.currentTimeMillis() + "creation.png";
55+
Creation creation = uploadResponse.creation;
56+
String relativePath = creation.store_dir + "/" + fileName;
57+
new UploadS3ImageRequest(data, relativePath).execute().getResponse();
58+
Image img = new Image();
59+
img.url = relativePath;
60+
UpdateCreationResponse updateResponse = new UpdateCreationRequest(response.access_token, creation).execute().getResponse();
61+
System.exit(0);
62+
}
3263
}

src/main/java/com/creatubbles/api/core/Creation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public class Creation {
77
public String last_bubbled_at, last_commented_at, last_submitted_at;
88
public Image image;
99
public Creator[] creators;
10+
public String store_dir;
1011
}

src/main/java/com/creatubbles/api/core/CreatubblesRequest.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.creatubbles.api.CreatubblesAPI;
44
import com.creatubbles.api.util.HttpMethod;
55
import org.glassfish.jersey.client.JerseyWebTarget;
6+
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
67

78
import javax.ws.rs.client.Entity;
89
import javax.ws.rs.client.Invocation;
@@ -19,6 +20,7 @@ public abstract class CreatubblesRequest<T extends CreatubblesResponse> {
1920
private Map<String, String> urlParameters;
2021
private Response response;
2122
private Future<Response> futureResponse;
23+
private static final String EMPTY_RESPONSE = "{}";
2224

2325
public CreatubblesRequest(String endPoint, HttpMethod httpMethod) {
2426
this(endPoint, httpMethod, null, null);
@@ -106,7 +108,7 @@ public CreatubblesRequest<T> setUrlParameter(String key, String value) {
106108

107109
public abstract Class<? extends T> getResponseClass();
108110

109-
private void resetResponse() {
111+
public void resetResponse() {
110112
if (response != null || futureResponse != null) {
111113
response = null;
112114
futureResponse = null;
@@ -119,11 +121,15 @@ public boolean isDone() {
119121

120122
public boolean wasSuccessful() {
121123
if (isDone()) {
122-
return getRawResponse().getStatus() == 200;
124+
return isSuccessStatus(getRawResponse());
123125
}
124126
return false;
125127
}
126128

129+
private boolean isSuccessStatus(Response response) {
130+
return response.getStatus() == 200 || response.getStatus() == 204;
131+
}
132+
127133
public void cancelRequest() {
128134
if (futureResponse != null & !futureResponse.isDone()) {
129135
futureResponse.cancel(true);
@@ -148,7 +154,11 @@ public T getResponse() {
148154
Response response = getRawResponse();
149155
Class<? extends T> responseClass = getResponseClass();
150156
if (response != null && responseClass != null) {
151-
T creatubblesResponse = CreatubblesAPI.GSON.fromJson(response.readEntity(String.class), responseClass);
157+
String json = response.readEntity(String.class);
158+
if (isSuccessStatus(response) && json.isEmpty()) {
159+
json = EMPTY_RESPONSE;
160+
}
161+
T creatubblesResponse = CreatubblesAPI.GSON.fromJson(json, responseClass);
152162
creatubblesResponse.setOriginatingRequest(this);
153163
return creatubblesResponse;
154164
}
@@ -167,6 +177,8 @@ public CreatubblesRequest<T> execute() {
167177
webTarget = webTarget.queryParam(paramKey, paramValue);
168178
}
169179
}
180+
HttpAuthenticationFeature basicAuth = HttpAuthenticationFeature.basic("c", "c");
181+
webTarget.register(basicAuth);
170182

171183
Invocation.Builder invocationBuilder = webTarget
172184
.request(MediaType.APPLICATION_JSON)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.creatubbles.api.request.amazon;
2+
3+
import com.amazonaws.services.s3.AmazonS3;
4+
import com.amazonaws.services.s3.model.ObjectMetadata;
5+
import com.amazonaws.services.s3.model.PutObjectRequest;
6+
import com.amazonaws.services.s3.model.PutObjectResult;
7+
import com.creatubbles.api.core.CreatubblesRequest;
8+
import com.creatubbles.api.response.amazon.UploadS3ImageResponse;
9+
import com.creatubbles.api.util.S3ClientUtil;
10+
11+
import java.io.ByteArrayInputStream;
12+
13+
/**
14+
* Created by Jevgeni on 28.10.2015.
15+
*/
16+
public class UploadS3ImageRequest extends CreatubblesRequest<UploadS3ImageResponse> {
17+
18+
private String filePath;
19+
private byte[] data;
20+
21+
public UploadS3ImageRequest(byte[] data, String filePath) {
22+
super(null, null);
23+
this.filePath = filePath;
24+
this.data = data;
25+
}
26+
27+
@Override
28+
public Class<? extends UploadS3ImageResponse> getResponseClass() {
29+
return UploadS3ImageResponse.class;
30+
}
31+
32+
@Override
33+
public CreatubblesRequest<UploadS3ImageResponse> execute() {
34+
resetResponse();
35+
AmazonS3 client = S3ClientUtil.getClient();
36+
ObjectMetadata metadata = new ObjectMetadata();
37+
metadata.setContentLength(data.length);
38+
PutObjectRequest putObjectRequest = new PutObjectRequest(S3ClientUtil.AWS_S3_BUCKET_NAME, filePath, new ByteArrayInputStream(data), metadata);
39+
client.putObject(putObjectRequest);
40+
return this;
41+
}
42+
43+
@Override
44+
public UploadS3ImageResponse getResponse() {
45+
return new UploadS3ImageResponse();
46+
}
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.creatubbles.api.request.creation;
2+
3+
import com.creatubbles.api.CreatubblesAPI;
4+
import com.creatubbles.api.core.Creation;
5+
import com.creatubbles.api.core.CreatubblesRequest;
6+
import com.creatubbles.api.core.Image;
7+
import com.creatubbles.api.response.creation.UpdateCreationResponse;
8+
import com.creatubbles.api.util.EndPoints;
9+
import com.creatubbles.api.util.HttpMethod;
10+
import com.creatubbles.api.util.S3ClientUtil;
11+
import com.google.gson.JsonObject;
12+
13+
/**
14+
* Created by Jevgeni on 28.10.2015.
15+
*/
16+
public class UpdateCreationRequest extends CreatubblesRequest<UpdateCreationResponse> {
17+
18+
public UpdateCreationRequest(String accessToken, Creation creation) {
19+
super(String.format(EndPoints.CREATIONS_BY_ID, creation.id), HttpMethod.PUT, accessToken);
20+
JsonObject jsonObject = new JsonObject();
21+
Image image = creation.image;
22+
if (image != null && image.url != null && !image.url.startsWith("http")) {
23+
image.url = S3ClientUtil.AWS_S3_BASE_URL + image.url;
24+
}
25+
jsonObject.add("creation", CreatubblesAPI.GSON.toJsonTree(creation));
26+
setData(jsonObject.toString());
27+
}
28+
29+
@Override
30+
public Class<? extends UpdateCreationResponse> getResponseClass() {
31+
return UpdateCreationResponse.class;
32+
}
33+
}

src/main/java/com/creatubbles/api/request/creation/UploadCreationRequest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// TODO Order of operations (and how the upload actually happens) is kind of vague in the API docs - figure out OoO
99
public class UploadCreationRequest extends CreatubblesRequest<UploadCreationResponse> {
1010

11-
public UploadCreationRequest() {
12-
super(EndPoints.CREATIONS, HttpMethod.POST);
11+
public UploadCreationRequest(String accessToken) {
12+
super(EndPoints.CREATIONS, HttpMethod.POST, accessToken);
1313
}
1414

1515
@Override
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.creatubbles.api.response.amazon;
2+
3+
import com.creatubbles.api.core.CreatubblesResponse;
4+
5+
/**
6+
* Created by Jevgeni on 28.10.2015.
7+
*/
8+
public class UploadS3ImageResponse extends CreatubblesResponse {
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.creatubbles.api.response.creation;
2+
3+
import com.creatubbles.api.core.CreatubblesResponse;
4+
5+
public class UpdateCreationResponse extends CreatubblesResponse {
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.creatubbles.api.response.creation;
22

3+
import com.creatubbles.api.core.Creation;
34
import com.creatubbles.api.core.CreatubblesResponse;
45

56
public class UploadCreationResponse extends CreatubblesResponse {
7+
8+
public Creation creation;
69
}

src/main/java/com/creatubbles/api/util/EndPoints.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public class EndPoints {
44

5-
public final static String URL_BASE = "https://www.creatubbles.com/api/v1/";
5+
public final static String URL_BASE = "https://staging.creatubbles.com/api/v1/";
66

77
public static final String SIGN_IN = "users/sign_in.json";
88

@@ -23,6 +23,8 @@ public class EndPoints {
2323

2424
public static final String USERS_GALLERIES = "users/%s/galleries.json";
2525

26+
public static final String CREATIONS_BY_ID = "creations/%s.json";
27+
2628
public static final String CREATIONS = "creations.json";
2729

2830
public static final String CREATORS_CREATIONS = "creators/%d/creations.json?page=%d";

0 commit comments

Comments
 (0)