Goffo.Http
3.0.0
See the version list below for details.
dotnet add package Goffo.Http --version 3.0.0
NuGet\Install-Package Goffo.Http -Version 3.0.0
<PackageReference Include="Goffo.Http" Version="3.0.0" />
<PackageVersion Include="Goffo.Http" Version="3.0.0" />
<PackageReference Include="Goffo.Http" />
paket add Goffo.Http --version 3.0.0
#r "nuget: Goffo.Http, 3.0.0"
#:package Goffo.Http@3.0.0
#addin nuget:?package=Goffo.Http&version=3.0.0
#tool nuget:?package=Goffo.Http&version=3.0.0
READ ME
Goffo.Http
Library that helps with generating urls using HttpClient
How to use
- ApiOutput Methods
- IApiResponse Methods
Example HTTP Methods using IApiResponse
Create an API Class injecting the HttpClient
Use the CRUD extension Methods for the HttpClient provided which you will pass Request and Response object
- ReadAsync
- CreateAsync
- UpdateAsync
- DeleteAsync
These methods should never throw an Exception.
Instead it catches Exceptions in the ApiResponseException class which you can check and then throw if desired.You can provide any object for the TErrorResponse of the IApiResponse.
If you know the Http Content format for non-successfully response you can pass that object Otherwise put in as string and it will return that json as a string (as shown below)
-- Dependency Injection Setup Example
IServiceCollection services = new ServiceCollection;
services.AddHttpClient("MyApiClient", client => client.BaseAddress = new Uri("https://www.MyWebApiSite.com/api/"))
.AddTypedClient<IEmployeeApi, EmployeeApi>();
services.RegisterGoffoHttp();
-- ============================================================================================================================================================
-- Api Class Example
public class EmployeeApi : IEmployeeApi
{
private readonly HttpClient _httpClient;
public EmployeeApi(HttpClient httpClient)
{
_httpClient = httpClient;
}
public async Task<IApiResponse<EmployeeResponse, string>> GetEmployeeByIdAsync(EmployeeRequest request, CancellationToken cancellationToken = default)
{
string endpoint = "employees";
string url = @$"{_httpClient.BaseAddress}{endpoint}/{request.Id}";
var response = await _httpClient.ReadAsync<EmployeeRequest, EmployeeResponse>(request, endpoint, url, cancellationToken);
return response;
}
public async Task<IApiResponse<EmployeeResponses, string>> GetEmployeesAsync(CancellationToken cancellationToken = default)
{
string endpoint = "employees";
string url = @$"{_httpClient.BaseAddress}{endpoint}";
var response = await _httpClient.ReadAsync<EmployeesRequest, EmployeeResponses>(new EmployeesRequest(), endpoint, url, cancellationToken);
return response;
}
public async Task<IApiResponse<CreateEmployeeResponse, string>> CreateEmployeeAsync(CreateEmployeeRequest request, CancellationToken cancellationToken = default)
{
string endpoint = "employees";
string url = @$"{_httpClient.BaseAddress}{endpoint}";
var response = await _httpClient.CreateAsync<CreateEmployeeRequest, CreateEmployeeResponse>(request, endpoint, url, cancellationToken);
return response;
}
public async Task<IApiResponse<UpdateEmployeeResponse, string>> UpdateEmployeeAsync(UpdateEmployeeRequest request, CancellationToken cancellationToken = default)
{
string endpoint = "employees";
string url = @$"{_httpClient.BaseAddress}{endpoint}";
var response = await _httpClient.UpdateAsync<UpdateEmployeeRequest, UpdateEmployeeResponse>(request, endpoint, url, cancellationToken);
return response;
}
public async Task<IApiResponse<DeleteEmployeeResponse, string>> DeleteEmployeeAsync(DeleteEmployeeRequest request, CancellationToken cancellationToken = default)
{
string endpoint = "employees";
string url = @$"{_httpClient.BaseAddress}{endpoint}/{request.Id}";
var response = await _httpClient.DeleteAsync<DeleteEmployeeRequest, DeleteEmployeeResponse>(request, endpoint, url, cancellationToken);
return response;
}
}
-- ======================================================================================================================================================================
-- Console App Example
App app = new();
Console.WriteLine("App is running...");
Console.WriteLine();
var request = new EmployeeRequest(Id: 1);
var response = await app.EmployeeApi.GetEmployeeByIdAsync(request);
if (response.ApiResponseException is not null)
{
Console.WriteLine($"An error occured: {response.ApiResponseException.Message}");
}
if (response.IsSuccessStatusCode)
{
Console.WriteLine($"Found Employee: {response.Response!.FirstName} {response.Response.LastName}");
}
else if(response.HttpStatusCode == System.Net.HttpStatusCode.NotFound)
{
Console.WriteLine($"Employee with Id {request.Id} returned {response.ReasonPhrase}");
}
else
{
Console.WriteLine($"Unexpected Error: {response.HttpStatusCodeDescription}");
}
Console.WriteLine();
Console.ReadLine();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.AspNetCore.WebUtilities (>= 10.0.0)
- Microsoft.Extensions.Http (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Created methods for the new IApiResponse and starting to phase out old methods