8000 added RefitRestServiceAttribute for HttpClient relative path by yuri-voloshyn · Pull Request #663 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

added RefitRestServiceAttribute for HttpClient relative path #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Refit.Tests/InterfaceStubGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void GenerateTemplateInfoForInterfaceListSmokeTest()
.ToList();

var result = fixture.GenerateTemplateInfoForInterfaceList(input);
Assert.Equal(12, result.ClassList.Count);
Assert.Equal(14, result.ClassList.Count);
}

[Fact]
Expand Down
64 changes: 64 additions & 0 deletions Refit.Tests/RefitStubs.Net46.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,70 @@ Task<RootObject> INpmJs.GetCongruence()
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[Preserve]
[global::System.Reflection.Obfuscation(Exclude=true)]
partial class AutoGeneratedIRelativePathApi1 : IRelativePathApi1
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedIRelativePathApi1(HttpClient client, IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}

/// <inheritdoc />
Task IRelativePathApi1.Get()
{
var arguments = new object[] { };
var func = requestBuilder.BuildRestResultFuncForMethod("Get", new Type[] { });
return (Task)func(Client, arguments);
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[Preserve]
[global::System.Reflection.Obfuscation(Exclude=true)]
partial class AutoGeneratedIRelativePathApi2 : IRelativePathApi2
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedIRelativePathApi2(HttpClient client, IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}

/// <inheritdoc />
Task IRelativePathApi2.Get()
{
var arguments = new object[] { };
var func = requestBuilder.BuildRestResultFuncForMethod("Get", new Type[] { });
return (Task)func(Client, arguments);
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;
Expand Down
64 changes: 64 additions & 0 deletions Refit.Tests/RefitStubs.NetCore2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,6 +1331,70 @@ Task<RootObject> INpmJs.GetCongruence()
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[Preserve]
[global::System.Reflection.Obfuscation(Exclude=true)]
partial class AutoGeneratedIRelativePathApi1 : IRelativePathApi1
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedIRelativePathApi1(HttpClient client, IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}

/// <inheritdoc />
Task IRelativePathApi1.Get()
{
var arguments = new object[] { };
var func = requestBuilder.BuildRestResultFuncForMethod("Get", new Type[] { });
return (Task)func(Client, arguments);
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;

/// <inheritdoc />
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.Diagnostics.DebuggerNonUserCode]
[Preserve]
[global::System.Reflection.Obfuscation(Exclude=true)]
partial class AutoGeneratedIRelativePathApi2 : IRelativePathApi2
{
/// <inheritdoc />
public HttpClient Client { get; protected set; }
readonly IRequestBuilder requestBuilder;

/// <inheritdoc />
public AutoGeneratedIRelativePathApi2(HttpClient client, IRequestBuilder requestBuilder)
{
Client = client;
this.requestBuilder = requestBuilder;
}

/// <inheritdoc />
Task IRelativePathApi2.Get()
{
var arguments = new object[] { };
var func = requestBuilder.BuildRestResultFuncForMethod("Get", new Type[] { });
return (Task)func(Client, arguments);
}
}
}

namespace Refit.Tests
{
using Refit.Tests.RefitInternalGenerated;
Expand Down
33 changes: 33 additions & 0 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,24 @@ public interface ITrimTrailingForwardSlashApi
Task Get();
}

[BaseAddress("/api/Test")]
public interface IRelativePathApi1
{
HttpClient Client { get; }

[Get("/someendpoint")]
Task Get();
}

[BaseAddress()]
public interface IRelativePathApi2
{
HttpClient Client { get; }

[Get("/someendpoint")]
Task Get();
}

public interface IValidApi
{
[Get("/someendpoint")]
Expand Down Expand Up @@ -1301,5 +1319,20 @@ public void NonGenericCreate()

Assert.Equal(fixture.Client.BaseAddress.AbsoluteUri, expectedBaseAddress);
}

[Fact]
public void RelativePathCreate()
{
var inputBaseAddress = "http://example.com/";

var expectedBaseAddress1 = "http://example.com/api/Test";
var expectedBaseAddress2 = "http://example.com/";

var fixture1 = RestService.For<IRelativePathApi1>(inputBaseAddress);
var fixture2 = RestService.For<IRelativePathApi2>(inputBaseAddress);

Assert.Equal(fixture1.Client.BaseAddress.AbsoluteUri, expectedBaseAddress1);
Assert.Equal(fixture2.Client.BaseAddress.AbsoluteUri, expectedBaseAddress2);
}
}
}
11 changes: 11 additions & 0 deletions Refit/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,4 +318,15 @@ public QueryAttribute(CollectionFormat collectionFormat)
/// </summary>
public CollectionFormat CollectionFormat { get; set; } = CollectionFormat.RefitParameterFormatter;
}

[AttributeUsage(AttributeTargets.Interface)]
public class BaseAddressAttribute : Attribute
{
public BaseAddressAttribute(string relativePath = null)
{
RelativePath = relativePath;
}

public string RelativePath { get; }
}
}
21 changes: 11 additions & 10 deletions Refit/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;

namespace Refit
Expand All @@ -18,9 +19,7 @@ public static class RestService

public static T For<T>(HttpClient client, IRequestBuilder<T> builder)
{
var generatedType = TypeMapping.GetOrAdd(typeof(T), GetGeneratedType<T>());

return (T)Activator.CreateInstance(generatedType, client, builder);
return (T)For(typeof(T), client, builder);
}

public static T For<T>(HttpClient client, RefitSettings settings)
Expand All @@ -43,7 +42,14 @@ public static T For<T>(string hostUrl, RefitSettings settings)

public static object For(Type refitInterfaceType, HttpClient client, IRequestBuilder builder)
{
var generatedType = TypeMapping.GetOrAdd(refitInterfaceType, GetGeneratedType(refitInterfaceType));
var generatedType = TypeMapping.GetOrAdd(refitInterfaceType, type => GetGeneratedType(type));

var aps = refitInterfaceType.GetTypeInfo().GetCustomAttributes(true).OfType<BaseAddressAttribute>().FirstOrDefault();

if (aps != null && !string.IsNullOrEmpty(aps.RelativePath))
{
client.BaseAddress = new Uri(client.BaseAddress, aps.RelativePath);
}

return Activator.CreateInstance(generatedType, client, builder);
}
Expand Down Expand Up @@ -94,14 +100,9 @@ public static HttpClient CreateHttpClient(string hostUrl, RefitSettings settings
return new HttpClient(innerHandler ?? new HttpClientHandler()) { BaseAddress = new Uri(hostUrl.TrimEnd('/')) };
}

static Type GetGeneratedType<T>()
{
return GetGeneratedType(typeof(T));
}

static Type GetGeneratedType(Type refitInterfaceType)
{
string typeName = UniqueName.ForType(refitInterfaceType);
var typeName = UniqueName.ForType(refitInterfaceType);

var generatedType = Type.GetType(typeName);

Expand Down
0