Skip to content

Commit 9b67e1d

Browse files
committed
- Update GetUri() signature
1 parent 5240758 commit 9b67e1d

File tree

6 files changed

+20
-19
lines changed

6 files changed

+20
-19
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ public class CustomerApi : WebApi<CustomerResult>
100100
{
101101
}
102102
103-
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
103+
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
104104
{
105105
// Executes as root or level 1 api. parentApiResult should be null.
106106
var customerContext = (CustomerContext)context;
107107
108-
return string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId);
108+
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId));
109109
}
110110
}
111111
```
@@ -118,10 +118,10 @@ internal class CommunicationApi : WebApi<CommunicationResult>
118118
{
119119
}
120120
121-
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
121+
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
122122
{
123123
var customer = (CustomerResult)parentApiResult;
124-
return string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id);
124+
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id));
125125
}
126126
}
127127
```

src/ApiAggregator/WebApi.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ namespace ApiAggregator.Net
1111
public abstract class WebApi<TResult> : IWebApi
1212
where TResult : IApiResult
1313
{
14-
protected string BaseAddress;
15-
protected string Url;
14+
protected Uri BaseAddress;
15+
protected Uri Url;
1616
private bool isContextResolved;
1717

1818
protected WebApi() : this(null)
@@ -21,7 +21,8 @@ protected WebApi() : this(null)
2121

2222
protected WebApi(string baseAddress)
2323
{
24-
BaseAddress = baseAddress;
24+
if (!string.IsNullOrEmpty(baseAddress))
25+
BaseAddress = new Uri(baseAddress);
2526
}
2627

2728
/// <summary>
@@ -56,7 +57,7 @@ protected virtual List<KeyValuePair<string, string>> GetHeaders()
5657
/// <param name="context">Request Context. Always available.</param>
5758
/// <param name="parentApiResult">Result from parent Api. Only available when configured as nested web api. Else will be null.</param>
5859
/// <returns></returns>
59-
protected abstract string GetUrl(IRequestContext context, IApiResult parentApiResult = null);
60+
protected abstract Uri GetUrl(IRequestContext context, IApiResult parentApiResult = null);
6061

6162
/// <summary>
6263
/// Implement to resolve api parameter.
@@ -83,7 +84,7 @@ public virtual async Task<IApiResult> Run(IHttpClientFactory httpClientFactory,
8384

8485
logger?.LogInformation($"Run api: {GetType().Name}");
8586

86-
if (string.IsNullOrEmpty(Url))
87+
if (Url == null)
8788
return null;
8889

8990
using (var client = httpClientFactory.CreateClient())
@@ -96,8 +97,8 @@ public virtual async Task<IApiResult> Run(IHttpClientFactory httpClientFactory,
9697

9798
try
9899
{
99-
if (!string.IsNullOrEmpty(BaseAddress))
100-
client.BaseAddress = new Uri(BaseAddress);
100+
if (BaseAddress != null)
101+
client.BaseAddress = BaseAddress;
101102

102103
var headers = GetHeaders();
103104

tests/ApiAggregator.Tests/ApiAggregate/WebApis/CommunicationApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public CommunicationApi() : base(Endpoints.BaseAddress)
99
{
1010
}
1111

12-
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
12+
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
1313
{
1414
var customer = (CustomerResult)parentApiResult;
15-
return string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id);
15+
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id), UriKind.Absolute);
1616
}
1717
}
1818
}

tests/ApiAggregator.Tests/ApiAggregate/WebApis/CustomerApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public CustomerApi() : base(Endpoints.BaseAddress)
99
{
1010
}
1111

12-
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
12+
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
1313
{
1414
// Executes as root or level 1 api.
1515
var customerContext = (CustomerContext)context;
1616

17-
return string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId);
17+
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId), UriKind.Absolute);
1818
}
1919
}
2020
}

tests/ApiAggregator.Tests/ApiAggregate/WebApis/OrderItemsApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ public OrderItemsApi() : base(Endpoints.BaseAddress)
1010
{
1111
}
1212

13-
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
13+
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
1414
{
1515
// Execute as nested api to order parent api taking OrderResult to resolve api parameter.
1616
var orders = (CollectionResult<OrderResult>)parentApiResult;
1717
var customerContext = (CustomerContext)context;
1818

19-
return string.Format(Endpoints.BaseAddress + Endpoints.OrderItems, customerContext.CustomerId, orders.Select(o => o.OrderId).ToCSV());
19+
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.OrderItems, customerContext.CustomerId, orders.Select(o => o.OrderId).ToCSV()), UriKind.Absolute);
2020
}
2121
}
2222
}

tests/ApiAggregator.Tests/ApiAggregate/WebApis/OrdersApi.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ public OrdersApi() : base(Endpoints.BaseAddress)
99
{
1010
}
1111

12-
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
12+
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
1313
{
1414
// Execute as child to customer api.
1515
var customer = (CustomerResult)parentApiResult;
1616

17-
return string.Format(Endpoints.BaseAddress + Endpoints.Orders, customer.Id);
17+
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Orders, customer.Id), UriKind.Absolute);
1818
}
1919
}
2020
}

0 commit comments

Comments
 (0)