4
4
import java .io .IOException ;
5
5
import java .lang .reflect .Type ;
6
6
import java .nio .file .Files ;
7
+ import java .util .Map .Entry ;
7
8
8
9
import org .glassfish .jersey .client .ClientProperties ;
9
10
import org .glassfish .jersey .client .JerseyClient ;
10
11
import org .glassfish .jersey .client .JerseyClientBuilder ;
11
12
12
- import com .creatubbles .api .core .Gallery ;
13
13
import com .creatubbles .api .core .LandingUrl ;
14
14
import com .creatubbles .api .request .amazon .UploadS3FileRequest ;
15
15
import com .creatubbles .api .request .creation .CreateCreationRequest ;
16
16
import com .creatubbles .api .request .creation .CreationsUploadsRequest ;
17
17
import com .creatubbles .api .request .creation .PingCreationsUploadsRequest ;
18
18
import com .creatubbles .api .request .landingurls .GetLandingUrlsRequest ;
19
19
import com .creatubbles .api .request .landingurls .GetSpecificLandingUrlRequest ;
20
- import com .creatubbles .api .response .auth .SignUpResponse ;
21
20
import com .creatubbles .api .response .creation .CreateCreationResponse ;
22
21
import com .creatubbles .api .response .creation .CreationsUploadsResponse ;
23
- import com .creatubbles .api .response .creation .GetCreationsResponse ;
24
- import com .creatubbles .api .response .creator .CreateCreatorResponse ;
25
- import com .creatubbles .api .response .creator .GetCreatorsResponse ;
26
- import com .creatubbles .api .response .gallery .CreateUserGalleryResponse ;
27
- import com .creatubbles .api .response .landingurls .GetCreationLandingUrlResponse ;
28
22
import com .creatubbles .api .response .landingurls .GetLandingUrlsResponse ;
29
23
import com .creatubbles .api .response .landingurls .GetSpecificLandingUrlResponse ;
30
- import com .creatubbles .api .response .user .UserProfileResponse ;
31
- import com .creatubbles .api .util .EndPoints ;
32
24
import com .creatubbles .api .util .HttpUtil ;
33
25
import com .google .gson .Gson ;
34
26
import com .google .gson .GsonBuilder ;
35
27
import com .google .gson .JsonDeserializationContext ;
36
28
import com .google .gson .JsonDeserializer ;
37
29
import com .google .gson .JsonElement ;
30
+ import com .google .gson .JsonObject ;
38
31
import com .google .gson .JsonParseException ;
32
+ import com .google .gson .TypeAdapter ;
33
+ import com .google .gson .TypeAdapterFactory ;
34
+ import com .google .gson .reflect .TypeToken ;
35
+ import com .google .gson .stream .JsonReader ;
36
+ import com .google .gson .stream .JsonWriter ;
39
37
40
38
41
- @ SuppressWarnings ("deprecation" )
42
39
public class CreatubblesAPI {
40
+
41
+
42
+ public static final String URL_BASE = "https://api.creatubbles.com/v2/" ;
43
+ public static final String URL_BASE_STAGING = "https://api.staging.creatubbles.com/v2/" ;
44
+
43
45
public final static Gson GSON = new GsonBuilder ()
44
- .registerTypeAdapter (SignUpResponse .class , new SignUpResponse ())
45
- .registerTypeAdapter (UserProfileResponse .class , new UserProfileResponse ())
46
- .registerTypeAdapter (CreateCreatorResponse .class , new CreateCreatorResponse ())
47
- .registerTypeAdapter (CreateUserGalleryResponse .class , new CreateUserGalleryResponse ())
48
- .registerTypeAdapter (CreateUserGalleryResponse .class , new CreateUserGalleryResponse ())
49
- .registerTypeAdapter (Gallery .class , new Gallery ())
50
- .registerTypeAdapter (GetCreatorsResponse .class , new GetCreatorsResponse ())
51
- .registerTypeAdapter (GetCreationsResponse .class , new GetCreationsResponse ())
52
- .registerTypeAdapter (CreateCreationResponse .class , new CreateCreationResponse ())
53
- .registerTypeAdapter (CreationsUploadsResponse .class , new CreationsUploadsResponse ())
54
- .registerTypeAdapter (GetLandingUrlsResponse .class , new GetLandingUrlsResponse ())
55
- .registerTypeAdapter (GetSpecificLandingUrlResponse .class , new GetSpecificLandingUrlResponse ())
56
- .registerTypeAdapter (GetCreationLandingUrlResponse .class , new GetCreationLandingUrlResponse ())
57
46
.registerTypeAdapter (String .class , new StringAdapter ())
47
+ .registerTypeAdapterFactory (new JsonHackx ())
58
48
.create ();
59
49
60
50
public final static JerseyClient CLIENT = JerseyClientBuilder
@@ -63,11 +53,12 @@ public class CreatubblesAPI {
63
53
.property (ClientProperties .READ_TIMEOUT , 5000 )
64
54
.property (ClientProperties .SUPPRESS_HTTP_COMPLIANCE_VALIDATION , Boolean .TRUE );
65
55
66
- public static String buildURL (final String endPoint ) {
56
+ public static String buildURL (final Object end ) {
57
+ String endPoint = end .toString ();
67
58
if (endPoint .startsWith ("https://" )) {
68
59
return endPoint ;
69
60
}
70
- String base = staging ? EndPoints . URL_BASE_STAGING : EndPoints . URL_BASE ;
61
+ String base = staging ? URL_BASE_STAGING : URL_BASE ;
71
62
return base .concat (endPoint );
72
63
}
73
64
@@ -77,6 +68,10 @@ public static void setStagingMode(boolean staging) {
77
68
CreatubblesAPI .staging = staging ;
78
69
}
79
70
71
+ public static boolean stagingModeEnabled () {
72
+ return staging ;
73
+ }
74
+
80
75
public static void main (String [] args ) throws IOException {
81
76
// Additional examples can be found in the JUnit test files
82
77
@@ -85,38 +80,38 @@ public static void main(String[] args) throws IOException {
85
80
86
81
CreateCreationRequest createCreation = new CreateCreationRequest (accessToken );
87
82
CreateCreationResponse createCreationResponse = createCreation .execute ().getResponse ();
88
- System .out .println (createCreationResponse .creation . id );
83
+ System .out .println (createCreationResponse .getCreation (). getId () );
89
84
90
85
File file = new File ("C:/dev/1.png" );
91
86
String extension = HttpUtil .getExtension (file .getPath ());
92
87
93
- CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest (createCreationResponse .creation . id , extension , accessToken );
88
+ CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest (createCreationResponse .getCreation (). getId () , extension , accessToken );
94
89
CreationsUploadsResponse creationsUploadsResponse = creationsUploads .execute ().getResponse ();
95
- System .out .println (creationsUploadsResponse .url );
96
- System .out .println (creationsUploadsResponse .id );
90
+ System .out .println (creationsUploadsResponse .getUrl () );
91
+ System .out .println (creationsUploadsResponse .getId () );
97
92
98
93
GetLandingUrlsRequest getLandingUrls = new GetLandingUrlsRequest (accessToken );
99
- for (LandingUrl landingUrl : getLandingUrls .execute ().getResponse (). urls ) {
100
- System .out .println (landingUrl .type + ":" + landingUrl .url );
94
+ for (GetLandingUrlsResponse landingUrl : getLandingUrls .execute ().getResponseList () ) {
95
+ System .out .println (landingUrl .getUrl (). getType () + ":" + landingUrl .getUrl (). getUrl () );
101
96
}
102
97
103
98
GetSpecificLandingUrlRequest getSpecificLandingUrl = new GetSpecificLandingUrlRequest (accessToken , LandingUrl .LandingUrlType .CTB_USER_PROFILE );
104
99
GetSpecificLandingUrlResponse getSpecificLandingUrlResponse = getSpecificLandingUrl .execute ().getResponse ();
105
- LandingUrl url = getSpecificLandingUrlResponse .url ;
106
- System .out .println ("specific url - " + url .type + ":" + url .url );
100
+ LandingUrl url = getSpecificLandingUrlResponse .getUrl () ;
101
+ System .out .println ("specific url - " + url .getType () + ":" + url .getUrl () );
107
102
108
103
byte [] data = Files .readAllBytes (file .toPath ());
109
104
110
- UploadS3FileRequest uploadS3Image = new UploadS3FileRequest (data , creationsUploadsResponse .url , creationsUploadsResponse .content_type );
105
+ UploadS3FileRequest uploadS3Image = new UploadS3FileRequest (data , creationsUploadsResponse .getUrl () , creationsUploadsResponse .getType () );
111
106
uploadS3Image .execute ().getResponse ();
112
107
113
- PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest (creationsUploadsResponse .ping_url , accessToken );
108
+ PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest (creationsUploadsResponse .getPingUrl () , accessToken );
114
109
pingCreationsUploads .setData ("" );
115
110
pingCreationsUploads .execute ().getResponse ();
116
111
System .out .println ("-Finish-" );
117
112
}
118
113
119
- static class StringAdapter implements JsonDeserializer <String > {
114
+ private static class StringAdapter implements JsonDeserializer <String > {
120
115
121
116
public String deserialize (JsonElement json , Type typeOfT ,
122
117
JsonDeserializationContext context )
@@ -125,5 +120,49 @@ public String deserialize(JsonElement json, Type typeOfT,
125
120
return asString != null && asString .isEmpty () ? null : asString ;
126
121
}
127
122
}
128
-
123
+
124
+ private static class JsonHackx implements TypeAdapterFactory {
125
+
126
+ @ Override
127
+ public <T > TypeAdapter <T > create (Gson gson , TypeToken <T > type ) {
128
+
129
+ final TypeAdapter <T > adapter = gson .getDelegateAdapter (this , type );
130
+ final TypeAdapter <JsonElement > elementAdapter = gson .getAdapter (JsonElement .class );
131
+
132
+ TypeAdapter <T > result = new TypeAdapter <T >() {
133
+
134
+ @ Override
135
+ public void write (JsonWriter out , T value ) throws IOException {
136
+ JsonObject object = adapter .toJsonTree (value ).getAsJsonObject ();
137
+ elementAdapter .write (out , object );
138
+ }
139
+
140
+ @ Override public T read (JsonReader in ) throws IOException {
141
+ JsonElement e = elementAdapter .read (in );
142
+
143
+ // Enter into all "data" fields immediately
144
+ if (e .isJsonObject () && e .getAsJsonObject ().has ("data" )) {
145
+ e = e .getAsJsonObject ().get ("data" );
146
+ }
147
+
148
+ // Add all "attributes" fields to the parent object so they are accessible without indirection
149
+ // Yeah this is a massive hack but it is necessary for things like landing URLs which only
150
+ // serve a single string inside the attributes
151
+ if (e .isJsonObject ()) {
152
+ JsonObject obj = e .getAsJsonObject ();
153
+ if (obj .has ("attributes" )) {
154
+ JsonObject attr = obj .get ("attributes" ).getAsJsonObject ();
155
+ for (Entry <String , JsonElement > entry : attr .entrySet ()) {
156
+ obj .add (entry .getKey (), entry .getValue ());
157
+ }
158
+ }
159
+ }
160
+
161
+ return adapter .fromJsonTree (e );
162
+ }
163
+ }.nullSafe (); // so we don't have to check for null on the stream
164
+
165
+ return result ;
166
+ }
167
+ }
129
168
}
0 commit comments