44import java .io .IOException ;
55import java .lang .reflect .Type ;
66import java .nio .file .Files ;
7+ import java .util .Map .Entry ;
78
89import org .glassfish .jersey .client .ClientProperties ;
910import org .glassfish .jersey .client .JerseyClient ;
1011import org .glassfish .jersey .client .JerseyClientBuilder ;
1112
12- import com .creatubbles .api .core .Gallery ;
1313import com .creatubbles .api .core .LandingUrl ;
1414import com .creatubbles .api .request .amazon .UploadS3FileRequest ;
1515import com .creatubbles .api .request .creation .CreateCreationRequest ;
1616import com .creatubbles .api .request .creation .CreationsUploadsRequest ;
1717import com .creatubbles .api .request .creation .PingCreationsUploadsRequest ;
1818import com .creatubbles .api .request .landingurls .GetLandingUrlsRequest ;
1919import com .creatubbles .api .request .landingurls .GetSpecificLandingUrlRequest ;
20- import com .creatubbles .api .response .auth .SignUpResponse ;
2120import com .creatubbles .api .response .creation .CreateCreationResponse ;
2221import 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 ;
2822import com .creatubbles .api .response .landingurls .GetLandingUrlsResponse ;
2923import com .creatubbles .api .response .landingurls .GetSpecificLandingUrlResponse ;
30- import com .creatubbles .api .response .user .UserProfileResponse ;
31- import com .creatubbles .api .util .EndPoints ;
3224import com .creatubbles .api .util .HttpUtil ;
3325import com .google .gson .Gson ;
3426import com .google .gson .GsonBuilder ;
3527import com .google .gson .JsonDeserializationContext ;
3628import com .google .gson .JsonDeserializer ;
3729import com .google .gson .JsonElement ;
30+ import com .google .gson .JsonObject ;
3831import 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 ;
3937
4038
41- @ SuppressWarnings ("deprecation" )
4239public 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+
4345 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 ())
5746 .registerTypeAdapter (String .class , new StringAdapter ())
47+ .registerTypeAdapterFactory (new JsonHackx ())
5848 .create ();
5949
6050 public final static JerseyClient CLIENT = JerseyClientBuilder
@@ -63,11 +53,12 @@ public class CreatubblesAPI {
6353 .property (ClientProperties .READ_TIMEOUT , 5000 )
6454 .property (ClientProperties .SUPPRESS_HTTP_COMPLIANCE_VALIDATION , Boolean .TRUE );
6555
66- public static String buildURL (final String endPoint ) {
56+ public static String buildURL (final Object end ) {
57+ String endPoint = end .toString ();
6758 if (endPoint .startsWith ("https://" )) {
6859 return endPoint ;
6960 }
70- String base = staging ? EndPoints . URL_BASE_STAGING : EndPoints . URL_BASE ;
61+ String base = staging ? URL_BASE_STAGING : URL_BASE ;
7162 return base .concat (endPoint );
7263 }
7364
@@ -77,6 +68,10 @@ public static void setStagingMode(boolean staging) {
7768 CreatubblesAPI .staging = staging ;
7869 }
7970
71+ public static boolean stagingModeEnabled () {
72+ return staging ;
73+ }
74+
8075 public static void main (String [] args ) throws IOException {
8176 // Additional examples can be found in the JUnit test files
8277
@@ -85,38 +80,38 @@ public static void main(String[] args) throws IOException {
8580
8681 CreateCreationRequest createCreation = new CreateCreationRequest (accessToken );
8782 CreateCreationResponse createCreationResponse = createCreation .execute ().getResponse ();
88- System .out .println (createCreationResponse .creation . id );
83+ System .out .println (createCreationResponse .getCreation (). getId () );
8984
9085 File file = new File ("C:/dev/1.png" );
9186 String extension = HttpUtil .getExtension (file .getPath ());
9287
93- CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest (createCreationResponse .creation . id , extension , accessToken );
88+ CreationsUploadsRequest creationsUploads = new CreationsUploadsRequest (createCreationResponse .getCreation (). getId () , extension , accessToken );
9489 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 () );
9792
9893 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 () );
10196 }
10297
10398 GetSpecificLandingUrlRequest getSpecificLandingUrl = new GetSpecificLandingUrlRequest (accessToken , LandingUrl .LandingUrlType .CTB_USER_PROFILE );
10499 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 () );
107102
108103 byte [] data = Files .readAllBytes (file .toPath ());
109104
110- UploadS3FileRequest uploadS3Image = new UploadS3FileRequest (data , creationsUploadsResponse .url , creationsUploadsResponse .content_type );
105+ UploadS3FileRequest uploadS3Image = new UploadS3FileRequest (data , creationsUploadsResponse .getUrl () , creationsUploadsResponse .getType () );
111106 uploadS3Image .execute ().getResponse ();
112107
113- PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest (creationsUploadsResponse .ping_url , accessToken );
108+ PingCreationsUploadsRequest pingCreationsUploads = new PingCreationsUploadsRequest (creationsUploadsResponse .getPingUrl () , accessToken );
114109 pingCreationsUploads .setData ("" );
115110 pingCreationsUploads .execute ().getResponse ();
116111 System .out .println ("-Finish-" );
117112 }
118113
119- static class StringAdapter implements JsonDeserializer <String > {
114+ private static class StringAdapter implements JsonDeserializer <String > {
120115
121116 public String deserialize (JsonElement json , Type typeOfT ,
122117 JsonDeserializationContext context )
@@ -125,5 +120,49 @@ public String deserialize(JsonElement json, Type typeOfT,
125120 return asString != null && asString .isEmpty () ? null : asString ;
126121 }
127122 }
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+ }
129168}
0 commit comments