Skip to content

Commit

Permalink
Merge pull request #663 from yuri-voloshyn/master
Browse files Browse the repository at this point in the history
added RefitRestServiceAttribute for HttpClient relative path
  • Loading branch information
Oren Novotny authored Jun 5, 2019
2 parents c6a3fcf + c5ef508 commit 76f03d3
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 11 deletions.
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 comments on commit 76f03d3

Please sign in to comment.