-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseClient.cs
103 lines (93 loc) · 3.56 KB
/
BaseClient.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
using RestSharp;
using RestSharp.Deserializers;
using System;
using System.Linq;
namespace LastFM.ReaderCore
{
public class BaseClient : RestSharp.RestClient
{
protected ICacheService _cache;
protected IErrorLogger _errorLogger;
public BaseClient(ICacheService cache, IDeserializer serializer, IErrorLogger errorLogger, string baseUrl)
{
_cache = cache;
_errorLogger = errorLogger;
AddHandler("application/json", () => { return serializer; });
AddHandler("text/json", () => { return serializer; });
AddHandler("text/x-json", () => { return serializer; });
BaseUrl = new Uri(baseUrl);
}
private void LogError(Uri BaseUrl, IRestRequest request, IRestResponse response)
{
//Get the values of the parameters passed to the API
string parameters = string.Join(", ", request.Parameters.Select(x => x.Name.ToString() + "=" + ((x.Value == null) ? "NULL" : x.Value)).ToArray());
//Set up the information message with the URL, the status code, and the parameters.
string info = "Request to " + BaseUrl.AbsoluteUri + request.Resource + " failed with status code " + response.StatusCode + ", parameters: "
+ parameters + ", and content: " + response.Content;
//Acquire the actual exception
Exception ex;
if (response != null && response.ErrorException != null)
{
ex = response.ErrorException;
}
else
{
ex = new Exception(info);
info = string.Empty;
}
//Log the exception and info message
_errorLogger.LogError(ex,info);
}
private void TimeoutCheck(IRestRequest request, IRestResponse response)
{
if (response.StatusCode == 0)
{
LogError(BaseUrl, request, response);
}
}
public override IRestResponse Execute(IRestRequest request)
{
var response = base.Execute(request);
TimeoutCheck(request, response);
return response;
}
public override IRestResponse<T> Execute<T>(IRestRequest request)
{
var response = base.Execute<T>(request);
TimeoutCheck(request, response);
return response;
}
public T Get<T>(IRestRequest request) where T : new()
{
var response = Execute<T>(request);
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
return response.Data;
}
else
{
LogError(BaseUrl,request,response);
return default(T);
}
}
public T GetFromCache<T>(IRestRequest request, string cacheKey) where T : class, new()
{
var item = _cache.Get<T>(cacheKey);
if (item == null) //If the cache doesn't have the item
{
var response = Execute<T>(request); //Get the item from the API call
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
_cache.Set(cacheKey, response.Data); //Set that item into the cache so we can get it next time
item = response.Data;
}
else
{
LogError(BaseUrl, request, response);
return default(T);
}
}
return item;
}
}
}