Dennisvg.DG.Common.Http 1.2.20

dotnet add package Dennisvg.DG.Common.Http --version 1.2.20
NuGet\Install-Package Dennisvg.DG.Common.Http -Version 1.2.20
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="Dennisvg.DG.Common.Http" Version="1.2.20" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dennisvg.DG.Common.Http --version 1.2.20
#r "nuget: Dennisvg.DG.Common.Http, 1.2.20"
#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.
// Install Dennisvg.DG.Common.Http as a Cake Addin
#addin nuget:?package=Dennisvg.DG.Common.Http&version=1.2.20

// Install Dennisvg.DG.Common.Http as a Cake Tool
#tool nuget:?package=Dennisvg.DG.Common.Http&version=1.2.20

dg-common-http

Web access utilities for c#

Fluent HTTP requests

Using the FluentRequest class, which seamlessly integrates in .Net's System.Net.Http environment, it is possible to easily set up a web request by chaining method calls. This allows you to replace the following code

using System.Net.Http;

var client = new HttpClient();

var formValues = new KeyValuePair<string, string>[]
{
  new KeyValuePair<string, string>("username", "test"),
  new KeyValuePair<string, string>("password", "MyPassword123#")
};
var request = new HttpRequestMessage(HttpMethod.Post, "https://www.example.com/login")
{
  Content = new FormUrlEncodedContent(formValues)
};
request.Headers.Add("Authorization", "Bearer " + token);

var response = await client.SendAsync(request);

With this

using DG.Common.Http.Fluent;

var response = await FluentRequest.Post.To("https://www.example.com/login")
  .WithContent(FluentFormContentBuilder
    .With("username", "test")
    .AndWith("password", "MyPassword123#"))
  .WithAuthorizaton(FluentAuthorization.FromBearer(token))
  .Send();

OAuth authorization

Implementing IOAuthLogic

To start using OAuth2 authorization, you first need to implement the four methods in the IOAuthLogic interface (found in the DG.Common.Http.Authorization.OAuth2.Interfaces namespace):

  1. BuildAuthorizationUri(OAuthRequest request)

    This method should create an authorization url to redirect a user to from an authorization request with the given scopes, callback url, and state. for example:

    https://www.api-service.com/authorize?state=abcdefg&scopes=user.read%2Cuser.write&callback-url=https%3A%2F%2Fwww.my-own-app.com%2Fcallback
    
  2. GetAccessTokenAsync(OAuthRequest request, string callBackCode)

    This method should call the external api with a given callBack code, and should return an access token, an expiration date for this access token, and a refresh token (if applicable).

  3. TryRefreshTokenAsync(string refreshToken)

    This method should call the external api with a refresh token, and return a new access token, a new expiration date for this access token, and a new refresh token (if needed).

  4. GetHeaderForToken(string accessToken)

    This method should create an authorization header value for the given access token.

For ease of use, it is recommended to implement IOAuthLogic in a class that has a public, parameterless constructor.

Starting an authorization flow

Starting an authorization flow can be done using any class that implements IOAuthLogic, like the below example.

var scopes = new string[] { "user.read", "email.read", "email.send" };
Uri callbackUri = new Uri("https://www.my-own-app.com/callback");

//ExampleOAuthLogic must implement IOAuthLogic, and expose a parameterless constructor
var flow = OAuthFlow.StartNew<ExampleOAuthLogic>(scopes, callbackUri);

//alternatively, if ExampleOAuthLogic does not have a parameterless constructor
var logic = new ExampleOAuthLogic(clientId, clientSecret);
var flow = OAuthFlow.StartNewFor(logic, scopes, callbackUri);

After receiving the callback code from your callback endpoint, you can continue the authorization flow like this:

await flow.AuthorizationCallbackAsync(callbackCode);

This instance of OAuthFlow is now authorized, and you can now use it to create an authorization header. The OAuthFlow class will automatically refresh the token, if needed (and possible).

var headerValue = await flow.GetAuthorizationHeaderAsync();

var request = new HttpRequestMessage(HttpMethod.Post, "https://api-service.com");
request.Headers.Add("Authorization", headerValue.ToString());

// OAuthFlow can also be used to provide Authorization headers to a FluentRequest directly, like this:
var request = FluentRequest.Post.To("https://api-service.com")
  .WithAuthorization(flow);

Note that an authorization flow can be interrupted and continued at any time, by simply exporting the flow and using the exported data to create a new instance.

var data = flow.Export();

OAuthFlow continued = OAuthFlow.Continue<ExampleOAuthLogic>(data);
Product Compatible and additional computed target framework versions.
.NET Framework net452 is compatible.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Dennisvg.DG.Common.Http:

Package Downloads
Dennisvg.DG.OneDrive

Common utilities

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.20 188 12/22/2023
1.2.19 133 12/9/2023
1.2.18 123 12/1/2023
1.2.15 132 11/17/2023
1.2.14 102 11/17/2023
1.2.13 106 11/16/2023
1.2.12 97 11/16/2023
1.1.5 116 11/9/2023
1.1.4 126 11/8/2023
1.1.2 103 11/7/2023
1.1.1 140 11/4/2023
1.1.0 114 11/4/2023
1.0.6 119 11/4/2023
1.0.5 111 11/4/2023
1.0.4 106 11/4/2023
1.0.3 109 11/4/2023
0.6.1 102 11/3/2023
0.5.1 121 11/2/2023
0.5.0 120 10/18/2023
0.4.2 127 10/5/2023
0.2.1 141 5/31/2023