P2Dev.ApiClient
0.1.0
dotnet add package P2Dev.ApiClient --version 0.1.0
NuGet\Install-Package P2Dev.ApiClient -Version 0.1.0
<PackageReference Include="P2Dev.ApiClient" Version="0.1.0" />
paket add P2Dev.ApiClient --version 0.1.0
#r "nuget: P2Dev.ApiClient, 0.1.0"
// Install P2Dev.ApiClient as a Cake Addin
#addin nuget:?package=P2Dev.ApiClient&version=0.1.0
// Install P2Dev.ApiClient as a Cake Tool
#tool nuget:?package=P2Dev.ApiClient&version=0.1.0
P2Dev.ApiClient
The ApiClient package provides a streamlined, easy-to-use HTTP client for interacting with RESTful APIs following the P2.Dev API Style Specification. It simplifies the process of making API calls, handling authentication via JSON Web Tokens (JWT), refreshing tokens, and pre-/post-processing of HTTP requests and responses.
Features
- Automatic JWT Authentication: Automatically adds JWTs to requests when provided.
- Token Refreshing: Seamlessly refreshes tokens when necessary.
- Error Handling: Provides custom exceptions for various HTTP status codes, facilitating better error management.
Usage
Setting Up
Token Provider: AWS Cognito User Pools
Setting Up with AWS Cognito The P2Dev.ApiClient
package includes a default token provider for AWS Cognito. To utilize this feature, you will need to configure the CognitoTokenProvider
with the appropriate details from your AWS Cognito setup.
Required Information
To set up the CognitoTokenProvider
, you will need the following information:
ClientId
: The client ID of your app client in the Cognito user pool.RedirectUri
: The URI where the OAuth response can be sent and received by your app.PoolName
: The name of your Cognito user pool.Region
: The AWS region where your Cognito user pool is located.PoolId
: The ID of your Cognito user pool.
Configuration Example
Here is a basic example of how to configure and use the CognitoTokenProvider
within your application:
using P2Dev.ApiClient.Cognito;
// Configure the Cognito Token Provider
CognitoTokenProvider tokenProvider = new CognitoTokenProvider {
ClientId = "YOUR_CLIENT_ID",
RedirectUri = "YOUR_REDIRECT_URI",
PoolName = "YOUR_POOL_NAME",
Region = "YOUR_COGNITO_REGION",
PoolId = "YOUR_POOL_ID"
};
// Initialize the ApiClient with the token provider
ApiClientBase apiClient = new ApiClientBase {
TokenProvider = tokenProvider,
BaseURL = "YOUR_API_BASE_URL"
};
// Now you can use the apiClient instance to make authenticated requests
Token Provider: Implement Your Own
First, implement the ITokenProvider
interface to handle token acquisition and refreshing in your application:
public class MyTokenProvider : ITokenProvider {
// Implementation details here
}
Initialize the ApiClientBase
with your ITokenProvider
and base URL:
var apiClient = new ApiClientBase {
TokenProvider = new MyTokenProvider(),
BaseURL = "https://api.example.com/"
};
Making Requests
Use the SendAsync<T>
method to make API calls. Specify the HTTP method, endpoint, and optionally, a request body and whether to retry on authentication failure:
var result = await apiClient.SendAsync<MyResponseType>(HttpMethod.Get, apiClient.GenURL("/my-endpoint"));`
Handling Tokens and Refreshing
The ApiClient handles adding the JWT to requests and refreshing tokens as needed. Implement the GetTokens
and RefreshTokens
methods in your ITokenProvider
to integrate with your token service.
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. |
.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
- Newtonsoft.Json (>= 13.0.3)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on P2Dev.ApiClient:
Package | Downloads |
---|---|
P2Dev.ApiClient.Cognito
This package provides an AWS Cognito User Pools token interface for the P2Dev.ApiClient |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.0 | 130 | 3/16/2024 |