Goffo.Http
1.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package Goffo.Http --version 1.0.0
NuGet\Install-Package Goffo.Http -Version 1.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Goffo.Http" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Goffo.Http" Version="1.0.0" />
<PackageReference Include="Goffo.Http" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Goffo.Http --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Goffo.Http, 1.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Goffo.Http@1.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Goffo.Http&version=1.0.0
#tool nuget:?package=Goffo.Http&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
READ ME
Goffo.Http
Library that helps with generating urls using HttpClient
How to use
- Create a class for your Web Api Service and inherit from AbstractWebApiService
- If using Interface have it inherit from IWebApiService else have the created class inherit
- Use the WebApiUriService to help generate your uri string (optional)
- Use the AbstractWebApiService Endpoint methods (optional)
Example Web Api Service Class
using Goffo.Http.Base;
using Goffo.Http.Library.Models;
using Goffo.Http.Services;
using System.Net.Http.Json;
namespace Goffo.Http.Console.WebApiServices;
public class EmployeeWebApiService : AbstractWebApiService, IEmployeeWebApiService
{
public EmployeeWebApiService(IHttpClientFactory httpClientFactory) : base(httpClientFactory)
{
EmployeeHttpClient = httpClientFactory.CreateClient("EmployeeHttpClient");
Endpoint = "Employees";
}
internal HttpClient EmployeeHttpClient { get; init; }
public async Task<IEnumerable<EmployeeModel>> GetEmployeesAsync()
{
Uri = WebApiUriService.GetApiUri(EmployeeHttpClient, Endpoint);
var output = await GetAsync<EmployeeModel>(EmployeeHttpClient, Endpoint, Uri);
if (output is null)
{
throw GetApiNullOutputException();
}
return output;
}
public async Task<EmployeeModel> GetEmployeeByIdAsync(int id)
{
Uri = WebApiUriService.GetApiUri(EmployeeHttpClient, Endpoint, id.ToString());
var output = await GetByIdAsync<EmployeeModel>(EmployeeHttpClient, Endpoint, Uri);
if (output is null)
{
throw GetApiNullOutputException();
}
return output;
}
public async Task<EmployeeModel> CreateEmployeeAsync(EmployeeModel employee)
{
Uri = WebApiUriService.GetApiUri(EmployeeHttpClient, Endpoint);
HttpContent content = await PostAsync(EmployeeHttpClient, Endpoint, Uri, employee);
EmployeeModel? output = await content.ReadFromJsonAsync<EmployeeModel>();
if (output is null)
{
throw GetApiNullOutputException();
}
return output;
}
public async Task<EmployeeModel> UpdateEmployeeAsync(EmployeeModel employee)
{
Uri = WebApiUriService.GetApiUri(EmployeeHttpClient, Endpoint, employee.Id.ToString());
HttpContent content = await PutAsync(EmployeeHttpClient, Endpoint, Uri, employee);
EmployeeModel? output = await content.ReadFromJsonAsync<EmployeeModel>();
if (output is null)
{
throw GetApiNullOutputException();
}
return output;
}
public async Task<EmployeeModel> DeleteEmployeeAsync(int id)
{
Uri = WebApiUriService.GetApiUri(EmployeeHttpClient, Endpoint, id.ToString());
var output = await DeleteAsync<EmployeeModel>(EmployeeHttpClient, Endpoint, Uri);
if(output is null)
{
throw GetApiNullOutputException();
}
return new EmployeeModel();
}
}
Example Web Api Service Interface
using Goffo.Http.Base;
using Goffo.Http.Library.Models;
namespace Goffo.Http.Console.WebApiServices;
public interface IEmployeeWebApiService : IWebApiService
{
Task<EmployeeModel> CreateEmployeeAsync(EmployeeModel employee);
Task<EmployeeModel> DeleteEmployeeAsync(int id);
Task<EmployeeModel> GetEmployeeByIdAsync(int id);
Task<IEnumerable<EmployeeModel>> GetEmployeesAsync();
Task<EmployeeModel> UpdateEmployeeAsync(EmployeeModel employee);
}
Example of how to use in an Application
using Goffo.Http.Console.WebApiServices;
using Goffo.Http.Extensions;
using Goffo.Http.Library.Extensions;
using Goffo.Http.Library.Models;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Goffo.Http.Console;
public class App
{
private readonly IHost _host;
public App()
{
_host = Host.CreateDefaultBuilder()
.ConfigureLogging(logger =>
{
logger.ClearProviders();
logger.AddDebug();
logger.AddConsole();
})
.ConfigureServices((context, services) =>
{
services.AddHttpClient("EmployeeHttpClient", client =>
{
client.BaseAddress = new Uri("https://localhost:7177/api/");
});
services.RegisterGoffoHttp();
services.RegisterHttpLibrary();
services.AddScoped<IEmployeeWebApiService, EmployeeWebApiService>();
})
.Build();
}
internal IEmployeeWebApiService EmployeeWebApiService => _host.Services.GetRequiredService<IEmployeeWebApiService>();
public async Task<IEnumerable<EmployeeModel>> GetEmployeesAsync()
{
return await EmployeeWebApiService.GetEmployeesAsync();
}
public async Task<EmployeeModel> GetEmployeeByIdAsync(int id)
{
return await EmployeeWebApiService.GetEmployeeByIdAsync(id);
}
public async Task<EmployeeModel> CreateEmployeeAsync(EmployeeModel employee)
{
return await EmployeeWebApiService.CreateEmployeeAsync(employee);
}
public async Task<EmployeeModel> UpdateEmployeeAsync(EmployeeModel employee)
{
return await EmployeeWebApiService.UpdateEmployeeAsync(employee);
}
public async Task<EmployeeModel> DeleteEmployeeAsync(int id)
{
return await EmployeeWebApiService.DeleteEmployeeAsync(id);
}
}
Program.cs
using Goffo.Http.Console;
using Goffo.Http.Library.Models;
App app = new();
Console.WriteLine("App is running...");
Console.WriteLine();
IEnumerable<EmployeeModel> employees = await app.GetEmployeesAsync();
foreach (EmployeeModel employee in employees)
{
Console.WriteLine(employee.Id);
Console.WriteLine(employee.FirstName);
Console.WriteLine(employee.LastName);
Console.WriteLine(employee.EmailAddress);
Console.WriteLine();
}
int employeeId = 3;
EmployeeModel employeeById = await app.GetEmployeeByIdAsync(employeeId);
Console.WriteLine(employeeById.Id);
Console.WriteLine(employeeById.FirstName);
Console.WriteLine(employeeById.LastName);
Console.WriteLine(employeeById.EmailAddress);
Console.WriteLine();
EmployeeModel createdEmployee = new()
{
FirstName = "John",
LastName = "Doe",
EmailAddress = "john.doe@jdoe.com"
};
EmployeeModel createdEmployeeReturned = await app.CreateEmployeeAsync(createdEmployee);
Console.WriteLine(createdEmployeeReturned.Id);
Console.WriteLine();
EmployeeModel updateEmployee = new()
{
FirstName = "Jane",
LastName = "Doe",
EmailAddress = "jane.doe@fakemail.com"
};
EmployeeModel updatedEmployeeReturned = await app.UpdateEmployeeAsync(updateEmployee);
Console.WriteLine(updateEmployee.Id);
Console.WriteLine(updateEmployee.FirstName);
Console.WriteLine(updateEmployee.LastName);
Console.WriteLine(updateEmployee.EmailAddress);
Console.WriteLine();
int deleteEmployeeId = updateEmployee.Id;
EmployeeModel deletedEmployeeReturned = await app.DeleteEmployeeAsync(deleteEmployeeId);
Console.WriteLine(deletedEmployeeReturned.Id);
Console.WriteLine(deletedEmployeeReturned.FirstName);
Console.WriteLine(deletedEmployeeReturned.LastName);
Console.WriteLine(deletedEmployeeReturned.EmailAddress);
Console.WriteLine();
Console.ReadLine();
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Microsoft.AspNetCore.WebUtilities (>= 9.0.3)
- Microsoft.Extensions.Http (>= 9.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Initial Release