Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.proxyseller.Api Maven / Gradle / Ivy
package org.proxyseller;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Api {
private static final String BASE_URL = "https://proxy-seller.com/personal/api/v1/";
private Config config;
/**
* Key placed in https://proxy-seller.com/personal/api/
*
* @param config the configuration
* @throws Exception if an error occurs
*/
public Api(Config config) throws Exception {
if (config.getKey().isEmpty()) {
throw new Exception("Need key, placed in https://proxy-seller.com/personal/api/");
}
if (config.getBaseUri() == null || !config.getBaseUri().isBlank()) {
config.setBaseUri(BASE_URL + config.getKey() + "/");
}
this.config = config;
}
/**
* Send request to the server.
*
* @param method The HTTP method to use.
* @param uri The URI of the server.
* @param options Additional options for the request.
* @return Object The result of the request.
* @throws Exception If an error occurs during the request.
*/
protected Object request(String method, String uri, RequestOptions options) throws Exception {
// Get query params
String fullUrl = config.getBaseUri() + uri;
if (options != null && options.getQuery() != null) {
String queryString = options.getQuery().entrySet().stream()
.map(entry -> entry.getKey() + "=" + entry.getValue())
.collect(Collectors.joining("&"));
fullUrl += "?" + queryString;
}
try {
URL url = new URL(fullUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod(method.toUpperCase());
// Body content
if (options != null && !options.getJson().isEmpty()) {
connection.setRequestProperty("Content-Type", "application/json");
connection.setDoOutput(true);
Gson gson = new Gson();
String jsonString = gson.toJson(options.getJson());
// String jsonString = options.getJson().entrySet().stream()
// .map(entry -> "\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"")
// .collect(Collectors.joining(", ", "{", "}"));
byte[] jsonBytes = jsonString.getBytes(StandardCharsets.UTF_8);
try (OutputStream outputStream = connection.getOutputStream()) {
outputStream.write(jsonBytes);
}
}
// Process response
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
String responseBody = response.toString();
try {
Map json = parseJson(responseBody);
if ("success".equals(json.get("status"))) {
return json.get("data");
} else if (json.containsKey("errors")) {
List> errors = (List>) json.get("errors");
throw new Exception(errors.get(0).get("message").toString());
}
} catch (JsonSyntaxException ignoring) {
}
return responseBody;
}
} else {
throw new IOException("Request failed with response code: " + responseCode);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static Map parseJson(String json) {
Gson gson = new Gson();
Type type = new TypeToken>() {
}.getType();
Map map = gson.fromJson(json, type);
return map;
}
/**
* Send request to the server.
*
* @param method method The HTTP method to use.
* @param uri uri The URI of the server.
* @return Object The result of the request.
* @throws Exception If an error occurs during the request.
*/
protected Object request(String method, String uri) throws Exception {
return request(method, uri, null);
}
/**
* Get balance statistic.
*
* @return float The balance statistic value.
* @throws Exception Error
*/
public Double balance() throws Exception {
return (Double) (((Map) request("get", "balance/get")).get("summ"));
}
/**
* Replenish the balance.
*
* @param summ The amount to be replenished.
* @param paymentId The ID of the payment.
* @return String A link to the payment page.
* Example: "... "
* @throws Exception Error
*/
public String balanceAdd(Double summ, Integer paymentId) throws Exception {
RequestOptions options = new RequestOptions();
LinkedHashMap map = new LinkedHashMap<>();
map.put("summ", summ);
map.put("paymentId", paymentId);
options.setJson(map);
return ((Map) request("post", "balance/add", options)).get("url").toString();
}
/**
* Get a list of payment systems for balance replenishing.
*
* @return An array of payment system items.
* Example items:
* [
* {
* 'id' : '29',
* 'name' : 'PayPal'
* },
* {
* 'id' : '37',
* 'name' : 'Visa / MasterCard'
* }
* ]
* @throws Exception Error
*/
public List balancePaymentsList() throws Exception {
return ((List) ((Map) request("get", "balance/payments/list")).get("items"));
}
/**
* Get necessary guides for creating an order.
*
* @param type The type of order (ipv4, ipv6, mobile, isp, mix, '').
* @return The necessary guides for creating an order.
* @throws Exception Error
*/
public Map referenceList(String type) throws Exception {
return ((Map) (request("get", "reference/list/" + type)));
}
/**
* Get necessary guides for creating an order.
*
* @return The necessary guides for creating an order.
* @throws Exception Error
*/
public Map referenceList() throws Exception {
return referenceList("");
}
/**
* Calculate the order IPv4
* Preliminary order calculation
* An error in warning must be corrected before placing an order.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @return An array containing the order details
* Example output:
* [
* 'warning': 'Insufficient funds. Total $2. Not enough $33.10',
* 'balance': 2,
* 'total': 35.1,
* 'quantity': 5,
* 'currency': 'USD',
* 'discount': 0.22,
* 'price': 7.02
* ]
* @throws Exception Error
*/
public Map orderCalcIpv4(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) throws Exception {
return orderCalc(prepareIpv4(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName));
}
/**
* Calculate the order ISP
* Preliminary order calculation
* An error in warning must be corrected before placing an order.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @return An array containing the order details
* Example output:
* [
* 'warning': 'Insufficient funds. Total $2. Not enough $33.10',
* 'balance': 2,
* 'total': 35.1,
* 'quantity': 5,
* 'currency': 'USD',
* 'discount': 0.22,
* 'price': 7.02
* ]
* @throws Exception Error
*/
public Map orderCalcIsp(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) throws Exception {
return orderCalc(prepareIpv4(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName));
}
/**
* Calculate the order MIX
* Preliminary order calculation
* An error in warning must be corrected before placing an order.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @return An array containing the order details
* Example output:
* [
* 'warning': 'Insufficient funds. Total $2. Not enough $33.10',
* 'balance': 2,
* 'total': 35.1,
* 'quantity': 5,
* 'currency': 'USD',
* 'discount': 0.22,
* 'price': 7.02
* ]
* @throws Exception Error
*/
public Map orderCalcMix(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) throws Exception {
return orderCalc(prepareIpv4(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName));
}
/**
* Calculate the order IPv6
* Preliminary order calculation
* An error in warning must be corrected before placing an order.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @param protocol The protocol HTTPS or SOCKS5.
* @return An array containing the order details
* Example output:
* [
* 'warning': 'Insufficient funds. Total $2. Not enough $33.10',
* 'balance': 2,
* 'total': 35.1,
* 'quantity': 5,
* 'currency': 'USD',
* 'discount': 0.22,
* 'price': 7.02
* ]
* @throws Exception Error
*/
public Map orderCalcIpv6(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName, String protocol) throws Exception {
return orderCalc(prepareIpv6(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName, protocol));
}
/**
* Calculate the order Mobile
* Preliminary order calculation
* An error in warning must be corrected before placing an order.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param operatorId The ID of the operator
* @param rotationId The ID of the rotation
* @return An array containing the order details
* Example output:
* [
* 'warning': 'Insufficient funds. Total $2. Not enough $33.10',
* 'balance': 2,
* 'total': 35.1,
* 'quantity': 5,
* 'currency': 'USD',
* 'discount': 0.22,
* 'price': 7.02
* ]
* @throws Exception Error
*/
public Map orderCalcMobile(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer operatorId, Integer rotationId) throws Exception {
return orderCalc(prepareMobile(countryId, periodId, quantity, authorization, coupon, operatorId, rotationId));
}
/**
* Create an order IPv4
* Attention! Calling this method will deduct $ from your balance!
* The parameters are identical to the /order/calc method. Practice there before calling the /order/make method.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @return An array containing the order details
* Example output:
* [
* 'orderId': 1000000,
* 'total': 35.1,
* 'balance': 10.19
* ]
* @throws Exception Error
*/
public Map orderMakeIpv4(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) throws Exception {
return orderMake(prepareIpv4(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName));
}
/**
* Create an order ISP
* Attention! Calling this method will deduct $ from your balance!
* The parameters are identical to the /order/calc method. Practice there before calling the /order/make method.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @return An array containing the order details
* Example output:
* [
* 'orderId': 1000000,
* 'total': 35.1,
* 'balance': 10.19
* ]
* @throws Exception Error
*/
public Map orderMakeIsp(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) throws Exception {
return orderMake(prepareIpv4(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName));
}
/**
* Create an order MIX
* Attention! Calling this method will deduct $ from your balance!
* The parameters are identical to the /order/calc method. Practice there before calling the /order/make method.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @return An array containing the order details
* Example output:
* [
* 'orderId': 1000000,
* 'total': 35.1,
* 'balance': 10.19
* ]
* @throws Exception Error
*/
public Map orderMakeMix(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) throws Exception {
return orderMake(prepareIpv4(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName));
}
/**
* Create an order IPv6
* Attention! Calling this method will deduct $ from your balance!
* The parameters are identical to the /order/calc method. Practice there before calling the /order/make method.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param targetSectionId The ID of the target section
* @param targetId The ID of the target
* @param customTargetName The custom target name
* @param protocol The protocol HTTPS or SOCKS5.
* @return An array containing the order details
* Example output:
* [
* 'orderId': 1000000,
* 'total': 35.1,
* 'balance': 10.19
* ]
* @throws Exception Error
*/
public Map orderMakeIpv6(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName, String protocol) throws Exception {
return orderMake(prepareIpv6(countryId, periodId, quantity, authorization, coupon, targetSectionId, targetId, customTargetName, protocol));
}
/**
* Create an order Mobile
* Attention! Calling this method will deduct $ from your balance!
* The parameters are identical to the /order/calc method. Practice there before calling the /order/make method.
*
* @param countryId The ID of the country
* @param periodId The ID of the period
* @param quantity The quantity of the order
* @param authorization IP whitelist (if need)
* @param coupon The coupon code
* @param operatorId The ID of the operator
* @param rotationId The ID of the rotation
* @return An array containing the order details
* Example output:
* [
* 'orderId': 1000000,
* 'total': 35.1,
* 'balance': 10.19
* ]
* @throws Exception Error
*/
public Map orderMakeMobile(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer operatorId, Integer rotationId) throws Exception {
return orderMake(prepareMobile(countryId, periodId, quantity, authorization, coupon, operatorId, rotationId));
}
protected static Map prepareIpv4(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName) {
LinkedHashMap map = new LinkedHashMap<>();
map.put("countryId", countryId);
map.put("periodId", periodId);
map.put("quantity", quantity);
map.put("authorization", authorization);
map.put("coupon", coupon);
map.put("targetSectionId", targetSectionId);
map.put("targetId", targetId);
map.put("customTargetName", customTargetName);
return map;
}
protected static Map prepareIpv6(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer targetSectionId, Integer targetId, String customTargetName, String protocol) {
LinkedHashMap map = new LinkedHashMap<>();
map.put("countryId", countryId);
map.put("periodId", periodId);
map.put("quantity", quantity);
map.put("authorization", authorization);
map.put("coupon", coupon);
map.put("targetSectionId", targetSectionId);
map.put("targetId", targetId);
map.put("customTargetName", customTargetName);
map.put("protocol", protocol);
return map;
}
protected static Map prepareMobile(Integer countryId, String periodId, Integer quantity, String authorization, String coupon, Integer operatorId, Integer rotationId) {
LinkedHashMap map = new LinkedHashMap<>();
map.put("countryId", countryId);
map.put("periodId", periodId);
map.put("quantity", quantity);
map.put("authorization", authorization);
map.put("coupon", coupon);
map.put("operatorId", operatorId);
map.put("rotationId", rotationId);
return map;
}
/**
* Calculate the order.
*
* @param json A free format map to send to the endpoint.
* @return The result of the order calculation.
* @throws Exception Error
*/
public Map orderCalc(Map json) throws Exception {
RequestOptions options = new RequestOptions();
options.setJson(json);
return ((Map) (request("post", "order/calc", options)));
}
/**
* Create an order.
*
* @param json A free format map to send to the endpoint.
* @return The result of the order calculation.
* @throws Exception Error
*/
public Map orderMake(Map json) throws Exception {
RequestOptions options = new RequestOptions();
options.setJson(json);
return ((Map) (request("post", "order/make", options)));
}
protected static Map prepareProlong(List ids, String periodId, String coupon) {
LinkedHashMap map = new LinkedHashMap<>();
map.put("ids", ids);
map.put("periodId", periodId);
map.put("coupon", coupon);
return map;
}
/**
* Calculate the renewal.
*
* @param type The type of the renewal (ipv4, ipv6, mobile, isp, mix).
* @param ids The list of IDs.
* @param periodId The period ID.
* @param coupon The coupon code.
* @return The result of the renewal calculation.
* Example:
* {
* 'warning': 'Insufficient funds. Total $2. Not enough $33.10',
* 'balance': 2,
* 'total': 35.1,
* 'quantity': 5,
* 'currency': 'USD',
* 'discount': 0.22,
* 'price': 7.02,
* 'items': [],
* 'orders': 1
* }
* @throws Exception Error
*/
public Map prolongCalc(String type, List ids, String periodId, String coupon) throws Exception {
RequestOptions options = new RequestOptions();
options.setJson(prepareProlong(ids, periodId, coupon));
return ((Map) (request("post", "prolong/calc/" + type, options)));
}
/**
* Create a renewal order.
* Attention! Calling this method will deduct $ from your balance!
* The parameters are identical to the /prolong/calc method. Practice there before calling the /prolong/make method.
* @param type The type of the renewal (ipv4, ipv6, mobile, isp, mix).
* @param ids The list of IDs.
* @param periodId The period ID.
* @param coupon The coupon code.
* @return The result of the renewal order creation.
* Example:
* {
* 'orderId': 1000000,
* 'total': 35.1,
* 'balance': 10.19
* }
* @throws Exception Error
*/
public Map prolongMake(String type, List ids, String periodId, String coupon) throws Exception {
RequestOptions options = new RequestOptions();
options.setJson(prepareProlong(ids, periodId, coupon));
return ((Map) (request("post", "prolong/make/" + type, options)));
}
/**
* Get the list of proxies.
*
* @param type The type of proxies (ipv4, ipv6, mobile, isp, mix, '').
* @return The list of proxies.
* Example:
* [
* {
* 'id' : 9876543,
* 'order_id' : 123456,
* 'basket_id' : 9123456,
* 'ip' : '127.0.0.2',
* 'ip_only' : '127.0.0.2',
* 'protocol' : 'HTTP',
* 'port_socks' : 50101,
* 'port_http' : 50100,
* 'login' : 'login',
* 'password' : 'password',
* 'auth_ip' : '',
* 'rotation' : '',
* 'link_reboot' : '#',
* 'country' : 'France',
* 'country_alpha3' : 'FRA',
* 'status' : 'Active',
* 'status_type' : 'ACTIVE',
* 'can_prolong' : 1,
* 'date_start' : '26.06.2023',
* 'date_end' : '26.07.2023',
* 'comment' : '',
* 'auto_renew' : 'Y',
* 'auto_renew_period' : ''
* }
* ]
* @throws Exception Error
*/
public Map proxyList(String type) throws Exception {
return ((Map) (request("get", "proxy/list/" + type)));
}
/**
* Get the list of all proxy types.
*
* @return The list of proxies.
* Example:
* [
* {
* 'id' : 9876543,
* 'order_id' : 123456,
* 'basket_id' : 9123456,
* 'ip' : '127.0.0.2',
* 'ip_only' : '127.0.0.2',
* 'protocol' : 'HTTP',
* 'port_socks' : 50101,
* 'port_http' : 50100,
* 'login' : 'login',
* 'password' : 'password',
* 'auth_ip' : '',
* 'rotation' : '',
* 'link_reboot' : '#',
* 'country' : 'France',
* 'country_alpha3' : 'FRA',
* 'status' : 'Active',
* 'status_type' : 'ACTIVE',
* 'can_prolong' : 1,
* 'date_start' : '26.06.2023',
* 'date_end' : '26.07.2023',
* 'comment' : '',
* 'auto_renew' : 'Y',
* 'auto_renew_period' : ''
* }
* ]
* @throws Exception Error
*/
public Map proxyList() throws Exception {
return proxyList("");
}
/**
* Export proxies of a certain type in TXT or CSV format.
*
* @param type The type of proxies (ipv4, ipv6, mobile, isp, mix).
* @param ext txt/csv
* @param proto HTTPS/SOCKS
* @return String The exported proxies in the specified format.
* Example:
* login:[email protected] :50100
* @throws Exception Error
*/
public String proxyDownload(String type, String ext, String proto) throws Exception {
RequestOptions options = new RequestOptions();
LinkedHashMap map = new LinkedHashMap<>();
map.put("ext", ext);
map.put("proto", proto);
options.setQuery(map);
return request("get", "proxy/download/" + type, options).toString();
}
/**
* Set a comment for proxies.
*
* @param ids The list of IDs for the proxies.
* @param comment The comment to set.
* @return Integer The count of updated proxies.
* @throws Exception Error
*/
public Integer proxyCommentSet(List ids, String comment) throws Exception {
RequestOptions options = new RequestOptions();
LinkedHashMap map = new LinkedHashMap<>();
map.put("ids", ids);
map.put("comment", comment);
options.setJson(map);
return ((Double) (((Map) request("post", "proxy/comment/set", options)).get("updated"))).intValue();
}
/**
* Check a single proxy.
*
* @param proxy The proxy to check. Available values: user:[email protected] :8080, [email protected] :8080, 127.0.0.1:8080.
* @return The result of the proxy check.
* Example result:
* {
* 'ip' : '127.0.0.1',
* 'port' : 8080,
* 'user' : 'user',
* 'password' : 'password',
* 'valid' : true,
* 'protocol' : 'HTTP',
* 'time' : 1234
* }
* @throws Exception Error
*/
public Map proxyCheck(String proxy) throws Exception {
RequestOptions options = new RequestOptions();
LinkedHashMap map = new LinkedHashMap<>();
map.put("proxy", proxy);
options.setQuery(map);
return (Map) request("get", "tools/proxy/check", options);
}
/**
* Check the availability of the service.
*
* @return Integer The timestamp indicating the availability of the service.
* @throws Exception Error
*/
public Integer ping() throws Exception {
return ((Double) ((Map) request("get", "system/ping")).get("pong")).intValue();
}
public static String getBaseURL() {
return BASE_URL;
}
public Config getConfig() {
return config;
}
public void setConfig(Config config) {
this.config = config;
}
}