Skip to content

Commit

Permalink
Merge pull request #668 from reactiveui/revert-663-master
Browse files Browse the repository at this point in the history
Revert "added RefitRestServiceAttribute for HttpClient relative path"
  • Loading branch information
Oren Novotny authored Jun 5, 2019
2 parents 76f03d3 + 962eba8 commit f7854e0
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 184 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(14, result.ClassList.Count);
Assert.Equal(12, result.ClassList.Count);
}

[Fact]
Expand Down
64 changes: 0 additions & 64 deletions Refit.Tests/RefitStubs.Net46.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,70 +1331,6 @@ 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: 0 additions & 64 deletions Refit.Tests/RefitStubs.NetCore2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1331,70 +1331,6 @@ 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: 0 additions & 33 deletions Refit.Tests/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,24 +139,6 @@ 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 @@ -1319,20 +1301,5 @@ 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: 0 additions & 11 deletions Refit/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,4 @@ 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: 10 additions & 11 deletions Refit/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Threading.Tasks;

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

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

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

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

public static object For(Type refitInterfaceType, HttpClient client, IRequestBuilder builder)
{
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);
}
var generatedType = TypeMapping.GetOrAdd(refitInterfaceType, GetGeneratedType(refitInterfaceType));

return Activator.CreateInstance(generatedType, client, builder);
}
Expand Down Expand Up @@ -100,9 +94,14 @@ 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)
{
var typeName = UniqueName.ForType(refitInterfaceType);
string typeName = UniqueName.ForType(refitInterfaceType);

var generatedType = Type.GetType(typeName);

Expand Down

0 comments on commit f7854e0

Please sign in to comment.