RSCG – Refit

RSCG – Refit
 
 

name Refit
nuget https://www.nuget.org/packages/Refit/
link https://github.com/reactiveui/refit
author ReactiveUI

Generates code for retrieving data from HTTP API

 

This is how you can use Refit .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Refit" Version="7.0.0" />
  </ItemGroup>
	 <PropertyGroup>
        <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
        <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
    </PropertyGroup>

</Project>


The code that you will use is


Console.WriteLine("Hello, World!");
var gitHubApi = RestService.For<IFindPosts>("https://jsonplaceholder.typicode.com/");
var data = await gitHubApi.GetPost(1);
Console.WriteLine(data.Title);


namespace RefitDemo;

public record Post
{
    public int Id { get; set; }
    public string Title { get; set; }
}


namespace RefitDemo;
public interface IFindPosts
{
    [Get("/posts/{nr}")]
    Task<Post> GetPost(long nr);
}


 

The code that is generated is


#pragma warning disable
namespace Refit.Implementation
{

    /// <inheritdoc />
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::RefitDemoRefitInternalGenerated.PreserveAttribute]
    [global::System.Reflection.Obfuscation(Exclude=true)]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    internal static partial class Generated
    {
    }
}
#pragma warning restore

#nullable enable
#pragma warning disable
namespace Refit.Implementation
{

    partial class Generated
    {

    /// <inheritdoc />
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.Diagnostics.DebuggerNonUserCode]
    [global::RefitDemoRefitInternalGenerated.PreserveAttribute]
    [global::System.Reflection.Obfuscation(Exclude=true)]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    partial class RefitDemoIFindPosts
        : global::RefitDemo.IFindPosts

    {
        /// <inheritdoc />
        public global::System.Net.Http.HttpClient Client { get; }
        readonly global::Refit.IRequestBuilder requestBuilder;

        /// <inheritdoc />
        public RefitDemoIFindPosts(global::System.Net.Http.HttpClient client, global::Refit.IRequestBuilder requestBuilder)
        {
            Client = client;
            this.requestBuilder = requestBuilder;
        }
    


        /// <inheritdoc />
        public global::System.Threading.Tasks.Task<global::RefitDemo.Post> GetPost(long @nr) 
        {
            var ______arguments = new object[] { @nr };
            var ______func = requestBuilder.BuildRestResultFuncForMethod("GetPost", new global::System.Type[] { typeof(long) } );
            return (global::System.Threading.Tasks.Task<global::RefitDemo.Post>)______func(this.Client, ______arguments);
        }

        /// <inheritdoc />
        global::System.Threading.Tasks.Task<global::RefitDemo.Post> global::RefitDemo.IFindPosts.GetPost(long @nr) 
        {
            var ______arguments = new object[] { @nr };
            var ______func = requestBuilder.BuildRestResultFuncForMethod("GetPost", new global::System.Type[] { typeof(long) } );
            return (global::System.Threading.Tasks.Task<global::RefitDemo.Post>)______func(this.Client, ______arguments);
        }
    }
    }
}

#pragma warning restore


#pragma warning disable
namespace RefitDemoRefitInternalGenerated
{
    [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
    [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
    [global::System.AttributeUsage (global::System.AttributeTargets.Class | global::System.AttributeTargets.Struct | global::System.AttributeTargets.Enum | global::System.AttributeTargets.Constructor | global::System.AttributeTargets.Method | global::System.AttributeTargets.Property | global::System.AttributeTargets.Field | global::System.AttributeTargets.Event | global::System.AttributeTargets.Interface | global::System.AttributeTargets.Delegate)]
    sealed class PreserveAttribute : global::System.Attribute
    {
        //
        // Fields
        //
        public bool AllMembers;

        public bool Conditional;
    }
}
#pragma warning restore

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/Refit