33import com .creatubbles .api .CreatubblesAPI ;
44import com .creatubbles .api .util .HttpMethod ;
55import org .glassfish .jersey .client .JerseyWebTarget ;
6- import org .glassfish .jersey .client .authentication .HttpAuthenticationFeature ;
76
87import javax .ws .rs .client .Entity ;
98import javax .ws .rs .client .Invocation ;
10- import javax .ws .rs .core .MediaType ;
119import javax .ws .rs .core .Response ;
1210import java .util .HashMap ;
1311import java .util .Map ;
@@ -22,7 +20,9 @@ public abstract class CreatubblesRequest<T extends CreatubblesResponse> {
2220 private Response response ;
2321 private Future <Response > futureResponse ;
2422 private T responseCache ;
23+ private String accessToken ;
2524 private static final String EMPTY_RESPONSE = "{}" ;
25+ private static final String APPLICATION_VND_API_JSON = "application/vnd.api+json" ;
2626
2727 public CreatubblesRequest (String endPoint , HttpMethod httpMethod ) {
2828 this (endPoint , httpMethod , null , null );
@@ -44,14 +44,11 @@ public CreatubblesRequest(String endPoint, HttpMethod httpMethod, String accessT
4444 } else {
4545 this .urlParameters = new HashMap <String , String >();
4646 }
47- if (accessToken != null ) {
48- this .urlParameters .put ("access_token" , accessToken );
49- }
47+ this .accessToken = accessToken ;
5048 }
5149
52- public CreatubblesRequest <T > setAccessToken (String accessToken ) {
53- this .urlParameters .put ("access_token" , accessToken );
54- return this ;
50+ public void setAccessToken (String accessToken ) {
51+ this .accessToken = accessToken ;
5552 }
5653
5754 public String getEndPoint () {
@@ -180,27 +177,30 @@ public CreatubblesRequest<T> execute() {
180177 webTarget = webTarget .queryParam (paramKey , paramValue );
181178 }
182179 }
183- HttpAuthenticationFeature basicAuth = HttpAuthenticationFeature .basic ("c" , "c" );
184- webTarget .register (basicAuth );
180+ //TODO: return if needed + check if staging
181+ //HttpAuthenticationFeature basicAuth = HttpAuthenticationFeature.basic("c", "c");
182+ //webTarget.register(basicAuth);
185183
186184 Invocation .Builder invocationBuilder = webTarget
187- .request (MediaType . APPLICATION_JSON )
188- .accept (MediaType . APPLICATION_JSON );
185+ .request (APPLICATION_VND_API_JSON )
186+ .accept (APPLICATION_VND_API_JSON );
189187
190188 if (acceptLanguage != null && acceptLanguage .length () == 2 ) {
191189 invocationBuilder .header ("Accept-Language" , acceptLanguage .toLowerCase ());
192190 }
193-
191+ if (accessToken != null && !accessToken .isEmpty ()) {
192+ invocationBuilder .header ("Authorization" , "Bearer " + accessToken );
193+ }
194194 if (xSource != null && !xSource .isEmpty ()) {
195195 invocationBuilder .header ("X-Source" , xSource .toLowerCase ());
196196 }
197197
198198 if (httpMethod == HttpMethod .GET ) {
199199 response = invocationBuilder .get ();
200200 } else if (httpMethod == HttpMethod .POST ) {
201- response = invocationBuilder .post (Entity .entity (data , MediaType . APPLICATION_JSON ));
201+ response = invocationBuilder .post (Entity .entity (data , APPLICATION_VND_API_JSON ));
202202 } else if (httpMethod == HttpMethod .PUT ) {
203- response = invocationBuilder .put (Entity .entity (data , MediaType . APPLICATION_JSON ));
203+ response = invocationBuilder .put (Entity .entity (data , APPLICATION_VND_API_JSON ));
204204 }
205205
206206 return this ;
@@ -219,23 +219,25 @@ public CreatubblesRequest<T> async() {
219219 }
220220
221221 Invocation .Builder invocationBuilder = webTarget
222- .request (MediaType . APPLICATION_JSON )
223- .accept (MediaType . APPLICATION_JSON );
222+ .request (APPLICATION_VND_API_JSON )
223+ .accept (APPLICATION_VND_API_JSON );
224224
225225 if (acceptLanguage != null && acceptLanguage .length () == 2 ) {
226226 invocationBuilder .header ("Accept-Language" , acceptLanguage .toLowerCase ());
227227 }
228-
228+ if (accessToken != null && !accessToken .isEmpty ()) {
229+ invocationBuilder .header ("Authorization" , "Bearer " + accessToken );
230+ }
229231 if (xSource != null && !xSource .isEmpty ()) {
230232 invocationBuilder .header ("X-Source" , xSource .toLowerCase ());
231233 }
232234
233235 if (httpMethod == HttpMethod .GET ) {
234236 futureResponse = invocationBuilder .async ().get ();
235237 } else if (httpMethod == HttpMethod .POST ) {
236- futureResponse = invocationBuilder .async ().post (Entity .entity (data , MediaType . APPLICATION_JSON ));
238+ futureResponse = invocationBuilder .async ().post (Entity .entity (data , APPLICATION_VND_API_JSON ));
237239 } else if (httpMethod == HttpMethod .PUT ) {
238- futureResponse = invocationBuilder .async ().put (Entity .entity (data , MediaType . APPLICATION_JSON ));
240+ futureResponse = invocationBuilder .async ().put (Entity .entity (data , APPLICATION_VND_API_JSON ));
239241 }
240242
241243 return this ;
0 commit comments