-
-
Notifications
You must be signed in to change notification settings - Fork 401
/
Request.cs
105 lines (89 loc) · 3.01 KB
/
Request.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
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Text;
namespace Ombi.Api
{
public class Request
{
public Request()
{
}
public Request(string endpoint, string baseUrl, HttpMethod http, ContentType contentType = ContentType.Json)
{
Endpoint = endpoint;
BaseUrl = baseUrl;
HttpMethod = http;
ContentType = contentType;
}
public ContentType ContentType { get; }
public string Endpoint { get; }
public string BaseUrl { get; }
public HttpMethod HttpMethod { get; }
public bool IgnoreErrors { get; set; }
public bool Retry { get; set; }
public List<HttpStatusCode> StatusCodeToRetry { get; set; } = new List<HttpStatusCode>();
public bool IgnoreBaseUrlAppend { get; set; }
public Action<string> OnBeforeDeserialization { get; set; }
private string FullUrl
{
get
{
var sb = new StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl))
{
sb.Append(!BaseUrl.EndsWith("/") && !IgnoreBaseUrlAppend ? string.Format("{0}/", BaseUrl) : BaseUrl);
}
sb.Append(Endpoint.StartsWith("/") ? Endpoint.Remove(0, 1) : Endpoint);
return sb.ToString();
}
}
private Uri _modified;
public Uri FullUri
{
get => _modified != null ? _modified : new Uri(FullUrl);
set => _modified = value;
}
public List<KeyValuePair<string, string>> Headers { get; } = new List<KeyValuePair<string, string>>();
public List<KeyValuePair<string, string>> ContentHeaders { get; } = new List<KeyValuePair<string, string>>();
public object JsonBody { get; private set; }
public bool IsValidUrl
{
get
{
try
{
// ReSharper disable once ObjectCreationAsStatement
new Uri(FullUrl);
return true;
}
catch (Exception)
{
return false;
}
}
}
public void AddHeader(string key, string value)
{
Headers.Add(new KeyValuePair<string, string>(key, value));
}
public void AddContentHeader(string key, string value)
{
ContentHeaders.Add(new KeyValuePair<string, string>(key, value));
}
public void ApplicationJsonContentType()
{
AddContentHeader("Content-Type", "application/json");
}
public void AddQueryString(string key, string value)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
_modified = FullUri.AddQueryParameter(key, value);
}
public void AddJsonBody(object obj)
{
JsonBody = obj;
}
}
}