ObjectUrl.Core
0.1.0-preview7
dotnet add package ObjectUrl.Core --version 0.1.0-preview7
NuGet\Install-Package ObjectUrl.Core -Version 0.1.0-preview7
<PackageReference Include="ObjectUrl.Core" Version="0.1.0-preview7" />
paket add ObjectUrl.Core --version 0.1.0-preview7
#r "nuget: ObjectUrl.Core, 0.1.0-preview7"
// Install ObjectUrl.Core as a Cake Addin #addin nuget:?package=ObjectUrl.Core&version=0.1.0-preview7&prerelease // Install ObjectUrl.Core as a Cake Tool #tool nuget:?package=ObjectUrl.Core&version=0.1.0-preview7&prerelease
<img src="https://github.com/NMillard/ObjectUrl/blob/main/images/icon.png" width="100" height="100" alt="icon">
ObjectUrl: Documented API objects
This package came into existence after having to use countless external APIs and dealing with documenting their query parameters, path variables, and response types.
The core concept here is to bundle the API call requirements into a neat single object, which can then be employed with
the HttpClient
. No need to build typed http clients and inject them as constructor arguments anymore.
The idea is simple: create a class that models a request and send the request. That's it.
[Endpoint("api/query/{id}")] // The API endpoint
public class MyApiRequest : Input<MyApiResponse> // MyApiResponse is the expected JSON format
{
[PathParameter("id")] // Property value replaces the path variable {id}
public Guid Id { get; set; }
[QueryParameter("amount")]
public required decimal Amount { get; set; }
[QueryParameter("credit-debit")]
public required string CreditDebit { get; set; }
}
No need to register anything with the dependency injection container. Just use it with the native HttpClient
you already
know and love.
var input = new MyApiRequest
{
Id = Guid.Parse("d65ce0f6-2cec-4a94-8a84-bee4dca75a01"),
Amount = 100,
CreditDebit = "Credit"
};
using var client = new HttpClient
{
BaseAddress = new Uri("http://localhost:5043")
};
MyApiResponse? result = await client.SendRequestAsync(input);
// -> http://localhost:5043/api/query/d65ce0f6-2cec-4a94-8a84-bee4dca75a01?amount=100&credit-debit=Credit
Query strings that are list values
In many scenarios, you will need to convert a list property into a query string. The following are the two conventional strategies:
- Duplicate key: Each list value is represented with a repeated occurrence of the property name.
- Delimited value: The property name occurs once and pairs with a delimited list of values.
ObjectUrl defaults to using a duplicate key strategy for list types, if none is defined.
// Duplicate key strategy
[QueryParameter("values")]
public IEnumerable<string> Values { get; set; } = ["one", "two"]
// -> ?values=one&values=two
// Delimited value
[QueryParameter("values")]
[DelimitedValueStrategy(delimiter: ",")]
public IEnumerable<string> Values { get; set; } = ["one", "two"]
// Notice the comma is url encoded
// -> ?values=one%2ctwo
Authorization
Adding authorization to your requests is easy. There's an AddAuthorization(client, authorization)
extension method on the HttpClient
class.
Note that the
HttpRequest
object is decoupled from the authorization, since one request may have multiple ways to authenticate.
Right off the bat, ObjectUrl supports two authorization types: Basic
and Bearer
.
// Simple Basic authorization using username and password.
// The username and password is concatenated with a colon (:), turned into ASCII bytes, and then converted to base64.
client.AddAuthorization(new BasicAuthorization("username", "password"));
// -> Authorization: Basic dXNlcm5hbWU6cGFzc3dvcmQ
// Bearer
client.AddAuthorization(new BearerAuthorization("some-bearer-token"));
// -> Authorization: Bearer some-bearer-token
Generate code coverage report
Run the generate-report.sh
script.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.0-preview7 | 87 | 2/9/2024 |
0.1.0-preview6 | 66 | 1/31/2024 |
0.1.0-preview5 | 62 | 1/30/2024 |
0.1.0-preview4 | 62 | 1/30/2024 |
0.1.0-preview3 | 67 | 10/11/2023 |
0.1.0-preview2 | 94 | 10/8/2023 |
0.1.0-preview1 | 78 | 10/7/2023 |