-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathHelper.cs
116 lines (97 loc) · 4.36 KB
/
Helper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.ServiceModel;
using Newtonsoft.Json.Linq;
using RestSharp;
using RestSharp.Serialization.Json;
namespace RestSharpDemo.Utilities
{
public static class Helper
{
private static BasicHttpBinding _basicHttpBinding;
private static IRestRequest _restRequest;
private static IRestResponse _restResponse;
public static Dictionary<string, string> DeserializeResponse(this IRestResponse restResponse)
{
var jsonObj = new JsonDeserializer().Deserialize<Dictionary<string, string>>(restResponse);
return jsonObj;
}
public static string DeserializeResponseUsingJObject(this IRestResponse restResponse, string responseObj)
{
var jObject = JObject.Parse(restResponse.Content);
return jObject[responseObj]?.ToString();
}
public static bool IsSuccessStatusCode(this HttpStatusCode responseCode)
{
var numericResponse = (int) responseCode;
const int statusCodeOk = (int) HttpStatusCode.OK;
const int statusCodeBadRequest = (int) HttpStatusCode.BadRequest;
return numericResponse >= statusCodeOk &&
numericResponse < statusCodeBadRequest;
}
//You could even wrap the API calls as helper methods as below. Use it as extension methods at your tess.
//Experimental.
//CreateGetRequest method here accepts an optional String, that means,
//you could send an endpoint as string given the RestClient takes just the baseUrl otherwise considered null
public static IRestRequest CreateGetRequest(string optionalEndPoint = null)
{
_restRequest = new RestRequest(optionalEndPoint, Method.GET);
_restRequest.AddHeader("Accept", "application/json");
return _restRequest;
}
public static IRestRequest CreatePostRequest(string jsonString)
{
_restRequest = new RestRequest(Method.POST);
_restRequest.AddHeader("Accept", "application/json");
_restRequest.AddParameter("application/json", jsonString, ParameterType.RequestBody);
return _restRequest;
}
public static IRestRequest CreatePutRequest(string jsonString)
{
_restRequest = new RestRequest(Method.PUT);
_restRequest.AddHeader("Accept", "application/json");
_restRequest.AddParameter("application/json", jsonString, ParameterType.RequestBody);
return _restRequest;
}
public static IRestRequest CreatePatchRequest(string jsonString)
{
_restRequest = new RestRequest(Method.PATCH);
_restRequest.AddHeader("Accept", "application/json");
_restRequest.AddParameter("application/json", jsonString, ParameterType.RequestBody);
return _restRequest;
}
public static IRestRequest CreateDeleteRequest()
{
_restRequest = new RestRequest(Method.DELETE);
_restRequest.AddHeader("Accept", "application/json");
return _restRequest;
}
public static IRestResponse GetResponse(IRestClient restClient, IRestRequest request)
{
return restClient.Execute(request);
}
public static BasicHttpBinding GetHttpBinding()
{
_basicHttpBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
_basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;
return _basicHttpBinding;
}
public static async Task<IRestResponse<T>> ExecuteAsyncRequest<T>(this RestClient client, IRestRequest request)
where T : class, new()
{
var taskCompletionSource = new TaskCompletionSource<IRestResponse<T>>();
client.ExecuteAsync<T>(request, restResponse =>
{
if (restResponse.ErrorException != null)
{
const string message = "Error retrieving response.";
throw new ApplicationException(message, restResponse.ErrorException);
}
taskCompletionSource.SetResult(restResponse);
});
return await taskCompletionSource.Task;
}
}
}