mirror of
https://github.com/Ombi-app/Ombi.git
synced 2025-08-20 05:13:18 -07:00
The move!
This commit is contained in:
parent
1daf480b1b
commit
25526cc4d9
1147 changed files with 85 additions and 8524 deletions
65
src/Ombi.Api/Api.cs
Normal file
65
src/Ombi.Api/Api.cs
Normal file
|
@ -0,0 +1,65 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
using System.Xml.Serialization;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public class Api
|
||||
{
|
||||
private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
|
||||
{
|
||||
NullValueHandling = NullValueHandling.Ignore
|
||||
};
|
||||
|
||||
public async Task<T> Request<T>(Request request)
|
||||
{
|
||||
using (var httpClient = new HttpClient())
|
||||
{
|
||||
using (var httpRequestMessage = new HttpRequestMessage(request.HttpMethod, request.FullUri))
|
||||
{
|
||||
// Add the Json Body
|
||||
if (request.JsonBody != null)
|
||||
{
|
||||
httpRequestMessage.Content = new JsonContent(request.JsonBody);
|
||||
}
|
||||
|
||||
// Add headers
|
||||
foreach (var header in request.Headers)
|
||||
{
|
||||
httpRequestMessage.Headers.Add(header.Key, header.Value);
|
||||
|
||||
}
|
||||
using (var httpResponseMessage = await httpClient.SendAsync(httpRequestMessage))
|
||||
{
|
||||
if (!httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
// Logging
|
||||
}
|
||||
// do something with the response
|
||||
var data = httpResponseMessage.Content;
|
||||
|
||||
|
||||
var receivedString = await data.ReadAsStringAsync();
|
||||
if (request.ContentType == ContentType.Json)
|
||||
{
|
||||
return JsonConvert.DeserializeObject<T>(receivedString, Settings);
|
||||
}
|
||||
else
|
||||
{
|
||||
// XML
|
||||
XmlSerializer serializer = new XmlSerializer(typeof(T));
|
||||
StringReader reader = new StringReader(receivedString);
|
||||
var value = (T)serializer.Deserialize(reader);
|
||||
return value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
75
src/Ombi.Api/ApiHelper.cs
Normal file
75
src/Ombi.Api/ApiHelper.cs
Normal file
|
@ -0,0 +1,75 @@
|
|||
#region Copyright
|
||||
// /************************************************************************
|
||||
// Copyright (c) 2017 Jamie Rees
|
||||
// File: ApiHelper.cs
|
||||
// Created By: Jamie Rees
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining
|
||||
// a copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to
|
||||
// permit persons to whom the Software is furnished to do so, subject to
|
||||
// the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
// ************************************************************************/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
public static class ApiHelper
|
||||
{
|
||||
public static Uri ChangePath(this Uri uri, string path, params string[] args)
|
||||
{
|
||||
var builder = new UriBuilder(uri);
|
||||
|
||||
if (args != null && args.Length > 0)
|
||||
{
|
||||
builder.Path = builder.Path + string.Format(path, args);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Path += path;
|
||||
}
|
||||
return builder.Uri;
|
||||
}
|
||||
public static Uri ChangePath(this Uri uri, string path)
|
||||
{
|
||||
return ChangePath(uri, path, null);
|
||||
}
|
||||
|
||||
public static Uri AddQueryParameter(this Uri uri, string parameter, string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(parameter) || string.IsNullOrEmpty(value)) return uri;
|
||||
var builder = new UriBuilder(uri);
|
||||
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}{parameter}={value}"
|
||||
: $"{startingTag}{parameter}={value}";
|
||||
return builder.Uri;
|
||||
}
|
||||
}
|
||||
}
|
10
src/Ombi.Api/ContentType.cs
Normal file
10
src/Ombi.Api/ContentType.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
namespace Ombi.Api
|
||||
{
|
||||
public enum ContentType
|
||||
{
|
||||
Json,
|
||||
Xml,
|
||||
Text,
|
||||
Html,
|
||||
}
|
||||
}
|
13
src/Ombi.Api/JsonContent.cs
Normal file
13
src/Ombi.Api/JsonContent.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Ombi.Api
|
||||
{
|
||||
internal class JsonContent : StringContent
|
||||
{
|
||||
public JsonContent(object obj) :
|
||||
base(JsonConvert.SerializeObject(obj), Encoding.UTF8, "application/json")
|
||||
{ }
|
||||
}
|
||||
}
|
13
src/Ombi.Api/Ombi.Api.csproj
Normal file
13
src/Ombi.Api/Ombi.Api.csproj
Normal file
|
@ -0,0 +1,13 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard1.6</TargetFramework>
|
||||
<!--<NetStandardImplicitPackageVersion>1.6.1</NetStandardImplicitPackageVersion>-->
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
|
||||
<PackageReference Include="System.Xml.XmlSerializer" Version="4.3.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
110
src/Ombi.Api/Request.cs
Normal file
110
src/Ombi.Api/Request.cs
Normal file
|
@ -0,0 +1,110 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
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; }
|
||||
|
||||
private string FullUrl
|
||||
{
|
||||
get
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
if (!string.IsNullOrEmpty(BaseUrl))
|
||||
{
|
||||
sb.Append(!BaseUrl.EndsWith("/") ? 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; 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 AddQueryString(string key, string value)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
|
||||
|
||||
var builder = new UriBuilder(FullUri);
|
||||
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}";
|
||||
_modified = builder.Uri;
|
||||
}
|
||||
|
||||
public void AddJsonBody(object obj)
|
||||
{
|
||||
JsonBody = obj;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue