-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update request modifiers so they can be scoped
- Loading branch information
1 parent
464cfc9
commit a71504f
Showing
7 changed files
with
214 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using ShipEngineSDK.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
|
@@ -19,6 +20,20 @@ namespace ShipEngineSDK | |
/// </summary> | ||
public class ShipEngineClient | ||
{ | ||
/// <summary> | ||
/// Default constructor | ||
/// </summary> | ||
public ShipEngineClient() { } | ||
|
||
/// <summary> | ||
/// Constructor that takes a collection of request modifiers to apply to the request before it is sent | ||
/// </summary> | ||
/// <param name="requestModifiers">Collection of modifiers to be used for each request</param> | ||
protected ShipEngineClient(IEnumerable<Action<HttpRequestMessage>> requestModifiers) | ||
Check warning on line 32 in ShipEngineSDK/ShipEngineClient.cs GitHub Actions / .Net 8.0 on windows-latest
Check warning on line 32 in ShipEngineSDK/ShipEngineClient.cs GitHub Actions / .Net 8.0 on windows-latest
|
||
{ | ||
this.requestModifiers = requestModifiers; | ||
} | ||
|
||
/// <summary> | ||
/// Options for serializing the method call params to JSON. | ||
/// A separate inline setting is used for deserializing the response | ||
|
@@ -46,9 +61,13 @@ public class ShipEngineClient | |
public CancellationToken CancellationToken { get; set; } | ||
|
||
/// <summary> | ||
/// Modifies the client request before it is sent | ||
/// Collections of request modifiers to apply to the request before it is sent | ||
/// </summary> | ||
public Action<HttpRequestMessage>? ModifyRequest { get; set; } | ||
/// <remarks> | ||
/// This is a collection instead of a single action so that modifiers can be added at multiple levels. | ||
/// For example, a consumer could add a modifier at the client level, and then add another at the method level. | ||
/// </remarks> | ||
protected IEnumerable<Action<HttpRequestMessage>> requestModifiers = []; | ||
Check warning on line 70 in ShipEngineSDK/ShipEngineClient.cs GitHub Actions / .Net 8.0 on windows-latest
Check warning on line 70 in ShipEngineSDK/ShipEngineClient.cs GitHub Actions / .Net 8.0 on windows-latest
|
||
|
||
/// <summary> | ||
/// Sets the HttpClient User agent, the json media type, and the API key to be used | ||
|
@@ -209,7 +228,10 @@ public virtual async Task<T> SendHttpRequestAsync<T>(HttpMethod method, string p | |
try | ||
{ | ||
var request = BuildRequest(method, path, jsonContent); | ||
ModifyRequest?.Invoke(request); | ||
foreach (var modifier in requestModifiers ?? []) | ||
{ | ||
modifier?.Invoke(request); | ||
} | ||
response = await client.SendAsync(request, cancellationToken); | ||
|
||
var deserializedResult = await DeserializedResultOrThrow<T>(response); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters