Skip to content

Commit

Permalink
Merge pull request #196 from Lombiq/issue/OSOE-628
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
Piedone authored Jun 13, 2023
2 parents d8ba265 + bd4acbd commit 1fbbf83
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 6 deletions.
41 changes: 41 additions & 0 deletions Lombiq.HelpfulLibraries.Refit/Helpers/RefitHelper.cs
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;
}
}
13 changes: 13 additions & 0 deletions Lombiq.HelpfulLibraries.Refit/License.md
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 Lombiq.HelpfulLibraries.Refit/Lombiq.HelpfulLibraries.Refit.csproj
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 Lombiq.HelpfulLibraries.Refit/Models/SimpleTextResponse.cs
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);
}
}
}
Binary file added Lombiq.HelpfulLibraries.Refit/NuGetIcon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Lombiq.HelpfulLibraries.Refit/Readme.md
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>()`)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
<PackageReference Include="RestEase" Version="1.5.7" />
<PackageReference Include="RestEase.SourceGenerator" Version="1.5.7">
<PrivateAssets>all</PrivateAssets>
Expand Down
6 changes: 6 additions & 0 deletions Lombiq.HelpfulLibraries.sln
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Lombiq.HelpfulLibraries.Orc
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lombiq.HelpfulLibraries.Cli", "Lombiq.HelpfulLibraries.Cli\Lombiq.HelpfulLibraries.Cli.csproj", "{E4434D6F-7C4F-4CBD-AAA3-B57809DFF571}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lombiq.HelpfulLibraries.Refit", "Lombiq.HelpfulLibraries.Refit\Lombiq.HelpfulLibraries.Refit.csproj", "{5DC1A3D5-0626-4258-95C6-7E5CA5495A80}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -57,6 +59,10 @@ Global
{E4434D6F-7C4F-4CBD-AAA3-B57809DFF571}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E4434D6F-7C4F-4CBD-AAA3-B57809DFF571}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E4434D6F-7C4F-4CBD-AAA3-B57809DFF571}.Release|Any CPU.Build.0 = Release|Any CPU
{5DC1A3D5-0626-4258-95C6-7E5CA5495A80}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5DC1A3D5-0626-4258-95C6-7E5CA5495A80}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5DC1A3D5-0626-4258-95C6-7E5CA5495A80}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5DC1A3D5-0626-4258-95C6-7E5CA5495A80}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
5 changes: 3 additions & 2 deletions Lombiq.HelpfulLibraries/Lombiq.HelpfulLibraries.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Lombiq Technologies</Authors>
<Copyright>Copyright © 2011, Lombiq Technologies Ltd.</Copyright>
<Description>Lombiq Helpful Libraries: Various useful libraries that can be handy when developing for .NET, ASP.NET Core, and Orchard Core, to be used from your own projects. This package references all Helpful Libraries libraries, but you can use only the ones specific for your use-case, see the dependencies. See the project website for detailed documentation.</Description>
<PackageTags>OrchardCore;Lombiq;AspNetCore;YesSql;DateTime;DependencyInjection;GraphQL;Liquid;Localization;Middlewares;Utilities;LinqToDB;RestEase</PackageTags>
<PackageTags>OrchardCore;Lombiq;AspNetCore;YesSql;DateTime;DependencyInjection;GraphQL;Liquid;Localization;Middlewares;Utilities;LinqToDB</PackageTags>
<PackageIcon>NuGetIcon.png</PackageIcon>
<RepositoryUrl>https://github.com/Lombiq/Helpful-Libraries</RepositoryUrl>
<PackageProjectUrl>https://github.com/Lombiq/Helpful-Libraries</PackageProjectUrl>
Expand All @@ -28,10 +28,11 @@

<ItemGroup>
<ProjectReference Include="..\Lombiq.HelpfulLibraries.AspNetCore\Lombiq.HelpfulLibraries.AspNetCore.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Lombiq.HelpfulLibraries.Cli\Lombiq.HelpfulLibraries.Cli.csproj" />
<ProjectReference Include="..\Lombiq.HelpfulLibraries.Common\Lombiq.HelpfulLibraries.Common.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Lombiq.HelpfulLibraries.OrchardCore\Lombiq.HelpfulLibraries.OrchardCore.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Lombiq.HelpfulLibraries.LinqToDb\Lombiq.HelpfulLibraries.LinqToDb.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Lombiq.HelpfulLibraries.RestEase\Lombiq.HelpfulLibraries.RestEase.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Lombiq.HelpfulLibraries.Refit\Lombiq.HelpfulLibraries.Refit.csproj" />
</ItemGroup>

</Project>
7 changes: 4 additions & 3 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Lombiq Helpful Libraries

[![Lombiq.HelpfulLibraries NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries?label=Lombiq.HelpfulLibraries)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries/) [![Lombiq.HelpfulLibraries.AspNetCore NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.AspNetCore?label=Lombiq.HelpfulLibraries.AspNetCore)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.AspNetCore/) [![Lombiq.HelpfulLibraries.Cli NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.Cli?label=Lombiq.HelpfulLibraries.Cli)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.Cli/) [![Lombiq.HelpfulLibraries.Common NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.Common?label=Lombiq.HelpfulLibraries.Common)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.Common/) [![Lombiq.HelpfulLibraries.LinqToDb NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.LinqToDb?label=Lombiq.HelpfulLibraries.LinqToDb)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.LinqToDb/) [![Lombiq.HelpfulLibraries.OrchardCore NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.OrchardCore?label=Lombiq.HelpfulLibraries.OrchardCore)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.OrchardCore/) [![Lombiq.HelpfulLibraries.OrchardCore.Testing NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.OrchardCore.Testing?label=Lombiq.HelpfulLibraries.OrchardCore.Testing)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.OrchardCore.Testing/) [![Lombiq.HelpfulLibraries.RestEase NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.RestEase?label=Lombiq.HelpfulLibraries.RestEase)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.RestEase/)
[![Lombiq.HelpfulLibraries NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries?label=Lombiq.HelpfulLibraries)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries/) [![Lombiq.HelpfulLibraries.AspNetCore NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.AspNetCore?label=Lombiq.HelpfulLibraries.AspNetCore)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.AspNetCore/) [![Lombiq.HelpfulLibraries.Cli NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.Cli?label=Lombiq.HelpfulLibraries.Cli)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.Cli/) [![Lombiq.HelpfulLibraries.Common NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.Common?label=Lombiq.HelpfulLibraries.Common)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.Common/) [![Lombiq.HelpfulLibraries.LinqToDb NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.LinqToDb?label=Lombiq.HelpfulLibraries.LinqToDb)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.LinqToDb/) [![Lombiq.HelpfulLibraries.OrchardCore NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.OrchardCore?label=Lombiq.HelpfulLibraries.OrchardCore)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.OrchardCore/) [![Lombiq.HelpfulLibraries.OrchardCore.Testing NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.OrchardCore.Testing?label=Lombiq.HelpfulLibraries.OrchardCore.Testing)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.OrchardCore.Testing/) [![Lombiq.HelpfulLibraries.Refit NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.Refit?label=Lombiq.HelpfulLibraries.Refit)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.Refit/) [![Lombiq.HelpfulLibraries.RestEase NuGet](https://img.shields.io/nuget/v/Lombiq.HelpfulLibraries.RestEase?label=Lombiq.HelpfulLibraries.RestEase)](https://www.nuget.org/packages/Lombiq.HelpfulLibraries.RestEase/)

## About

Expand All @@ -12,14 +12,15 @@ Note that this project has an Orchard 1 version in the [dev-orchard-1 branch](ht

## Documentation

Helpful Libraries consists of the following independent libraries (all in their own projects/packages). Use the one specific to your use-case, or all at once by depending on `Lombiq.HelpfulLibraries`.
Helpful Libraries consists of the following independent libraries (all in their own projects/packages). Use the one specific to your use-case, or most of them at once by depending on `Lombiq.HelpfulLibraries`.

- [ASP.NET Core Libraries](Lombiq.HelpfulLibraries.AspNetCore/Readme.md)
- [Command Line Libraries](Lombiq.HelpfulLibraries.Cli/Readme.md)
- [Common Libraries](Lombiq.HelpfulLibraries.Common/Readme.md)
- [LINQ to DB Libraries for Orchard Core](Lombiq.HelpfulLibraries.LinqToDb/Readme.md)
- [Orchard Core Libraries](Lombiq.HelpfulLibraries.OrchardCore/Readme.md)
- [RestEase Libraries for Orchard Core](Lombiq.HelpfulLibraries.RestEase/Readme.md)
- [RestEase Libraries for Orchard Core](Lombiq.HelpfulLibraries.RestEase/Readme.md) (Not included in `Lombiq.HelpfulLibraries`, we suggest the Refit libraries instead.)
- [Refit Libraries for Orchard Core](Lombiq.HelpfulLibraries.Refit/Readme.md)

Public APIs are always documented so please always read method comments.

Expand Down

0 comments on commit 1fbbf83

Please sign in to comment.