-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
OSOE-628: Remove Lombiq.HelpfulLibraries.RestEase from the main package because we recommend Refit now
- Loading branch information
Showing
10 changed files
with
175 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using Refit; | ||
using System; | ||
using System.Net.Http; | ||
|
||
namespace Lombiq.HelpfulLibraries.Refit.Helpers; | ||
|
||
public static class RefitHelper | ||
{ | ||
/// <summary> | ||
/// Generate a Refit implementation of the specified interface with <c>Newtonsoft.Json</c> as the serializer instead | ||
/// of <c>System.Text.Json</c>. | ||
/// </summary> | ||
/// <param name="hostUrl">Base address the implementation will use.</param> | ||
/// <param name="configure">Optional action for configuring other settings.</param> | ||
/// <typeparam name="T">Interface to create the implementation for.</typeparam> | ||
/// <returns>An instance that implements <typeparamref name="T"/>.</returns> | ||
public static T WithNewtonsoftJson<T>(string hostUrl, Action<RefitSettings> configure = null) => | ||
WithNewtonsoftJson<T>(new Uri(hostUrl), configure); | ||
|
||
/// <inheritdoc cref="WithNewtonsoftJson{T}(string,Action{RefitSettings})"/> | ||
public static T WithNewtonsoftJson<T>(Uri hostUrl, Action<RefitSettings> configure = null) => | ||
RestService.For<T>(hostUrl.AbsoluteUri, CreateSettingsWithNewtonsoftJson(configure)); | ||
|
||
/// <summary> | ||
/// Generate a Refit implementation of the specified interface with <c>Newtonsoft.Json</c> as the serializer instead | ||
/// of <c>System.Text.Json</c>. | ||
/// </summary> | ||
/// <param name="httpClient">The <see cref="HttpClient"/> the implementation will use to send requests.</param> | ||
/// <param name="configure">Optional action for configuring other settings.</param> | ||
/// <typeparam name="T">Interface to create the implementation for.</typeparam> | ||
/// <returns>An instance that implements <typeparamref name="T"/>.</returns> | ||
public static T WithNewtonsoftJson<T>(HttpClient httpClient, Action<RefitSettings> configure = null) => | ||
RestService.For<T>(httpClient, CreateSettingsWithNewtonsoftJson(configure)); | ||
|
||
private static RefitSettings CreateSettingsWithNewtonsoftJson(Action<RefitSettings> configure) | ||
{ | ||
var settings = new RefitSettings(new NewtonsoftJsonContentSerializer()); | ||
configure?.Invoke(settings); | ||
return settings; | ||
} | ||
} |
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 @@ | ||
Copyright © 2011, [Lombiq Technologies Ltd.](https://lombiq.com) | ||
|
||
All rights reserved. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
|
||
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
|
||
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
- Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 changes: 32 additions & 0 deletions
32
Lombiq.HelpfulLibraries.Refit/Lombiq.HelpfulLibraries.Refit.csproj
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,32 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<DefaultItemExcludes>$(DefaultItemExcludes);.git*</DefaultItemExcludes> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<Title>Lombiq Helpful Libraries - Refit Libraries for Orchard Core</Title> | ||
<Authors>Lombiq Technologies</Authors> | ||
<Copyright>Copyright © 2011, Lombiq Technologies Ltd.</Copyright> | ||
<Description>Lombiq Helpful Libraries - Refit Libraries for Orchard Core: Adds helpers for working with the Refit RESTful API consumer library. See the project website for detailed documentation.</Description> | ||
<PackageIcon>NuGetIcon.png</PackageIcon> | ||
<PackageTags>Lombiq;Refit;REST;RESTful;API</PackageTags> | ||
<RepositoryUrl>https://github.com/Lombiq/Helpful-Libraries</RepositoryUrl> | ||
<PackageProjectUrl>https://github.com/Lombiq/Helpful-Libraries/blob/dev/Lombiq.HelpfulLibraries.Refit/Readme.md</PackageProjectUrl> | ||
<PackageLicenseFile>License.md</PackageLicenseFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="License.md" Pack="true" PackagePath="" /> | ||
<None Include="Readme.md" /> | ||
<None Include="NuGetIcon.png" Pack="true" PackagePath="" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> | ||
<PackageReference Include="Refit.HttpClientFactory" Version="6.3.2" /> | ||
<PackageReference Include="Refit.Newtonsoft.Json" Version="6.3.2" /> | ||
</ItemGroup> | ||
|
||
</Project> |
64 changes: 64 additions & 0 deletions
64
Lombiq.HelpfulLibraries.Refit/Models/SimpleTextResponse.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,64 @@ | ||
using Refit; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
|
||
namespace Lombiq.HelpfulLibraries.Refit.Models; | ||
|
||
/// <summary> | ||
/// An alternative container to <see cref="ApiResponse{T}"/> of <see cref="string"/> which is not <see | ||
/// cref="IDisposable"/>. It stores the text content, and some important metadata. Pass the <see cref="ApiResponse{T}"/> | ||
/// result of an API endpoint directly into the <see cref="ConvertAndDisposeApiResponse"/> which creates an instance and | ||
/// safely disposes the original response so you don't have to worry about memory leaks from storing or passing around | ||
/// an <see cref="IDisposable"/>. | ||
/// </summary> | ||
public class SimpleTextResponse | ||
{ | ||
/// <summary> | ||
/// Gets the string content of the API response. | ||
/// </summary> | ||
public string Content { get; } | ||
|
||
/// <summary> | ||
/// Gets a read-only dictionary of all headers and their first values. | ||
/// </summary> | ||
public IReadOnlyDictionary<string, string> Headers { get; } | ||
|
||
/// <summary> | ||
/// Gets a value indicating whether the response had no error and its status was <see cref="HttpStatusCode.OK"/>. | ||
/// </summary> | ||
public bool IsOk { get; } | ||
|
||
/// <summary> | ||
/// Gets the error captured by the original <see cref="ApiResponse{T}"/> or <see langword="null"/>. | ||
/// </summary> | ||
public ApiException Error { get; } | ||
|
||
/// <summary> | ||
/// Gets the location header in <see cref="Headers"/>. | ||
/// </summary> | ||
public string Location => Headers.TryGetValue(nameof(Location), out var value) ? value : null; | ||
|
||
internal SimpleTextResponse(IApiResponse<string> response) | ||
{ | ||
Content = response.Content; | ||
Headers = response.Headers.ToDictionary(header => header.Key, header => header.Value.First()); | ||
IsOk = response.Error == null && response.StatusCode == HttpStatusCode.OK; | ||
Error = response.Error; | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new instance of <see cref="SimpleTextResponse"/> from <paramref name="response"/> and then disposes | ||
/// the input. | ||
/// </summary> | ||
public static SimpleTextResponse ConvertAndDisposeApiResponse(ApiResponse<string> response) | ||
{ | ||
if (response == null) return null; | ||
|
||
using (response) | ||
{ | ||
return new SimpleTextResponse(response); | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,11 @@ | ||
# Lombiq Helpful Libraries - Refit Libraries for Orchard Core | ||
|
||
## About | ||
|
||
Adds helpers for working with the [Refit](https://github.com/reactiveui/refit) RESTful API consumer library. | ||
|
||
For general details about and on using the Helpful Libraries see the [root Readme](../Readme.md). | ||
|
||
## Helpers | ||
|
||
- `RefitHelper`: Adds shortcuts for creating Refit API clients from interfaces. (e.g. `RefitHelper.WithNewtonsoftJson<T>()`) |
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