Advertisement
Guest User

Untitled

a guest
May 20th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.17 KB | None | 0 0
  1. private T SerializeDeserialize<T>(object data, Type typeToRequest, Type typeToReturn, string apiMethod)
  2.         {
  3.             object classToUse;
  4.             string serializedJson = null;
  5.             HttpResponseMessage result;
  6.  
  7.             if (data != null)
  8.             {
  9.                 serializedJson = JsonConvert.SerializeObject(data, typeToRequest, st);
  10.                 result = HttpProcessing.PostHTTPRequest(new Dictionary<string, string>(), apiURL + apiMethod, serializedJson).Result;
  11.             }
  12.             else
  13.                 result = HttpProcessing.GetHTTPRequeset(new Dictionary<string, string>(), apiURL + apiMethod).Result;
  14.  
  15.             if (result.StatusCode == System.Net.HttpStatusCode.OK)
  16.                 classToUse = JsonConvert.DeserializeObject(result.Content.ToString(), typeToReturn);
  17.             else if (result.StatusCode == System.Net.HttpStatusCode.BadRequest)
  18.                 classToUse = JsonConvert.DeserializeObject(result.Content.ToString(), typeof(Error));
  19.             else
  20.                 throw new Exception("Error: Status Code:" + result.StatusCode + " Status Error:" + result.Content.ToString());
  21.  
  22.             return (T)classToUse;
  23.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement