You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Ombi/src/Ombi.Api/Request.cs

121 lines
3.4 KiB

7 years ago
using System;
using System.Collections.Generic;
using System.Net;
7 years ago
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)
7 years ago
{
Endpoint = endpoint;
BaseUrl = baseUrl;
HttpMethod = http;
ContentType = contentType;
7 years ago
}
public ContentType ContentType { get; }
7 years ago
public string Endpoint { get; }
public string BaseUrl { get; }
public HttpMethod HttpMethod { get; }
public bool Retry { get; set; }
public List<HttpStatusCode> StatusCodeToRetry { get; set; } = new List<HttpStatusCode>();
public Action<string> OnBeforeDeserialization { get; set; }
7 years ago
private string FullUrl
{
get
{
var sb = new StringBuilder();
if (!string.IsNullOrEmpty(BaseUrl))
{
sb.Append(!BaseUrl.EndsWith("/") ? string.Format("{0}/", BaseUrl) : BaseUrl);
}
7 years ago
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>>();
7 years ago
public object JsonBody { get; private set; }
7 years ago
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));
}
7 years ago
public void ApplicationJsonContentType()
{
AddContentHeader("Content-Type", "application/json");
}
7 years ago
public void AddQueryString(string key, string value)
{
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
var builder = new UriBuilder(FullUri);
7 years ago
var startingTag = string.Empty;
var hasQuery = false;
if (string.IsNullOrEmpty(builder.Query))
{
startingTag = "?";
}
else
{
hasQuery = true;
startingTag = builder.Query.Contains("?") ? "&" : "?";
}
builder.Query = hasQuery
? $"{builder.Query}{startingTag}{key}={value}"
: $"{startingTag}{key}={value}";
7 years ago
_modified = builder.Uri;
}
7 years ago
public void AddJsonBody(object obj)
{
JsonBody = obj;
}
}
7 years ago
}