8000 Allow more than one parameter per URL segment. by bennor · Pull Request #47 · reactiveui/refit · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Allow more than one parameter per URL segment. #47

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 4 commits into from
Jun 30, 2014
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
28 changes: 28 additions & 0 deletions Refit-Tests/RequestBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public interface IRestMethodInfoTests
[Get("/foo/bar/{id}")]
Task<string> FetchSomeStuffWithAlias([AliasAs("id")] int anId);

[Get("/foo/bar/{width}x{height}")]
Task<string> FetchAnImage(int width, int height);

[Get("/foo/bar/{id}")]
IObservable<string> FetchSomeStuffWithBody([AliasAs("id")] int anId, [Body] Dictionary<int, string> theData);

Expand Down Expand Up @@ -120,6 +123,17 @@ public void AliasMappingShouldWork()
Assert.IsNull(fixture.BodyParameterInfo);
}

[Test]
public void MultipleParametersPerSegmentShouldWork()
{
var input = typeof(IRestMethodInfoTests);
var fixture = new RestMethodInfo(input, input.GetMethods().First(x => x.Name == "FetchAnImage"));
Assert.AreEqual("width", fixture.ParameterMap[0]);
Assert.AreEqual("height", fixture.ParameterMap[1]);
Assert.AreEqual(0, fixture.QueryParameterMap.Count);
Assert.IsNull(fixture.BodyParameterInfo);
}

[Test]
public void FindTheBodyParameter()
{
Expand Down Expand Up @@ -202,6 +216,9 @@ public interface IDummyHttpApi
[Get("/foo/bar/{id}?baz=bamf")]
Task<string> FetchSomeStuffWithHardcodedAndOtherQueryParameters(int id, [AliasAs("search_for")] string searchQuery);

[Get("/{id}/{width}x{height}/foo")]
Task<string> FetchSomethingWithMultipleParametersPerSegment(int id, int width, int height);

[Get("/foo/bar/{id}")]
[Headers("Api-Version: 2")]
Task<string> FetchSomeStuffWithHardcodedHeader(int id);
Expand Down Expand Up @@ -305,6 +322,17 @@ public void ParameterizedQueryParamsShouldBeInUrl()
Assert.AreEqual("/foo/bar/6?baz=bamf&search_for=foo", uri.PathAndQuery);
}

[Test]
public void MultipleParametersInTheSameSegmentAreGeneratedProperly()
{
var fixture = new RequestBuilderImplementation(typeof(IDummyHttpApi));
var factory = fixture.BuildRequestFactoryForMethod("FetchSomethingWithMultipleParametersPerSegment");
var output = factory(new object[] { 6, 1024, 768 });

var uri = new Uri(new Uri("http://api"), output.RequestUri);
Assert.AreEqual("/6/1024x768/foo", uri.PathAndQuery);
}

[Test]
public void HardcodedHeadersShouldBeInHeaders()
{
Expand Down
9 changes: 4 additions & 5 deletions Refit/RequestBuilderImplementation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public class RestMethodInfo
public Type ReturnType { get; set; }
public Type SerializedReturnType { get; set; }

static readonly Regex parameterRegex = new Regex(@"^{(.*)}$");
static readonly Regex parameterRegex = new Regex(@"{(.*?)}");

public RestMethodInfo(Type targetInterface, MethodInfo methodInfo)
{
Expand Down Expand Up @@ -388,10 +388,9 @@ Dictionary<int, string> buildParameterMap(string relativePath, List<ParameterInf
{
var ret = new Dictionary<int, string>();

var parameterizedParts = relativePath.Split('/', '?').SelectMany(x => {
var m = parameterRegex.Match(x);
return (m.Success ? EnumerableEx.Return(m) : Enumerable.Empty<Match>());
}).ToList();
var parameterizedParts = relativePath.Split('/', '?')
.SelectMany(x => parameterRegex.Matches(x).Cast<Match>())
.ToList();

if (parameterizedParts.Count == 0) {
return ret;
Expand Down
0