-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from konak/dev-response-headers
Merge dev-response-headers branch into dev
- Loading branch information
Showing
17 changed files
with
276 additions
and
40 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
.idea/.idea.am.kon.packages.services.web-client-service/.idea/.gitignore
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
8 changes: 8 additions & 0 deletions
8
.idea/.idea.am.kon.packages.services.web-client-service/.idea/indexLayout.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
.idea/.idea.am.kon.packages.services.web-client-service/.idea/vcs.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,3 +1,65 @@ | ||
[![.NET](https://github.com/konak/am.kon.packages.services.web-client-service/actions/workflows/dotnet.yml/badge.svg)](https://github.com/konak/am.kon.packages.services.web-client-service/actions/workflows/dotnet.yml) | ||
|
||
# am.kon.packages.services.web-client-service | ||
# WebClientService for .NET Core | ||
|
||
The WebClientService package provides a robust and flexible web client component designed to be used as a service within .NET Core applications. It leverages the power of '**IHttpClientFactory**' to facilitate efficient and scalable interactions with web services. This package supports dependency injection, making it easy to integrate into your existing .NET Core applications. | ||
|
||
## Key Features: | ||
- **Dependency Injection**: Seamlessly integrates with the .NET Core dependency injection framework. | ||
- **HTTP Client Management**: Utilizes '**IHttpClientFactory**' for robust HTTP client instantiation and management. | ||
- **Result Handling**: Comprehensive handling of HTTP response results with detailed status and error information. | ||
- **Extensible Base Classes**: Abstract base classes for implementing services that interact with web endpoints, returning various data types including strings and streams. | ||
- **Configurable Requests**: Supports various HTTP methods, custom headers, bearer token authentication, and more. | ||
|
||
## Usage Scenarios: | ||
- Consuming RESTful APIs | ||
- Fetching data from web services | ||
- Handling HTTP responses with rich error information | ||
- Streaming data from endpoints | ||
|
||
## Getting Started: | ||
|
||
1. **Installation**: Install the package via NuGet Package Manager. | ||
|
||
```shell | ||
dotnet add package WebClientService | ||
``` | ||
|
||
2. **Configuration**: Configure IHttpClientFactory in your Startup.cs. | ||
|
||
```C# | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddHttpClient(); | ||
services.AddScoped<WebClientStringDataService>(); | ||
services.AddScoped<WebClientStreamDataService>(); | ||
} | ||
``` | ||
|
||
3. **Usage**: Inject the service into your controllers or services and start making web requests. | ||
|
||
```C# | ||
public class MyService | ||
{ | ||
private readonly WebClientStringDataService _webClient; | ||
|
||
public MyService(WebClientStringDataService webClient) | ||
{ | ||
_webClient = webClient; | ||
} | ||
|
||
public async Task<string> GetDataAsync(Uri requestUri) | ||
{ | ||
var result = await _webClient.InvokeRequest(requestUri); | ||
if (result.Result == RequestInvocationResultTypes.Ok) | ||
{ | ||
return result.Data; | ||
} | ||
else | ||
{ | ||
// Handle error | ||
throw new Exception(result.Message); | ||
} | ||
} | ||
} | ||
``` |
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
24 changes: 24 additions & 0 deletions
24
src/am.kon.packages.services.web-client-service/Constants/HttpContentMediaTypesConstants.cs
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
namespace am.kon.packages.services.WebClientService.Constants | ||
{ | ||
/// <summary> | ||
/// Class of media type constants. | ||
/// </summary> | ||
public static class HttpContentMediaTypesConstants | ||
{ | ||
public const string TextPlain = "text/plain"; | ||
public const string TextHtml = "text/html"; | ||
public const string TextCss = "text/css"; | ||
public const string ApplicationJson = "application/json"; | ||
public const string ApplicationXml = "application/xml"; | ||
public const string ApplicationFormUrlEncoded = "application/x-www-form-urlencoded"; | ||
public const string ApplicationOctetStream = "application/octet-stream"; | ||
public const string MultipartFormData = "multipart/form-data"; | ||
public const string ImagePng = "image/png"; | ||
public const string ImageJpeg = "image/jpeg"; | ||
public const string ImageGif = "image/gif"; | ||
public const string AudioMpeg = "audio/mpeg"; | ||
public const string AudioOgg = "audio/ogg"; | ||
public const string VideoMp4 = "video/mp4"; | ||
public const string VideoOgg = "video/ogg"; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...am.kon.packages.services.web-client-service/Extensions/HttpContentMediaTypesExtensions.cs
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
using am.kon.packages.services.WebClientService.Constants; | ||
|
||
namespace am.kon.packages.services.WebClientService.Extensions | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="HttpContentMediaTypes"/>. | ||
/// </summary> | ||
public static class HttpContentMediaTypesExtensions | ||
{ | ||
/// <summary> | ||
/// Extension method to get string representation for instance of <see cref="HttpContentMediaTypes"/>. | ||
/// </summary> | ||
/// <param name="mediaType">Instance of <see cref="HttpContentMediaTypes"/> to get string representation for.</param> | ||
/// <returns>String representation of the instance of <see cref="HttpContentMediaTypes"/>.</returns> | ||
/// <exception cref="ArgumentOutOfRangeException">Trows when invalid value was provided for <see cref="HttpContentMediaTypes"/>.</exception> | ||
public static string GetMimeType(this HttpContentMediaTypes mediaType) | ||
{ | ||
return mediaType switch | ||
{ | ||
HttpContentMediaTypes.TextPlain => HttpContentMediaTypesConstants.TextPlain, | ||
HttpContentMediaTypes.TextHtml => HttpContentMediaTypesConstants.TextHtml, | ||
HttpContentMediaTypes.TextCss => HttpContentMediaTypesConstants.TextCss, | ||
HttpContentMediaTypes.ApplicationJson => HttpContentMediaTypesConstants.ApplicationJson, | ||
HttpContentMediaTypes.ApplicationXml => HttpContentMediaTypesConstants.ApplicationXml, | ||
HttpContentMediaTypes.ApplicationFormUrlEncoded => HttpContentMediaTypesConstants.ApplicationFormUrlEncoded, | ||
HttpContentMediaTypes.ApplicationOctetStream => HttpContentMediaTypesConstants.ApplicationOctetStream, | ||
HttpContentMediaTypes.MultipartFormData => HttpContentMediaTypesConstants.MultipartFormData, | ||
HttpContentMediaTypes.ImagePng => HttpContentMediaTypesConstants.ImagePng, | ||
HttpContentMediaTypes.ImageJpeg => HttpContentMediaTypesConstants.ImageJpeg, | ||
HttpContentMediaTypes.ImageGif => HttpContentMediaTypesConstants.ImageGif, | ||
HttpContentMediaTypes.AudioMpeg => HttpContentMediaTypesConstants.AudioMpeg, | ||
HttpContentMediaTypes.AudioOgg => HttpContentMediaTypesConstants.AudioOgg, | ||
HttpContentMediaTypes.VideoMp4 => HttpContentMediaTypesConstants.VideoMp4, | ||
HttpContentMediaTypes.VideoOgg => HttpContentMediaTypesConstants.VideoOgg, | ||
_ => throw new ArgumentOutOfRangeException(nameof(mediaType), mediaType, null) | ||
}; | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/am.kon.packages.services.web-client-service/Extensions/HttpHeadersExtensions.cs
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http.Headers; | ||
|
||
namespace am.kon.packages.services.WebClientService.Extensions | ||
{ | ||
/// <summary> | ||
/// Extension methods for <see cref="HttpHeaders"/> | ||
/// </summary> | ||
public static class HttpHeadersExtensions | ||
{ | ||
/// <summary> | ||
/// Copy all available headers from instance of <see cref="HttpHeaders"/> to new instance of <see cref="WebClientResponseHeaders"/>. | ||
/// </summary> | ||
/// <param name="headers">Instance of <see cref="HttpHeaders"/> to copy headers from.</param> | ||
/// <returns>New instance of <see cref="WebClientResponseHeaders"/> with all available headers of instance of <see cref="HttpHeaders"/>.</returns> | ||
public static WebClientResponseHeaders ToWebClientResponseHeaders(this HttpHeaders headers) | ||
{ | ||
WebClientResponseHeaders newInstance = new WebClientResponseHeaders(); | ||
|
||
return ToWebClientResponseHeaders(headers, newInstance); | ||
} | ||
|
||
/// <summary> | ||
/// Copy all available headers from instance of <see cref="HttpHeaders"/> to new instance of <see cref="WebClientResponseHeaders"/>. | ||
/// </summary> | ||
/// <param name="srcHeaders">Instance of <see cref="HttpHeaders"/> to copy headers from.</param> | ||
/// <param name="dstHeaders">Instance of <see cref="WebClientResponseHeaders"/> to copy headers to.</param> | ||
/// <returns>Instance of <see cref="WebClientResponseHeaders"/> with all available headers of instance of <see cref="HttpHeaders"/>.</returns> | ||
public static WebClientResponseHeaders ToWebClientResponseHeaders(this HttpHeaders srcHeaders, WebClientResponseHeaders dstHeaders) | ||
{ | ||
foreach (KeyValuePair<string,IEnumerable<string>> header in srcHeaders) | ||
{ | ||
dstHeaders.TryAddWithoutValidation(header.Key, header.Value); | ||
} | ||
|
||
return dstHeaders; | ||
} | ||
} | ||
} |
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
13 changes: 13 additions & 0 deletions
13
src/am.kon.packages.services.web-client-service/WebClientResponseHeaders.cs
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System.Collections.Generic; | ||
using System.Net.Http.Headers; | ||
|
||
namespace am.kon.packages.services.WebClientService | ||
{ | ||
/// <summary> | ||
/// Collection of headers and their values returned from the server after a web client request. | ||
/// </summary> | ||
public class WebClientResponseHeaders : HttpHeaders | ||
{ | ||
|
||
} | ||
} |
Oops, something went wrong.