Platform() { super(new ProductionApiClient(apiKey, new OkHttpClient.Builder() .connectTimeout(1, TimeUnit.SECONDS) .writeTimeout(3, TimeUnit.SECONDS) .readTimeout(3, TimeUnit.SECONDS) .build()), executorService); }
@Override public QueueInformation retrieveOfflineQueue() throws IOException, ApiException { return get("/queue/offline-commands", CacheControl.FORCE_NETWORK, QueueInformation.class); }
@Override public void deleteCoupon(String id) throws IOException, ApiException { Request request = getBuilder("/coupons/" + id + "/code") .delete() .build(); Response response = httpClient.newCall(request).execute(); try (ResponseBody rspBody = response.body()) { if (!response.isSuccessful()) { throw handleError(response, rspBody); } } }
Request request = getBuilder("/queue") .method("DELETE", builder.build()) .build();
} else { getLogger().info("Validating your server key..."); ApiClient client = new ProductionApiClient(configuration.getServerKey(), httpClient, this.getLogger()); try { updateInformation(client);
@Override public void deleteCoupon(int id) throws IOException, ApiException { Request request = getBuilder("/coupons/" + id) .delete() .build(); Response response = httpClient.newCall(request).execute(); try (ResponseBody rspBody = response.body()) { if (!response.isSuccessful()) { throw handleError(response, rspBody); } } }
@Override public ServerInformation getServerInformation() throws IOException, ApiException { return get("/information", CacheControl.FORCE_NETWORK, ServerInformation.class); }
} else { getLogger().info("Validating your server key..."); ApiClient client = new ProductionApiClient(configuration.getServerKey(), httpClient); try { updateInformation(client);
@Override public CheckoutUrlResponse getCheckoutUri(String username, int packageId) throws IOException, ApiException { RequestBody body = new FormBody.Builder() .add("username", username) .add("package_id", Integer.toString(packageId)) .build(); Request request = getBuilder("/checkout") .post(body) .build(); Response response = httpClient.newCall(request).execute(); try (ResponseBody rspBody = response.body()) { if (!response.isSuccessful()) { throw handleError(response, rspBody); } else { return gson.fromJson(rspBody.charStream(), CheckoutUrlResponse.class); } } }
@Override public DueQueueInformation retrieveDueQueue() throws IOException, ApiException { return get("/queue", CacheControl.FORCE_NETWORK, DueQueueInformation.class); }
} else { getLogger().info("Validating your server key..."); final ApiClient client = new ProductionApiClient(configuration.getServerKey(), httpClient);
private <T> T get(String endpoint, CacheControl control, Type type) throws IOException, ApiException { try { Request.Builder requestBuilder = getBuilder(endpoint).get(); if (control != null) requestBuilder.cacheControl(control); Request request = requestBuilder.build(); Response response = httpClient.newCall(request).execute(); try (ResponseBody body = response.body()) { if (response.isSuccessful()) { try { return gson.fromJson(body.charStream(), type); } catch (JsonSyntaxException e) { throw new ApiException("Unable to parse response.", response.request(), response, body.string()); } } else { throw handleError(response, body); } } } catch (Exception e) { if(this.logger != null) { this.logger.severe("Unable to connect to API. Please check that your secret key is correct."); } return null; } }
@Override public QueueInformation getPlayerQueue(int id) throws IOException, ApiException { return get("/queue/online-commands/" + id, CacheControl.FORCE_NETWORK, QueueInformation.class); }
} else { getLogger().info("Validating your server key..."); final ApiClient client = new ProductionApiClient(configuration.getServerKey(), httpClient); try { updateInformation(client);
@Override public CheckoutUrlResponse getCategoryUri(String username, int categoryId) throws IOException, ApiException { RequestBody body = new FormBody.Builder() .add("username", username) .add("category", "true") .add("category_id", Integer.toString(categoryId)) .build(); Request request = getBuilder("/checkout") .post(body) .build(); Response response = httpClient.newCall(request).execute(); try (ResponseBody rspBody = response.body()) { if (!response.isSuccessful()) { throw handleError(response, rspBody); } else { return gson.fromJson(rspBody.charStream(), CheckoutUrlResponse.class); } } }
private <T> T get(String endpoint, Type type) throws IOException, ApiException { return get(endpoint, null, type); }
@Override public void run() { ApiClient client = new ProductionApiClient(args[0], plugin.getHttpClient()); try { plugin.updateInformation(client); } catch (IOException | ApiException e) { plugin.getLogger().log(Level.SEVERE, "Unable to verify secret", e); sender.sendMessage(ChatColor.RED + plugin.getI18n().get("secret_does_not_work")); return; } ServerInformation information = plugin.getServerInformation(); plugin.setApiClient(client); plugin.getConfiguration().setServerKey(args[0]); try { plugin.saveConfiguration(); } catch (IOException e) { sender.sendMessage(ChatColor.RED + plugin.getI18n().get("secret_cant_be_saved")); } sender.sendMessage(ChatColor.GREEN + plugin.getI18n().get("secret_success", information.getServer().getName(), information.getAccount().getName())); plugin.getProxy().getScheduler().runAsync(plugin, plugin.getDuePlayerFetcher()); } });
.build(); Request request = getBuilder("/coupons") .post(body) .build(); throw handleError(response, rspBody); } else { return gson.fromJson(rspBody.charStream(), CouponSingleListing.class).getData();
@Override public Listing retrieveListing() throws IOException, ApiException { Listing listing = get("/listing", CacheControl.FORCE_NETWORK, Listing.class); if(listing != null) listing.order(); return listing; }
@Override public void run() { String currentKey = plugin.getConfiguration().getServerKey(); ApiClient client = new ProductionApiClient((String) args.getOne("secret").get(), plugin.getHttpClient()); try { plugin.updateInformation(client); } catch (IOException | ApiException e) { plugin.getLogger().error("Unable to verify secret", e); src.sendMessage(Text.builder(plugin.getI18n().get("secret_does_not_work")).color(TextColors.RED).build()); return; } ServerInformation information = plugin.getServerInformation(); plugin.setApiClient(client); plugin.getListingUpdateTask().run(); plugin.getConfiguration().setServerKey((String) args.getOne("secret").get()); try { plugin.saveConfiguration(); } catch (IOException e) { src.sendMessage(Text.builder(plugin.getI18n().get("secret_cant_be_saved")).color(TextColors.RED).build()); } src.sendMessage(Text.builder(plugin.getI18n().get("secret_success", information.getServer().getName(), information.getAccount().getName())).color(TextColors.GREEN).build()); boolean repeatChecks = false; if (currentKey == "INVALID") { repeatChecks = true; } plugin.getDuePlayerFetcher().run(repeatChecks); } });