Skip to content

Commit 84b5239

Browse files
author
Jevgeni Koltšin
committed
create-creation-upload, landing urls, ping url, new api base urls, example.
1 parent fb4805a commit 84b5239

13 files changed

+280
-35
lines changed

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

+40-16
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
package com.creatubbles.api;
22

3-
import java.io.File;
4-
import java.io.IOException;
5-
import java.lang.reflect.Type;
6-
import java.nio.file.Files;
7-
8-
import com.google.gson.*;
9-
import org.glassfish.jersey.client.ClientProperties;
10-
import org.glassfish.jersey.client.JerseyClient;
11-
import org.glassfish.jersey.client.JerseyClientBuilder;
12-
133
import com.creatubbles.api.core.Gallery;
14-
import com.creatubbles.api.request.amazon.UploadS3ImageRequest;
4+
import com.creatubbles.api.core.LandingUrl;
5+
import com.creatubbles.api.request.amazon.UploadS3FileRequest;
156
import com.creatubbles.api.request.creation.CreateCreationRequest;
167
import com.creatubbles.api.request.creation.CreationsUploadsRequest;
178
import com.creatubbles.api.request.creation.PingCreationsUploadsRequest;
9+
import com.creatubbles.api.request.landingurls.GetLandingUrlsRequest;
10+
import com.creatubbles.api.request.landingurls.GetSpecificLandingUrlRequest;
1811
import com.creatubbles.api.response.auth.SignUpResponse;
1912
import com.creatubbles.api.response.creation.CreateCreationResponse;
2013
import com.creatubbles.api.response.creation.CreationsUploadsResponse;
2114
import com.creatubbles.api.response.creation.GetCreationsResponse;
2215
import com.creatubbles.api.response.creator.CreateCreatorResponse;
2316
import com.creatubbles.api.response.creator.GetCreatorsResponse;
2417
import com.creatubbles.api.response.gallery.CreateUserGalleryResponse;
18+
import com.creatubbles.api.response.landingurls.GetLandingUrlsResponse;
19+
import com.creatubbles.api.response.landingurls.GetSpecificLandingUrlResponse;
2520
import com.creatubbles.api.response.user.UserProfileResponse;
2621
import com.creatubbles.api.util.EndPoints;
22+
import com.creatubbles.api.util.HttpUtil;
23+
import com.google.gson.*;
24+
import org.glassfish.jersey.client.ClientProperties;
25+
import org.glassfish.jersey.client.JerseyClient;
26+
import org.glassfish.jersey.client.JerseyClientBuilder;
27+
28+
import java.io.File;
29+
import java.io.IOException;
30+
import java.lang.reflect.Type;
31+
import java.nio.file.Files;
2732

2833

2934
@SuppressWarnings("deprecation")
@@ -39,6 +44,8 @@ public class CreatubblesAPI {
3944
.registerTypeAdapter(GetCreationsResponse.class, new GetCreationsResponse())
4045
.registerTypeAdapter(CreateCreationResponse.class, new CreateCreationResponse())
4146
.registerTypeAdapter(CreationsUploadsResponse.class, new CreationsUploadsResponse())
47+
.registerTypeAdapter(GetLandingUrlsResponse.class, new GetLandingUrlsResponse())
48+
.registerTypeAdapter(GetSpecificLandingUrlResponse.class, new GetSpecificLandingUrlResponse())
4249
.registerTypeAdapter(String.class, new StringAdapter())
4350
.create();
4451

@@ -49,6 +56,9 @@ public class CreatubblesAPI {
4956
.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, Boolean.TRUE);
5057

5158
public static String buildURL(final String endPoint) {
59+
if (endPoint.startsWith("https://")) {
60+
return endPoint;
61+
}
5262
String base = staging ? EndPoints.URL_BASE_STAGING : EndPoints.URL_BASE;
5363
return base.concat(endPoint);
5464
}
@@ -61,25 +71,39 @@ public static void setStagingMode(boolean staging) {
6171

6272
public static void main(String[] args) throws IOException {
6373
// Additional examples can be found in the JUnit test files
64-
74+
6575
CreatubblesAPI.setStagingMode(true);
6676
String accessToken = ""; // TODO commit tests AuthTests.getAuthToken();
6777

6878
CreateCreationRequest createCreation = new CreateCreationRequest(accessToken);
6979
CreateCreationResponse createCreationResponse = createCreation.execute().getResponse();
7080
System.out.println(createCreationResponse.creation.id);
7181

72-
CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest(createCreationResponse.creation.id, accessToken);
82+
File file = new File("C:/dev/1.png");
83+
String extension = HttpUtil.getExtension(file.getPath());
84+
85+
CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest(createCreationResponse.creation.id, extension, accessToken);
7386
CreationsUploadsResponse creationsUploadsResponse = creationsUploads.execute().getResponse();
7487
System.out.println(creationsUploadsResponse.url);
7588
System.out.println(creationsUploadsResponse.id);
7689

77-
File file = new File("C:/dev/1.png");
90+
GetLandingUrlsRequest getLandingUrls = new GetLandingUrlsRequest(accessToken);
91+
for (LandingUrl landingUrl : getLandingUrls.execute().getResponse().urls) {
92+
System.out.println(landingUrl.type + ":" + landingUrl.url);
93+
}
94+
95+
GetSpecificLandingUrlRequest getSpecificLandingUrl = new GetSpecificLandingUrlRequest(accessToken, LandingUrl.LandingUrlType.CTB_USER_PROFILE);
96+
GetSpecificLandingUrlResponse getSpecificLandingUrlResponse = getSpecificLandingUrl.execute().getResponse();
97+
LandingUrl url = getSpecificLandingUrlResponse.url;
98+
System.out.println("specific url - " + url.type + ":" + url.url);
99+
78100
byte[] data = Files.readAllBytes(file.toPath());
79-
UploadS3ImageRequest uploadS3Image = new UploadS3ImageRequest(data, creationsUploadsResponse.url);
101+
102+
UploadS3FileRequest uploadS3Image = new UploadS3FileRequest(data, creationsUploadsResponse.url, creationsUploadsResponse.content_type);
80103
uploadS3Image.execute().getResponse();
81104

82-
PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest(creationsUploadsResponse.id, accessToken);
105+
PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest(creationsUploadsResponse.ping_url, accessToken);
106+
pingCreationsUploads.setData("");
83107
pingCreationsUploads.execute().getResponse();
84108
System.out.println("-Finish-");
85109
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.creatubbles.api.core;
2+
3+
/**
4+
* Created by Jevgeni on 10.03.2016.
5+
*/
6+
public class LandingUrl {
7+
8+
public LandingUrlType type;
9+
public String url;
10+
11+
public enum LandingUrlType {
12+
13+
CTB_ABOUT_US("ctb-about_us"),
14+
CTB_TERMS_OF_USE("ctb-terms_of_use"),
15+
CTB_PRIVACY_POLICY("ctb-privacy_policy"),
16+
CTB_REGISTRATION("ctb-registration"),
17+
CTB_FORGOT_PASSWORD("ctb-forgot_password"),
18+
CTB_USER_PROFILE("ctb-user_profile"),
19+
CTB_EXPLORE("ctb-explore");
20+
21+
private String type;
22+
23+
public String getType() {
24+
return type;
25+
}
26+
27+
LandingUrlType(String type) {
28+
this.type = type;
29+
}
30+
31+
public static LandingUrlType from(String type) {
32+
for (LandingUrlType e : LandingUrlType.values()) {
33+
if (e.getType().equals(type)) {
34+
return e;
35+
}
36+
}
37+
throw new IllegalStateException();
38+
}
39+
40+
@Override
41+
public String toString() {
42+
return type;
43+
}
44+
}
45+
}

src/main/java/com/creatubbles/api/request/amazon/UploadS3ImageRequest.java renamed to src/main/java/com/creatubbles/api/request/amazon/UploadS3FileRequest.java

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import com.creatubbles.api.APIVersion;
44
import com.creatubbles.api.core.CreatubblesRequest;
5-
import com.creatubbles.api.response.amazon.UploadS3ImageResponse;
5+
import com.creatubbles.api.response.amazon.UploadS3FileResponse;
66
import com.creatubbles.api.util.HttpUtil;
77
import com.creatubbles.api.util.HttpUtil.Response;
88

@@ -12,28 +12,30 @@
1212
* Created by Jevgeni on 28.10.2015.
1313
*/
1414
@APIVersion(2)
15-
public class UploadS3ImageRequest extends CreatubblesRequest<UploadS3ImageResponse> {
15+
public class UploadS3FileRequest extends CreatubblesRequest<UploadS3FileResponse> {
1616

1717
private byte[] data;
1818
private String url;
19+
private String contentType;
1920

20-
public UploadS3ImageRequest(byte[] data, String url) {
21+
public UploadS3FileRequest(byte[] data, String url, String contentType) {
2122
super(null, null);
2223
this.data = data;
2324
this.url = url;
25+
this.contentType = contentType;
2426
}
2527

2628
@Override
27-
public Class<? extends UploadS3ImageResponse> getResponseClass() {
28-
return UploadS3ImageResponse.class;
29+
public Class<? extends UploadS3FileResponse> getResponseClass() {
30+
return UploadS3FileResponse.class;
2931
}
3032

3133
@Override
32-
public CreatubblesRequest<UploadS3ImageResponse> execute() {
34+
public CreatubblesRequest<UploadS3FileResponse> execute() {
3335
resetResponse();
34-
UploadS3ImageResponse response = new UploadS3ImageResponse();
36+
UploadS3FileResponse response = new UploadS3FileResponse();
3537
try {
36-
Response resp = HttpUtil.uploadObject(data, url, HttpUtil.IMAGE_JPEG_CONTENT_TYPE);
38+
Response resp = HttpUtil.uploadObject(data, url, contentType);
3739
response.success = isSuccessStatusCode(resp.code);
3840
response.message = resp.message;
3941
setResponseCache(response);

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,33 @@
55
import com.creatubbles.api.response.creation.CreationsUploadsResponse;
66
import com.creatubbles.api.util.EndPoints;
77
import com.creatubbles.api.util.HttpMethod;
8+
import com.creatubbles.api.util.HttpUtil;
89

910
@APIVersion(2)
1011
public class CreationsUploadsRequest extends CreatubblesRequest<CreationsUploadsResponse> {
1112

12-
public CreationsUploadsRequest(String creationId, String accessToken) {
13+
private String extension;
14+
15+
public CreationsUploadsRequest(String creationId, String extension, String accessToken) {
1316
super(String.format(EndPoints.CREATIONS_UPLOADS, creationId), HttpMethod.POST, accessToken);
17+
this.extension = extension;
18+
setUrlParameter("extension", extension);
1419
}
1520

1621
@Override
1722
public Class<? extends CreationsUploadsResponse> getResponseClass() {
1823
return CreationsUploadsResponse.class;
1924
}
25+
26+
@Override
27+
public CreatubblesRequest<CreationsUploadsResponse> execute() {
28+
if (extension == null || !HttpUtil.allowedFileTypes.contains(extension)) {
29+
resetResponse();
30+
CreationsUploadsResponse response = new CreationsUploadsResponse();
31+
response.message = String.format("Invalid file with type %s", extension);
32+
setResponseCache(response);
33+
return this;
34+
}
35+
return super.execute();
36+
}
2037
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
@APIVersion(2)
1313
public class PingCreationsUploadsRequest extends CreatubblesRequest<PingCreationsUploadsResponse> {
1414

15-
public PingCreationsUploadsRequest(int uploadId, String accessToken) {
16-
super(String.format(EndPoints.PING_CREATIONS_UPLOADS, uploadId), HttpMethod.PUT, accessToken);
15+
public PingCreationsUploadsRequest(String url, String accessToken) {
16+
super(url, HttpMethod.PUT, accessToken);
1717
}
1818

1919
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.creatubbles.api.request.landingurls;
2+
3+
import com.creatubbles.api.APIVersion;
4+
import com.creatubbles.api.core.CreatubblesRequest;
5+
import com.creatubbles.api.response.landingurls.GetLandingUrlsResponse;
6+
import com.creatubbles.api.util.HttpMethod;
7+
8+
import static com.creatubbles.api.util.EndPoints.LANDING_URLS;
9+
10+
/**
11+
* Created by Jevgeni on 09.03.2016.
12+
*/
13+
@APIVersion(2)
14+
public class GetLandingUrlsRequest extends CreatubblesRequest<GetLandingUrlsResponse> {
15+
16+
public GetLandingUrlsRequest(String accessToken) {
17+
super(LANDING_URLS, HttpMethod.GET, accessToken);
18+
}
19+
20+
@Override
21+
public Class<? extends GetLandingUrlsResponse> getResponseClass() {
22+
return GetLandingUrlsResponse.class;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.creatubbles.api.request.landingurls;
2+
3+
import com.creatubbles.api.APIVersion;
4+
import com.creatubbles.api.core.CreatubblesRequest;
5+
import com.creatubbles.api.core.LandingUrl;
6+
import com.creatubbles.api.response.landingurls.GetSpecificLandingUrlResponse;
7+
import com.creatubbles.api.util.HttpMethod;
8+
9+
import static com.creatubbles.api.util.EndPoints.SPECIFIC_LANDING_URL;
10+
11+
/**
12+
* Created by Jevgeni on 09.03.2016.
13+
*/
14+
@APIVersion(2)
15+
public class GetSpecificLandingUrlRequest extends CreatubblesRequest<GetSpecificLandingUrlResponse> {
16+
17+
public GetSpecificLandingUrlRequest(String accessToken, LandingUrl.LandingUrlType type) {
18+
super(String.format(SPECIFIC_LANDING_URL, type), HttpMethod.GET, accessToken);
19+
}
20+
21+
@Override
22+
public Class<? extends GetSpecificLandingUrlResponse> getResponseClass() {
23+
return GetSpecificLandingUrlResponse.class;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.creatubbles.api.response.amazon;
22

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

56
/**
67
* Created by Jevgeni on 28.10.2015.
78
*/
8-
public class UploadS3ImageResponse extends CreatubblesResponse {
9+
@APIVersion(2)
10+
public class UploadS3FileResponse extends CreatubblesResponse {
911

1012
public boolean success;
1113
}

src/main/java/com/creatubbles/api/response/creation/CreationsUploadsResponse.java

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public class CreationsUploadsResponse extends CreatubblesResponse implements Jso
1111

1212
public int id;
1313
public String url;
14+
public String content_type;
15+
public String ping_url;
1416

1517
@Override
1618
public CreationsUploadsResponse deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
@@ -28,6 +30,8 @@ private void deserializeData(JsonObject jsonObject) {
2830
JsonElement idE = data.get("id");
2931
if (idE != null && attributes != null) {
3032
url = attributes.get("url").getAsString();
33+
content_type = attributes.get("content_type").getAsString();
34+
ping_url = attributes.get("ping_url").getAsString();
3135
id = idE.getAsInt();
3236
}
3337
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.creatubbles.api.response.landingurls;
2+
3+
import com.creatubbles.api.APIVersion;
4+
import com.creatubbles.api.core.CreatubblesResponse;
5+
import com.creatubbles.api.core.LandingUrl;
6+
import com.google.gson.*;
7+
import jersey.repackaged.com.google.common.collect.Lists;
8+
9+
import java.lang.reflect.Type;
10+
import java.util.List;
11+
12+
/**
13+
* Created by Jevgeni on 10.03.2016.
14+
*/
15+
@APIVersion(2)
16+
public class GetLandingUrlsResponse extends CreatubblesResponse implements JsonDeserializer<GetLandingUrlsResponse> {
17+
18+
public List<LandingUrl> urls;
19+
20+
@Override
21+
public GetLandingUrlsResponse deserialize(JsonElement jsonElement, Type typeOfT, JsonDeserializationContext context) throws
22+
JsonParseException {
23+
if (!jsonElement.isJsonPrimitive()) {
24+
JsonObject jsonObject = (JsonObject) jsonElement;
25+
deserializeData(jsonObject);
26+
}
27+
return this;
28+
}
29+
30+
private void deserializeData(JsonObject jsonObject) {
31+
JsonArray data = jsonObject.getAsJsonArray("data");
32+
if (data != null) {
33+
urls = Lists.newArrayList();
34+
for (int i = 0; i < data.size(); i++) {
35+
JsonElement elem = data.get(i);
36+
if (!elem.isJsonPrimitive()) {
37+
JsonObject obj = (JsonObject) elem;
38+
JsonObject attributes = obj.getAsJsonObject("attributes");
39+
JsonElement idE = obj.get("id");
40+
if (idE != null && attributes != null) {
41+
LandingUrl landingUrl = new LandingUrl();
42+
landingUrl.type = LandingUrl.LandingUrlType.from(idE.getAsString());
43+
landingUrl.url = attributes.get("url").getAsString();
44+
urls.add(landingUrl);
45+
}
46+
}
47+
}
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)