F3R4L.DevPack.Api
1.0.5
dotnet add package F3R4L.DevPack.Api --version 1.0.5
NuGet\Install-Package F3R4L.DevPack.Api -Version 1.0.5
<PackageReference Include="F3R4L.DevPack.Api" Version="1.0.5" />
<PackageVersion Include="F3R4L.DevPack.Api" Version="1.0.5" />
<PackageReference Include="F3R4L.DevPack.Api" />
paket add F3R4L.DevPack.Api --version 1.0.5
#r "nuget: F3R4L.DevPack.Api, 1.0.5"
#:package F3R4L.DevPack.Api@1.0.5
#addin nuget:?package=F3R4L.DevPack.Api&version=1.0.5
#tool nuget:?package=F3R4L.DevPack.Api&version=1.0.5
This is a library intended to simplify making REST API calls. There are two possible ways of using it.
Simplified
The first is the original, more simple manner. Create a new class that inherits from one of the "Endpoint" classes. These are named per the Http VERB they are used for, and have variants depending on the request and response types:
- GetEndpoint
- PostEndpoint
- PutEndpoint
- PatchEndpoint
- DeleteEndpoint
Each endpoint type has sub-variants that may be used, dependant on the number of request and / or response types. An example endpoint for a POST with a user defined request type of "TestRequest" & a response type of "TestResponse" would be:
<details> <summary>Example</summary>
public class TestPostEndpoint : PostEndpoint<TestRequest, TestResponse>
{
public TestPostEndpoint(string hostName, string endpoint)
: base(hostName, endpoint) { }
}
</details>
Enhanced
An enhanced functionality version switches the base classes to the following:
- AuditableGetEndpoint
- AuditablePostEndpoint
- AuditablePutEndpoint
- AuditablePatchEndpoint
- AuditableDeleteEndpoint
Again, each endpoint type has sub-variants that may be used, dependant on the number of request and / or response types. An example endpoint for a POST with a user defined request type of "TestRequest" & a response type of "TestResponse" would be:
<details> <summary>Example</summary>
public class TestPostEndpoint : AuditablePostEndpoint<TestRequest, TestResponse>
{
public TestPostEndpoint(string hostName, string endpoint)
: base(hostName, endpoint) { }
}
</details>
Differences
The simplified system attempts to return an object of type "TestResponse": if the response form the server cannot be deserialised to the target type, an "ApiCallException" is throw that contains the details of the problem. The enhanced system will always return an "AuditContainer":
<details>
<summary><bold>AuditContainer</bold></summary>
public class AuditContainer
{
public string Url { get; set; }
public HttpStatusCode StatusCode { get; set; }
public string ErrorMessage { get; set; }
public string ResponseMessage { get; set; }
}
- Url: The full URL of the request made
- StatusCode: The HttpStatusCode returned by the server
- ErrorMessage: If the request failed, this will contain the error message returned by the server
- ResponseMessage: If the request failed, this will contain the response message returned by the server </details>
<details>
<summary><bold>AuditContainer<T></bold></summary>
public class AuditContainer<T>
{
public string Url { get; set; }
public HttpStatusCode StatusCode { get; set; }
public string ErrorMessage { get; set; }
public string ResponseMessage { get; set; }
public ObjectContainer<T> Request { get; set; }
public ObjectContainer<T> Response { get; set; }
}
- Url: The full URL of the request made
- StatusCode: The HttpStatusCode returned by the server
- ErrorMessage: If the request failed, this will contain the error message returned by the server
- ResponseMessage: If the request failed, this will contain the response message returned by the server
- Request: An ObjectContainer containing the request object and the actual JSON string that was sent
- Response: An ObjectContainer containing the response object and the actual JSON string that was received
Only one of the Request or Response properties will be populated, depending on the type of endpoint used. </details>
<details>
<summary><bold>AuditContainer<TIn, TOut></bold></summary>
public class AuditContainer<TIn, TOut>
{
public string Url { get; set; }
public HttpStatusCode StatusCode { get; set; }
public string ErrorMessage { get; set; }
public string ResponseMessage { get; set; }
public ObjectContainer<TIn> Request { get; set; }
public ObjectContainer<TOut> Response { get; set; }
}
- Url: The full URL of the request made
- StatusCode: The HttpStatusCode returned by the server
- ErrorMessage: If the request failed, this will contain the error message returned by the server
- ResponseMessage: If the request failed, this will contain the response message returned by the server
- Request: An ObjectContainer containing the request object and the actual JSON string that was sent
- Response: An ObjectContainer containing the response object and the actual JSON string that was received </details>
Dependency Injection
To your project, add a reference to the F3R4L.DevPack.Api package. Then, in your injection container, add the following using line:
using F3R4L.DevPack.Api.DependencyInjection;
Then, add the following line to the code:
services.AddApiBindings();
Usage
To your class, add the following using line:
using F3R4L.DevPack.Api.Services;
Then, add the following to your class constructor:
IApiService apiService
Add a private field to your class to contain the injected IApiService, which can be used to make the API calls. For example:
var endpoint = new TestGetEndpoint(_baseUrl, "/get");
var response = await _objectUnderTest.GetAsync(endpoint);
'''
When "TestGetEndpoint" derives from "GetEndpoint", the variable response will be of type "T" that you defined in the endpoint class.
If "TestGetEndpoint" derives from "AuditableGetEndpoint", the variable response will be of type "AuditContainer<T>" that you defined in the endpoint class.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. net9.0 was computed. 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. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.Extensions.Http (>= 5.0.0)
- Newtonsoft.Json (>= 13.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.