Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pr/66'
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisbetts committed Nov 6, 2014
2 parents 54a12ee + 2bd945f commit c6f6046
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 23 deletions.
29 changes: 29 additions & 0 deletions Refit-Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Net.Http;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
Expand Down Expand Up @@ -297,6 +298,21 @@ protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage reques
}
}

public class TestUrlParameterFormatter : IUrlParameterFormatter
{
readonly string constantParameterOutput;

public TestUrlParameterFormatter(string constantOutput)
{
constantParameterOutput = constantOutput;
}

public string Format(object value, ParameterInfo parameterInfo)
{
return constantParameterOutput;
}
}

[TestFixture]
public class RequestBuilderTests
{
Expand Down Expand Up @@ -551,5 +567,18 @@ public async Task FormFieldGetsAliased()

Assert.AreEqual("rpn=99", content);
}

[Test]
public async Task CustomParmeterFormatter()
{
var settings = new RefitSettings { UrlParameterFormatter = new TestUrlParameterFormatter("custom-parameter") };
var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi), settings);

var factory = fixture.BuildRequestFactoryForMethod("FetchSomeStuff");
var output = factory(new object[] { 5 });

var uri = new Uri(new Uri("http://api"), output.RequestUri);
Assert.AreEqual("/foo/bar/custom-parameter", uri.PathAndQuery);
}
}
}
2 changes: 1 addition & 1 deletion Refit/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ public HeaderAttribute(string header)
Header = header;
}
}
}
}
1 change: 1 addition & 0 deletions Refit/Refit-Monoandroid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RequestBuilderImplementation.cs" />
<Compile Include="RequestBuilder.cs" />
<Compile Include="RefitSettings.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions Refit/Refit-Net45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="RefitSettings.cs" />
<Compile Include="RequestBuilderImplementation.cs" />
<Compile Include="Utility.cs" />
<Compile Include="Attributes.cs" />
Expand All @@ -68,7 +69,6 @@
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />

<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand All @@ -79,4 +79,4 @@
<ItemGroup>
<None Include="packages.Refit-Net45.config" />
</ItemGroup>
</Project>
</Project>
5 changes: 2 additions & 3 deletions Refit/Refit-Portable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<ItemGroup>
<Compile Include="Attributes.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RefitSettings.cs" />
<Compile Include="RequestBuilder.cs" />
<Compile Include="RestService.cs" />
</ItemGroup>
Expand All @@ -56,10 +57,8 @@
<HintPath>..\packages\Microsoft.Net.Http.2.2.28\lib\portable-net40+sl4+win8+wp71+wpa81\System.Net.Http.Extensions.dll</HintPath>
</Reference>
</ItemGroup>

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.21\build\Microsoft.Bcl.Build.targets')" />

<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand All @@ -70,4 +69,4 @@
<ItemGroup>
<None Include="packages.Refit-Portable.config" />
</ItemGroup>
</Project>
</Project>
3 changes: 2 additions & 1 deletion Refit/Refit-WPA81.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Compile Include="RequestBuilderImplementation.cs" />
<Compile Include="RestService.cs" />
<Compile Include="Utility.cs" />
<Compile Include="RefitSettings.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.CSharp.targets" />
</Project>
</Project>
1 change: 1 addition & 0 deletions Refit/Refit-WinRT45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Compile Include="RequestBuilderImplementation.cs" />
<Compile Include="RestService.cs" />
<Compile Include="Utility.cs" />
<Compile Include="RefitSettings.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.Refit-WinRT45.config" />
Expand Down
1 change: 1 addition & 0 deletions Refit/Refit-Xamarin.iOS10.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<Compile Include="RequestBuilderImplementation.cs" />
<Compile Include="RequestBuilder.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RefitSettings.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
</Project>
1 change: 1 addition & 0 deletions Refit/Refit-wp8.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@
<Compile Include="RequestBuilderImplementation.cs" />
<Compile Include="RestService.cs" />
<Compile Include="Utility.cs" />
<Compile Include="RefitSettings.cs" />
</ItemGroup>

<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
Expand Down
27 changes: 27 additions & 0 deletions Refit/RefitSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Reflection;

namespace Refit
{
public class RefitSettings
{
public RefitSettings()
{
UrlParameterFormatter = new DefaultUrlParameterFormatter();
}

public IUrlParameterFormatter UrlParameterFormatter { get; set; }
}

public interface IUrlParameterFormatter
{
string Format(object value, ParameterInfo parameterInfo);
}

public class DefaultUrlParameterFormatter : IUrlParameterFormatter
{
public virtual string Format(object parameterValue, ParameterInfo parameterInfo)
{
return parameterValue != null ? parameterValue.ToString() : null;
}
}
}
20 changes: 15 additions & 5 deletions Refit/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,38 @@ public interface IRequestBuilder

interface IRequestBuilderFactory
{
IRequestBuilder Create(Type interfaceType);
IRequestBuilder Create(Type interfaceType, RefitSettings settings);
}

public static class RequestBuilder
{
static readonly IRequestBuilderFactory platformRequestBuilderFactory = new RequestBuilderFactory();


public static IRequestBuilder ForType(Type interfaceType, RefitSettings settings)
{
return platformRequestBuilderFactory.Create(interfaceType, settings);
}

public static IRequestBuilder ForType(Type interfaceType)
{
return platformRequestBuilderFactory.Create(interfaceType);
return platformRequestBuilderFactory.Create(interfaceType, null);
}

public static IRequestBuilder ForType<T>(RefitSettings settings)
{
return ForType(typeof(T), settings);
}

public static IRequestBuilder ForType<T>()
{
return ForType(typeof(T));
return ForType(typeof(T), null);
}
}

#if PORTABLE
class RequestBuilderFactory : IRequestBuilderFactory
{
public IRequestBuilder Create(Type interfaceType)
public IRequestBuilder Create(Type interfaceType, RefitSettings settings = null)
{
throw new NotImplementedException("You've somehow included the PCL version of Refit in your app. You need to use the platform-specific version!");
}
Expand Down
18 changes: 11 additions & 7 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ namespace Refit
{
public class RequestBuilderFactory : IRequestBuilderFactory
{
public IRequestBuilder Create(Type interfaceType)
public IRequestBuilder Create(Type interfaceType, RefitSettings settings = null)
{
return new RequestBuilderImplementation(interfaceType);
return new RequestBuilderImplementation(interfaceType, settings);
}
}

public class RequestBuilderImplementation : IRequestBuilder
{
readonly Type targetType;
readonly Dictionary<string, RestMethodInfo> interfaceHttpMethods;
readonly RefitSettings settings;

public RequestBuilderImplementation(Type targetInterface)
public RequestBuilderImplementation(Type targetInterface, RefitSettings refitSettings = null)
{
settings = refitSettings ?? new RefitSettings();
if (targetInterface == null || !targetInterface.IsInterface()) {
throw new ArgumentException("targetInterface must be an Interface");
}
Expand Down Expand Up @@ -75,9 +77,8 @@ public Func<object[], HttpRequestMessage> BuildRequestFactoryForMethod(string me
urlTarget = Regex.Replace(
urlTarget,
"{" + restMethod.ParameterMap[i] + "}",
paramList[i].ToString(),
settings.UrlParameterFormatter.Format(paramList[i], restMethod.ParameterInfoMap[i]),
RegexOptions.IgnoreCase);

continue;
}

Expand Down Expand Up @@ -108,7 +109,7 @@ public Func<object[], HttpRequestMessage> BuildRequestFactoryForMethod(string me
setHeader(ret, restMethod.HeaderParameterMap[i], paramList[i]);
} else {
if (paramList[i] != null) {
queryParamsToAdd[restMethod.QueryParameterMap[i]] = paramList[i].ToString();
queryParamsToAdd[restMethod.QueryParameterMap[i]] = settings.UrlParameterFormatter.Format(paramList[i], restMethod.ParameterInfoMap[i]);
}
}
}
Expand Down Expand Up @@ -313,6 +314,7 @@ public class RestMethodInfo
public Dictionary<int, string> HeaderParameterMap { get; set; }
public Tuple<BodySerializationMethod, int> BodyParameterInfo { get; set; }
public Dictionary<int, string> QueryParameterMap { get; set; }
public Dictionary<int, ParameterInfo> ParameterInfoMap { get; set; }
public Type ReturnType { get; set; }
public Type SerializedReturnType { get; set; }

Expand All @@ -335,7 +337,9 @@ public RestMethodInfo(Type targetInterface, MethodInfo methodInfo)
determineReturnTypeInfo(methodInfo);

var parameterList = methodInfo.GetParameters().ToList();

ParameterInfoMap = parameterList
.Select((parameter, index) => new { index, parameter })
.ToDictionary(x => x.index, x => x.parameter);
ParameterMap = buildParameterMap(RelativePath, parameterList);
BodyParameterInfo = findBodyParameter(parameterList);

Expand Down
18 changes: 14 additions & 4 deletions Refit/RestService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ interface IRestService

public static class RestService
{
public static T For<T>(HttpClient client)
public static T For<T>(HttpClient client, RefitSettings settings)
{
var className = "AutoGenerated" + typeof(T).Name;
var requestBuilder = RequestBuilder.ForType<T>();
var requestBuilder = RequestBuilder.ForType<T>(settings);
var typeName = typeof(T).AssemblyQualifiedName.Replace(typeof(T).Name, className);
var generatedType = Type.GetType(typeName);

Expand All @@ -31,10 +31,20 @@ public static T For<T>(HttpClient client)
return (T)Activator.CreateInstance(generatedType, client, requestBuilder);
}

public static T For<T>(string hostUrl)
public static T For<T>(HttpClient client)
{
return RestService.For<T>(client, null);
}

public static T For<T>(string hostUrl, RefitSettings settings)
{
var client = new HttpClient() { BaseAddress = new Uri(hostUrl) };
return RestService.For<T>(client);
return RestService.For<T>(client, settings);
}

public static T For<T>(string hostUrl)
{
return RestService.For<T>(hostUrl, null);
}
}
}

0 comments on commit c6f6046

Please sign in to comment.