From 894c1ec9dfd2c6864421b59dcec0f4c0fea51842 Mon Sep 17 00:00:00 2001 From: Railz Date: Mon, 24 Feb 2020 21:18:02 +0100 Subject: [PATCH] Imported first version --- 5sim_api/5sim_api.csproj | 5 + 5sim_api/Api.cs | 286 ++++++++++++++++++ 5sim_api/Class1.cs | 8 - 5sim_api/Exceptions/HostingOrderException.cs | 18 ++ 5sim_api/Exceptions/NoFreePhonesException.cs | 18 ++ .../NotEnoughProductQuantityException.cs | 18 ++ .../Exceptions/NotEnoughRatingException.cs | 18 ++ .../NotEnoughUserBalanceException.cs | 18 ++ 5sim_api/Exceptions/OrderExpiredException.cs | 18 ++ 5sim_api/Exceptions/OrderHasSmsException.cs | 18 ++ 5sim_api/Exceptions/OrderNotFoundException.cs | 18 ++ 5sim_api/Objects/Account.cs | 20 ++ 5sim_api/Objects/Number.cs | 36 +++ 5sim_api/Objects/Service.cs | 29 ++ 5sim_api/Objects/Sms.cs | 19 ++ 15 files changed, 539 insertions(+), 8 deletions(-) create mode 100644 5sim_api/Api.cs delete mode 100644 5sim_api/Class1.cs create mode 100644 5sim_api/Exceptions/HostingOrderException.cs create mode 100644 5sim_api/Exceptions/NoFreePhonesException.cs create mode 100644 5sim_api/Exceptions/NotEnoughProductQuantityException.cs create mode 100644 5sim_api/Exceptions/NotEnoughRatingException.cs create mode 100644 5sim_api/Exceptions/NotEnoughUserBalanceException.cs create mode 100644 5sim_api/Exceptions/OrderExpiredException.cs create mode 100644 5sim_api/Exceptions/OrderHasSmsException.cs create mode 100644 5sim_api/Exceptions/OrderNotFoundException.cs create mode 100644 5sim_api/Objects/Account.cs create mode 100644 5sim_api/Objects/Number.cs create mode 100644 5sim_api/Objects/Service.cs create mode 100644 5sim_api/Objects/Sms.cs diff --git a/5sim_api/5sim_api.csproj b/5sim_api/5sim_api.csproj index 2c08497..ea2d910 100644 --- a/5sim_api/5sim_api.csproj +++ b/5sim_api/5sim_api.csproj @@ -5,4 +5,9 @@ _5sim_api + + + + + diff --git a/5sim_api/Api.cs b/5sim_api/Api.cs new file mode 100644 index 0000000..001b243 --- /dev/null +++ b/5sim_api/Api.cs @@ -0,0 +1,286 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using RestSharp; +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; +using YoutubeBot._5sim.Exceptions; +using YoutubeBot._5sim.Objects; + +namespace YoutubeBot._5sim +{ + public class Api + { + public const string endpoint = "https://5sim.net/v1/"; + private const string referral_key = null; + + string token; + private RestClient client = new RestClient(endpoint); + + public Api(string token) + { + this.token = token; + } + + + public Service getService(string service_name, string country = "any", string carrier = "any") + { + String uri = $"guest/products/{country}/{carrier}"; + RestRequest request = new RestRequest(uri, Method.GET); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject services_json = (JObject)JsonConvert.DeserializeObject(response.Content); + + JObject service_json = (JObject)services_json[service_name]; + return service_json.ToObject(); + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + public Account getAccount() + { + String uri = "user/profile"; + RestRequest request = new RestRequest(uri, Method.GET); + + request.AddHeader("Authorization", $"Bearer {token}"); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject account_json = (JObject)JsonConvert.DeserializeObject(response.Content); + return account_json.ToObject(); + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + /// + /// Buy a activation-number + /// + public Number buyActivationNumber(string service_name, string country = "any", string carrier = "any") + { + return buyNumber("activation", service_name, country, carrier); + } + /// + /// Buy a hosting-number + /// + /// One of: {3hours, 1day} + public Number buyHostingNumber(string duration, string country = "any", string carrier = "any") + { + return buyNumber("hosting", duration, country, carrier); + } + public Number buyNumber(string type, string product, string country = "any", string provider = "any") + { + String uri = $"user/buy/{type}/{country}/{provider}/{product}"; + RestRequest request = new RestRequest(uri, Method.GET); + + // Add referral_key if any + if (!String.IsNullOrEmpty(referral_key)) request.AddQueryParameter("ref", referral_key); + + request.AddHeader("Authorization", $"Bearer {token}"); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject activationNumber_json = (JObject)JsonConvert.DeserializeObject(response.Content); + + Number activationNumber = activationNumber_json.ToObject(); + activationNumber.carrier = (string)activationNumber_json["operator"]; + return activationNumber; + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + + public Number checkNumber(int id) + { + String uri = $"user/check/{id}"; + RestRequest request = new RestRequest(uri, Method.GET); + + request.AddHeader("Authorization", $"Bearer {token}"); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject activationNumber_json = (JObject)JsonConvert.DeserializeObject(response.Content); + + Number activationNumber = activationNumber_json.ToObject(); + activationNumber.carrier = (string)activationNumber_json["operator"]; + return activationNumber; + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + public Number finishNumber(int id) + { + String uri = $"user/finish/{id}"; + RestRequest request = new RestRequest(uri, Method.GET); + + request.AddHeader("Authorization", $"Bearer {token}"); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject activationNumber_json = (JObject)JsonConvert.DeserializeObject(response.Content); + + Number activationNumber = activationNumber_json.ToObject(); + activationNumber.carrier = (string)activationNumber_json["operator"]; + return activationNumber; + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + public Number cancelNumber(int id) + { + String uri = $"user/cancel/{id}"; + RestRequest request = new RestRequest(uri, Method.GET); + + request.AddHeader("Authorization", $"Bearer {token}"); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject activationNumber_json = (JObject)JsonConvert.DeserializeObject(response.Content); + + Number activationNumber = activationNumber_json.ToObject(); + activationNumber.carrier = (string)activationNumber_json["operator"]; + return activationNumber; + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + public Number banNumber(int id) + { + String uri = $"user/ban/{id}"; + RestRequest request = new RestRequest(uri, Method.GET); + + request.AddHeader("Authorization", $"Bearer {token}"); + + IRestResponse response = client.Execute(request); + + // Client-error handling + Exception ex = response.ErrorException; + if (ex != null) throw ex; + + // Check for expected errors and throw the corresponding exception of necessary + checkAndThrowException(response.StatusCode, response.Content); + + if (response.StatusCode == HttpStatusCode.OK) + { + JObject activationNumber_json = (JObject)JsonConvert.DeserializeObject(response.Content); + + Number activationNumber = activationNumber_json.ToObject(); + activationNumber.carrier = (string)activationNumber_json["operator"]; + return activationNumber; + } + else + throw new InvalidOperationException($"Invalid response: {response.StatusCode}"); + } + + + + + // ---------- + + private static void checkAndThrowException(HttpStatusCode statusCode, string content) + { + switch (statusCode) + { + case HttpStatusCode.OK: + switch (content) + { + case "no free phones": + throw new NoFreePhonesException(); + } + break; + + case HttpStatusCode.BadRequest: + switch (content) + { + case "not enough product qty": + throw new NotEnoughProductQuantityException(); + + case "not enough user balance": + throw new NotEnoughUserBalanceException(); + + case "not enough rating": + throw new NotEnoughRatingException(); + } + break; + + case HttpStatusCode.NotFound: + switch (content) + { + case "order not found": + throw new OrderNotFoundException(); + + case "order expired": + throw new OrderExpiredException(); + + case "order has sms": + throw new OrderHasSmsException(); + + case "hosting order": + throw new HostingOrderException(); + } + break; + } + } + } +} diff --git a/5sim_api/Class1.cs b/5sim_api/Class1.cs deleted file mode 100644 index 7836e12..0000000 --- a/5sim_api/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace _5sim_api -{ - public class Class1 - { - } -} diff --git a/5sim_api/Exceptions/HostingOrderException.cs b/5sim_api/Exceptions/HostingOrderException.cs new file mode 100644 index 0000000..37ce801 --- /dev/null +++ b/5sim_api/Exceptions/HostingOrderException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class HostingOrderException : Exception + { + public HostingOrderException() + { + } + public HostingOrderException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/NoFreePhonesException.cs b/5sim_api/Exceptions/NoFreePhonesException.cs new file mode 100644 index 0000000..7c53b6f --- /dev/null +++ b/5sim_api/Exceptions/NoFreePhonesException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class NoFreePhonesException : Exception + { + public NoFreePhonesException() + { + } + public NoFreePhonesException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/NotEnoughProductQuantityException.cs b/5sim_api/Exceptions/NotEnoughProductQuantityException.cs new file mode 100644 index 0000000..99f73ce --- /dev/null +++ b/5sim_api/Exceptions/NotEnoughProductQuantityException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class NotEnoughProductQuantityException : Exception + { + public NotEnoughProductQuantityException() + { + } + public NotEnoughProductQuantityException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/NotEnoughRatingException.cs b/5sim_api/Exceptions/NotEnoughRatingException.cs new file mode 100644 index 0000000..5aa7714 --- /dev/null +++ b/5sim_api/Exceptions/NotEnoughRatingException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class NotEnoughRatingException : Exception + { + public NotEnoughRatingException() + { + } + public NotEnoughRatingException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/NotEnoughUserBalanceException.cs b/5sim_api/Exceptions/NotEnoughUserBalanceException.cs new file mode 100644 index 0000000..357de19 --- /dev/null +++ b/5sim_api/Exceptions/NotEnoughUserBalanceException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class NotEnoughUserBalanceException : Exception + { + public NotEnoughUserBalanceException() + { + } + public NotEnoughUserBalanceException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/OrderExpiredException.cs b/5sim_api/Exceptions/OrderExpiredException.cs new file mode 100644 index 0000000..5d95a9f --- /dev/null +++ b/5sim_api/Exceptions/OrderExpiredException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class OrderExpiredException : Exception + { + public OrderExpiredException() + { + } + public OrderExpiredException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/OrderHasSmsException.cs b/5sim_api/Exceptions/OrderHasSmsException.cs new file mode 100644 index 0000000..b4596da --- /dev/null +++ b/5sim_api/Exceptions/OrderHasSmsException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class OrderHasSmsException : Exception + { + public OrderHasSmsException() + { + } + public OrderHasSmsException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Exceptions/OrderNotFoundException.cs b/5sim_api/Exceptions/OrderNotFoundException.cs new file mode 100644 index 0000000..93bd838 --- /dev/null +++ b/5sim_api/Exceptions/OrderNotFoundException.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Exceptions +{ + [Serializable] + class OrderNotFoundException : Exception + { + public OrderNotFoundException() + { + } + public OrderNotFoundException(String message) + : base(message) + { + } + } +} diff --git a/5sim_api/Objects/Account.cs b/5sim_api/Objects/Account.cs new file mode 100644 index 0000000..b75b611 --- /dev/null +++ b/5sim_api/Objects/Account.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Objects +{ + public class Account + { + public int id; + public string email; + /// Funds in Rubel (RUB) + public float balance; + /// Total numbers of orders + public int rating; + + public Account() + { + } + } +} diff --git a/5sim_api/Objects/Number.cs b/5sim_api/Objects/Number.cs new file mode 100644 index 0000000..49ea46e --- /dev/null +++ b/5sim_api/Objects/Number.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Objects +{ + // Number from 5sim + public class Number + { + public int id; + + /// The phone-number + public string number; + + /// The recieved sms (Activation only has 1 sms) + public List sms; + + public DateTime created_at; + public DateTime expires; + + public bool forwarding; + public string forwarding_number; + + public string product; + public float price; + + public string carrier; + public string country; + + public string status; + + public Number() + { + } + } +} diff --git a/5sim_api/Objects/Service.cs b/5sim_api/Objects/Service.cs new file mode 100644 index 0000000..d393aec --- /dev/null +++ b/5sim_api/Objects/Service.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Objects +{ + /// + /// Service from 5sim + /// + public class Service + { + public String name; + public String category; + public int quantity; + /// Price in Rubel (RUB) + public float price; + + public Service() + { + } + public Service(String name, String category, int quantity, float price) + { + this.name = name; + this.category = category; + this.quantity = quantity; + this.price = price; + } + } +} diff --git a/5sim_api/Objects/Sms.cs b/5sim_api/Objects/Sms.cs new file mode 100644 index 0000000..33bb72e --- /dev/null +++ b/5sim_api/Objects/Sms.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace YoutubeBot._5sim.Objects +{ + /// + /// SMS received by a 5sim number + /// + public class Sms + { + public int id; + public DateTime created_at; + public DateTime date; + public string sender; + public string text; + public string code; + } +}