Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Newtonsoft.Json;
- using SGSDT.Mobile.Common.ServiceProxy;
- using System;
- using System.Collections.Generic;
- using System.Net;
- using System.Threading.Tasks;
- namespace SGSDT.Mobile.Common.Code.BaseApi
- {
- public class BaseApi
- {
- private readonly string _apiPage;
- private readonly IServiceProxy _proxy;
- protected static string Token { get; set; }
- public static bool IsAuthorized { get; set; }
- public BaseApi(string baseUri, string apiPage)
- : this(apiPage, (IServiceProxy) new SGSDT.Mobile.Common.ServiceProxy.ServiceProxy(new Uri(baseUri)))
- {
- }
- public BaseApi(string apiPage, IServiceProxy proxy)
- {
- this._apiPage = apiPage;
- this._proxy = proxy;
- }
- protected virtual async Task<T> Сall<T>(string methodName, Dictionary<string, string> parameters, RequestType type, bool checkIsAuthorized = false) where T : BaseEntity
- {
- if (checkIsAuthorized && !SGSDT.Mobile.Common.Code.BaseApi.BaseApi.IsAuthorized)
- throw new Exception("auth required");
- parameters["method"] = methodName;
- if (SGSDT.Mobile.Common.Code.BaseApi.BaseApi.IsAuthorized)
- parameters["guid"] = this.GetToken();
- string response;
- try
- {
- if (type == RequestType.Get)
- response = await this._proxy.GetRequestAsync(parameters, this._apiPage);
- else
- response = await this._proxy.PostRequestAsync(parameters, this._apiPage);
- }
- catch (WebException ex)
- {
- switch (ex.Status)
- {
- case WebExceptionStatus.ConnectFailure:
- throw new WebException("No access to the network");
- case WebExceptionStatus.RequestCanceled:
- throw new WebException("Slow internet connection.");
- default:
- throw new WebException(ex.Message);
- }
- }
- BaseResponse baseResponse;
- try
- {
- baseResponse = (BaseResponse) JsonConvert.DeserializeObject<BaseResponse>(response);
- }
- catch (Exception ex)
- {
- throw new Exception("Base response deserilation error");
- }
- string str = Convert.ToString(baseResponse.Data);
- if (baseResponse.Status == 1)
- {
- T obj;
- try
- {
- obj = (T) JsonConvert.DeserializeObject<T>(str);
- }
- catch (Exception ex)
- {
- throw new Exception("Data deserilation error");
- }
- return obj;
- }
- Error error;
- try
- {
- error = (Error) JsonConvert.DeserializeObject<Error>(str);
- }
- catch (Exception ex)
- {
- throw new Exception("Error deserilation error");
- }
- throw new Exception(error.Message);
- }
- public async Task<T> Сall<T>(string methodName, BaseEntity parameters, RequestType type = RequestType.Get, bool checkIsAuthorized = false) where T : BaseEntity
- {
- return await this.Сall<T>(methodName, parameters.ToDictionary(), type, checkIsAuthorized);
- }
- public async Task<T> Call<T>(BaseParameters parameters, bool checkIsAuthorized = false) where T : BaseEntity
- {
- return await this.Сall<T>(parameters.MethodName, (BaseEntity) parameters, parameters.Type, checkIsAuthorized);
- }
- public async Task Call(BaseParameters parameters, bool checkIsAuthorized = false)
- {
- BaseEntity baseEntity = await this.Сall<BaseEntity>(parameters.MethodName, (BaseEntity) parameters, parameters.Type, checkIsAuthorized);
- }
- protected string GetToken()
- {
- return SGSDT.Mobile.Common.Code.BaseApi.BaseApi.Token;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement