Http .dll moved in LaaProductionWeb
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
namespace LaaProduction.Http
|
||||
{
|
||||
using LaaProduction.Http.Interfaces;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Net.Http;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class HttpClient : IHttpClient
|
||||
{
|
||||
private readonly System.Net.Http.HttpClient httpClient;
|
||||
private readonly string routePrefix;
|
||||
|
||||
public HttpClient(string baseURI, string routePrefix)
|
||||
{
|
||||
var httpHandler = new HttpClientHandler
|
||||
{
|
||||
UseDefaultCredentials = true,
|
||||
};
|
||||
|
||||
this.httpClient = new System.Net.Http.HttpClient(httpHandler)
|
||||
{
|
||||
BaseAddress = new Uri(baseURI, UriKind.Absolute)
|
||||
};
|
||||
|
||||
this.routePrefix = routePrefix.TrimEnd('/');
|
||||
}
|
||||
|
||||
public static IHttpClient CreateHttpClient(string baseURI, string routePrefix)
|
||||
=> new HttpClient(baseURI, routePrefix);
|
||||
|
||||
public IHttpMessage Delete(string route)
|
||||
=> new HttpMessage(this, HttpMethod.Delete, $"{routePrefix}/{route.TrimStart('/')}");
|
||||
|
||||
public IHttpMessage Get(string route)
|
||||
=> new HttpMessage(this, HttpMethod.Get, $"{routePrefix}/{route.TrimStart('/')}");
|
||||
|
||||
public IHttpMessage Options(string route)
|
||||
=> new HttpMessage(this, HttpMethod.Options, $"{routePrefix}/{route.TrimStart('/')}");
|
||||
|
||||
public IHttpMessage Post(string route)
|
||||
=> new HttpMessage(this, HttpMethod.Post, $"{routePrefix}/{route.TrimStart('/')}");
|
||||
|
||||
public IHttpMessage Put(string route)
|
||||
=> new HttpMessage(this, HttpMethod.Put, $"{routePrefix}/{route.TrimStart('/')}");
|
||||
|
||||
internal async Task<IHttpResponse<T>> SendAsync<T>(HttpMessage fluentHttpRequestMessage)
|
||||
{
|
||||
var httpResult = new HttpResponse<T>();
|
||||
|
||||
using (fluentHttpRequestMessage)
|
||||
{
|
||||
using (var httpResponseMessage = await this.httpClient.SendAsync(fluentHttpRequestMessage))
|
||||
{
|
||||
if (httpResponseMessage.Content is HttpContent httpContent)
|
||||
{
|
||||
var contentString = await httpContent.ReadAsStringAsync();
|
||||
|
||||
try
|
||||
{
|
||||
if (httpResponseMessage.IsSuccessStatusCode)
|
||||
{
|
||||
httpResult.Value = JsonConvert.DeserializeObject<T>(contentString);
|
||||
httpResult.Succeeded = httpResponseMessage.IsSuccessStatusCode && httpResult != null;
|
||||
}
|
||||
else
|
||||
{
|
||||
httpResult.Errors = JsonConvert.DeserializeObject<IDictionary<string, string>>(contentString);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
httpResult.Succeeded = false;
|
||||
httpResult.Message = JsonConvert.SerializeObject(new
|
||||
{
|
||||
HttpContent = contentString,
|
||||
Exception = e.ToString(),
|
||||
InnerException = e.InnerException?.ToString()
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return httpResult;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
namespace LaaProduction.Http
|
||||
{
|
||||
using LaaProduction.Http.Interfaces;
|
||||
|
||||
using Newtonsoft.Json;
|
||||
|
||||
using System.Net.Http;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
internal class HttpMessage : IHttpMessage
|
||||
{
|
||||
private const string Bearer = nameof(Bearer);
|
||||
|
||||
private readonly HttpClient fluentHttpClient;
|
||||
private readonly HttpRequestMessage httpRequestMessage;
|
||||
|
||||
public HttpMessage(HttpClient fluentHttpClient, HttpMethod method, string route)
|
||||
{
|
||||
this.fluentHttpClient = fluentHttpClient;
|
||||
this.httpRequestMessage = new HttpRequestMessage(method, route);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
=> this.httpRequestMessage?.Dispose();
|
||||
|
||||
public IHttpMessage AuthorizeBearer(string parameter)
|
||||
{
|
||||
this.httpRequestMessage.Headers.Authorization = new AuthenticationHeaderValue(Bearer, parameter);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public Task<IHttpResponse<T>> SendAsync<T>()
|
||||
=> this.fluentHttpClient.SendAsync<T>(this);
|
||||
|
||||
public IHttpMessage WhitJsonBody(object model)
|
||||
{
|
||||
var content = JsonConvert.SerializeObject(model);
|
||||
|
||||
this.httpRequestMessage.Content = new StringContent(content, Encoding.UTF8, "application/json");
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public static implicit operator HttpRequestMessage(HttpMessage message)
|
||||
=> message?.httpRequestMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace LaaProduction.Http.Interfaces
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class HttpResponse<T> : IHttpResponse<T>
|
||||
{
|
||||
public bool Succeeded { get; internal set; }
|
||||
|
||||
public T Value { get; internal set; }
|
||||
|
||||
public IDictionary<string, string> Errors { get; internal set; }
|
||||
= new Dictionary<string, string>();
|
||||
|
||||
public string Message { get; internal set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
namespace LaaProduction.Http.Interfaces
|
||||
{
|
||||
public interface IHttpClient
|
||||
{
|
||||
IHttpMessage Delete(string route);
|
||||
|
||||
IHttpMessage Get(string route);
|
||||
|
||||
IHttpMessage Options(string route);
|
||||
|
||||
IHttpMessage Post(string url);
|
||||
|
||||
IHttpMessage Put(string url);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
namespace LaaProduction.Http.Interfaces
|
||||
{
|
||||
using System.Collections.Generic;
|
||||
|
||||
public interface IHttpErrorResponse
|
||||
{
|
||||
IDictionary<string, string> Errors { get; }
|
||||
|
||||
string Message { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace LaaProduction.Http.Interfaces
|
||||
{
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public interface IHttpMessage : IDisposable
|
||||
{
|
||||
IHttpMessage AuthorizeBearer(string parameter);
|
||||
|
||||
Task<IHttpResponse<T>> SendAsync<T>();
|
||||
|
||||
IHttpMessage WhitJsonBody(object model);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace LaaProduction.Http.Interfaces
|
||||
{
|
||||
public interface IHttpResponse<T> : IHttpErrorResponse
|
||||
{
|
||||
bool Succeeded { get; }
|
||||
|
||||
T Value { get; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
|
||||
<PropertyGroup>
|
||||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
|
||||
<ProjectGuid>{197185D3-53B7-41A1-A099-CE9F85D5C8F9}</ProjectGuid>
|
||||
<OutputType>Library</OutputType>
|
||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||
<RootNamespace>LaaProduction.Http</RootNamespace>
|
||||
<AssemblyName>LaaProduction.Http</AssemblyName>
|
||||
<TargetFrameworkVersion>v4.8.1</TargetFrameworkVersion>
|
||||
<FileAlignment>512</FileAlignment>
|
||||
<Deterministic>true</Deterministic>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
|
||||
<DebugSymbols>true</DebugSymbols>
|
||||
<DebugType>full</DebugType>
|
||||
<Optimize>false</Optimize>
|
||||
<OutputPath>bin\Debug\</OutputPath>
|
||||
<DefineConstants>DEBUG;TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
|
||||
<DebugType>pdbonly</DebugType>
|
||||
<Optimize>true</Optimize>
|
||||
<OutputPath>bin\Release\</OutputPath>
|
||||
<DefineConstants>TRACE</DefineConstants>
|
||||
<ErrorReport>prompt</ErrorReport>
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Net.Http" />
|
||||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="HttpClient.cs" />
|
||||
<Compile Include="HttpMessage.cs" />
|
||||
<Compile Include="HttpResponse[T].cs" />
|
||||
<Compile Include="Interfaces\IHttpClient.cs" />
|
||||
<Compile Include="Interfaces\IHttpErrorResponse.cs" />
|
||||
<Compile Include="Interfaces\IHttpMessage.cs" />
|
||||
<Compile Include="Interfaces\IHttpResponse[T].cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="packages.config" />
|
||||
<None Include="README.md" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
// General Information about an assembly is controlled through the following
|
||||
// set of attributes. Change these attribute values to modify the information
|
||||
// associated with an assembly.
|
||||
[assembly: AssemblyTitle("LaaProduction.Http")]
|
||||
[assembly: AssemblyDescription("")]
|
||||
[assembly: AssemblyConfiguration("")]
|
||||
[assembly: AssemblyCompany("")]
|
||||
[assembly: AssemblyProduct("LaaProduction.Http")]
|
||||
[assembly: AssemblyCopyright("Copyright © 2024")]
|
||||
[assembly: AssemblyTrademark("")]
|
||||
[assembly: AssemblyCulture("")]
|
||||
|
||||
// Setting ComVisible to false makes the types in this assembly not visible
|
||||
// to COM components. If you need to access a type in this assembly from
|
||||
// COM, set the ComVisible attribute to true on that type.
|
||||
[assembly: ComVisible(false)]
|
||||
|
||||
// The following GUID is for the ID of the typelib if this project is exposed to COM
|
||||
[assembly: Guid("197185d3-53b7-41a1-a099-ce9f85d5c8f9")]
|
||||
|
||||
// Version information for an assembly consists of the following four values:
|
||||
//
|
||||
// Major Version
|
||||
// Minor Version
|
||||
// Build Number
|
||||
// Revision
|
||||
//
|
||||
// You can specify all the values or you can default the Build and Revision Numbers
|
||||
// by using the '*' as shown below:
|
||||
// [assembly: AssemblyVersion("1.0.*")]
|
||||
[assembly: AssemblyVersion("1.0.0.0")]
|
||||
[assembly: AssemblyFileVersion("1.0.0.0")]
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="Newtonsoft.Json" version="13.0.3" targetFramework="net481" />
|
||||
</packages>
|
||||
Reference in New Issue
Block a user