static String uri(final URI apiHost, final Map<String, List<String>> parameters, final String... segments) { final Builder builder = builder(apiHost, segments); for (Map.Entry<String, List<String>> entry : parameters.entrySet()) { for (String value : entry.getValue()) { builder.addQueryParameter(entry.getKey(), value); } } return builder.build().toString(); }
public Address(String uriHost, int uriPort, Dns dns, SocketFactory socketFactory, SSLSocketFactory sslSocketFactory, HostnameVerifier hostnameVerifier, CertificatePinner certificatePinner, Authenticator authenticator, Proxy proxy, List<Protocol> protocols, List<ConnectionSpec> connectionSpecs, ProxySelector proxySelector) { this.url = new Builder().scheme(sslSocketFactory != null ? b.a : "http").host(uriHost) .port(uriPort).build(); if (dns == null) { throw new IllegalArgumentException("dns == null"); } this.dns = dns; if (socketFactory == null) { throw new IllegalArgumentException("socketFactory == null"); } this.socketFactory = socketFactory; if (authenticator == null) { throw new IllegalArgumentException("authenticator == null"); } this.authenticator = authenticator; if (protocols == null) { throw new IllegalArgumentException("protocols == null"); } this.protocols = Util.immutableList((List) protocols); if (connectionSpecs == null) { throw new IllegalArgumentException("connectionSpecs == null"); } this.connectionSpecs = Util.immutableList((List) connectionSpecs); if (proxySelector == null) { throw new IllegalArgumentException("proxySelector == null"); } this.proxySelector = proxySelector; this.proxy = proxy; this.sslSocketFactory = sslSocketFactory; this.hostnameVerifier = hostnameVerifier; this.certificatePinner = certificatePinner; }
public void getAccessToken() throws IOException, PlaylyfeException { System.out.println("Getting Access Token"); JsonObject json = new JsonObject(); json.addProperty("client_id", client_id); json.addProperty("client_secret", client_secret); if(type.equals("client")) { json.addProperty("grant_type", "client_credentials"); } else { json.addProperty("grant_type", "authorization_code"); json.addProperty("code", code); json.addProperty("redirect_uri", redirect_uri); } Request request = new Request.Builder() .url("https://playlyfe.com/auth/token") .post(RequestBody.create(MEDIA_TYPE_JSON, json.toString())) .build(); Response response = client.newCall(request).execute(); final Map<String, Object> token = (Map<String, Object>) parseJson(response.body().string()); Long expires_at = System.currentTimeMillis() + (((Double) token.get("expires_in")).longValue() * 1000); token.remove("expires_in"); token.put("expires_at", expires_at); if(pac == null) { pac = new PersistAccessToken(){ @Override public void store(Map<String, Object> token) { System.out.println("Storing Access Token"); } @Override public Map<String, Object> load() { return token; } }; } pac.store(token); }
public Object api(String method, String route, Map<String, String> query, Object body, final boolean raw) throws IOException, PlaylyfeException { Builder urlBuilder = new HttpUrl.Builder() .scheme("https") .host("api.playlyfe.com") .encodedPath("/"+this.version+route); if (query != null) { for (Map.Entry<String, String> entry : query.entrySet()) { urlBuilder.addQueryParameter(entry.getKey(), entry.getValue()); } } Map<String, Object> token = null; if (pac != null) { token = pac.load(); } if(pac == null || token == null ||System.currentTimeMillis() >= ((Long) token.get("expires_at"))){ getAccessToken(); token = pac.load(); } urlBuilder.addQueryParameter("access_token", token.get("access_token").toString()); HttpUrl url = urlBuilder.build(); //System.out.println(url); String req_body = ""; if (body != null) { req_body = gson.toJson(body); } Request request; if(method.equalsIgnoreCase("GET")) { request = new Request.Builder() .url(url) .build(); } else if(method.equalsIgnoreCase("POST")) { request = new Request.Builder() .url(url) .post(RequestBody.create(MEDIA_TYPE_JSON, req_body)) .build(); } else if(method.equalsIgnoreCase("PUT")) { request = new Request.Builder() .url(url) .put(RequestBody.create(MEDIA_TYPE_JSON, req_body)) .build(); } else if(method.equalsIgnoreCase("PATCH")) { request = new Request.Builder() .url(url) .patch(RequestBody.create(MEDIA_TYPE_JSON, req_body)) .build(); } else if(method.equalsIgnoreCase("Delete")) { request = new Request.Builder() .url(url) .delete() .build(); } else { request = new Request.Builder() .url(url) .build(); } Response response = client.newCall(request).execute(); if(raw == true){ return response.body().bytes(); } else { return parseJson(response.body().string()); } }