Advertisement
mcgrab

BaseApi

Dec 11th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. using Newtonsoft.Json;
  2. using SGSDT.Mobile.Common.ServiceProxy;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Net;
  6. using System.Threading.Tasks;
  7.  
  8. namespace SGSDT.Mobile.Common.Code.BaseApi
  9. {
  10.   public class BaseApi
  11.   {
  12.     private readonly string _apiPage;
  13.     private readonly IServiceProxy _proxy;
  14.  
  15.     protected static string Token { get; set; }
  16.  
  17.     public static bool IsAuthorized { get; set; }
  18.  
  19.     public BaseApi(string baseUri, string apiPage)
  20.       : this(apiPage, (IServiceProxy) new SGSDT.Mobile.Common.ServiceProxy.ServiceProxy(new Uri(baseUri)))
  21.     {
  22.     }
  23.  
  24.     public BaseApi(string apiPage, IServiceProxy proxy)
  25.     {
  26.       this._apiPage = apiPage;
  27.       this._proxy = proxy;
  28.     }
  29.  
  30.     protected virtual async Task<T> Сall<T>(string methodName, Dictionary<string, string> parameters, RequestType type, bool checkIsAuthorized = false) where T : BaseEntity
  31.     {
  32.       if (checkIsAuthorized && !SGSDT.Mobile.Common.Code.BaseApi.BaseApi.IsAuthorized)
  33.         throw new Exception("auth required");
  34.       parameters["method"] = methodName;
  35.       if (SGSDT.Mobile.Common.Code.BaseApi.BaseApi.IsAuthorized)
  36.         parameters["guid"] = this.GetToken();
  37.       string response;
  38.       try
  39.       {
  40.         if (type == RequestType.Get)
  41.           response = await this._proxy.GetRequestAsync(parameters, this._apiPage);
  42.         else
  43.           response = await this._proxy.PostRequestAsync(parameters, this._apiPage);
  44.       }
  45.       catch (WebException ex)
  46.       {
  47.         switch (ex.Status)
  48.         {
  49.           case WebExceptionStatus.ConnectFailure:
  50.             throw new WebException("No access to the network");
  51.           case WebExceptionStatus.RequestCanceled:
  52.             throw new WebException("Slow internet connection.");
  53.           default:
  54.             throw new WebException(ex.Message);
  55.         }
  56.       }
  57.       BaseResponse baseResponse;
  58.       try
  59.       {
  60.         baseResponse = (BaseResponse) JsonConvert.DeserializeObject<BaseResponse>(response);
  61.       }
  62.       catch (Exception ex)
  63.       {
  64.         throw new Exception("Base response deserilation error");
  65.       }
  66.       string str = Convert.ToString(baseResponse.Data);
  67.       if (baseResponse.Status == 1)
  68.       {
  69.         T obj;
  70.         try
  71.         {
  72.           obj = (T) JsonConvert.DeserializeObject<T>(str);
  73.         }
  74.         catch (Exception ex)
  75.         {
  76.           throw new Exception("Data deserilation error");
  77.         }
  78.         return obj;
  79.       }
  80.       Error error;
  81.       try
  82.       {
  83.         error = (Error) JsonConvert.DeserializeObject<Error>(str);
  84.       }
  85.       catch (Exception ex)
  86.       {
  87.         throw new Exception("Error deserilation error");
  88.       }
  89.       throw new Exception(error.Message);
  90.     }
  91.  
  92.     public async Task<T> Сall<T>(string methodName, BaseEntity parameters, RequestType type = RequestType.Get, bool checkIsAuthorized = false) where T : BaseEntity
  93.     {
  94.       return await this.Сall<T>(methodName, parameters.ToDictionary(), type, checkIsAuthorized);
  95.     }
  96.  
  97.     public async Task<T> Call<T>(BaseParameters parameters, bool checkIsAuthorized = false) where T : BaseEntity
  98.     {
  99.       return await this.Сall<T>(parameters.MethodName, (BaseEntity) parameters, parameters.Type, checkIsAuthorized);
  100.     }
  101.  
  102.     public async Task Call(BaseParameters parameters, bool checkIsAuthorized = false)
  103.     {
  104.       BaseEntity baseEntity = await this.Сall<BaseEntity>(parameters.MethodName, (BaseEntity) parameters, parameters.Type, checkIsAuthorized);
  105.     }
  106.  
  107.     protected string GetToken()
  108.     {
  109.       return SGSDT.Mobile.Common.Code.BaseApi.BaseApi.Token;
  110.     }
  111.   }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement