Skip to content

Commit

Permalink
- Update GetUri() signature
Browse files Browse the repository at this point in the history
  • Loading branch information
NinjaRocks committed Nov 4, 2024
1 parent 5240758 commit 9b67e1d
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ public class CustomerApi : WebApi<CustomerResult>
{
}
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
// Executes as root or level 1 api. parentApiResult should be null.
var customerContext = (CustomerContext)context;
return string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId);
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId));
}
}
```
Expand All @@ -118,10 +118,10 @@ internal class CommunicationApi : WebApi<CommunicationResult>
{
}
protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
var customer = (CustomerResult)parentApiResult;
return string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id);
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id));
}
}
```
Expand Down
15 changes: 8 additions & 7 deletions src/ApiAggregator/WebApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ namespace ApiAggregator.Net
public abstract class WebApi<TResult> : IWebApi
where TResult : IApiResult
{
protected string BaseAddress;
protected string Url;
protected Uri BaseAddress;
protected Uri Url;
private bool isContextResolved;

protected WebApi() : this(null)

Check warning on line 18 in src/ApiAggregator/WebApi.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
Expand All @@ -21,7 +21,8 @@ protected WebApi() : this(null)

protected WebApi(string baseAddress)

Check warning on line 22 in src/ApiAggregator/WebApi.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'BaseAddress' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

Check warning on line 22 in src/ApiAggregator/WebApi.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'Url' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
BaseAddress = baseAddress;
if (!string.IsNullOrEmpty(baseAddress))
BaseAddress = new Uri(baseAddress);
}

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

Check warning on line 60 in src/ApiAggregator/WebApi.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

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

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

if (string.IsNullOrEmpty(Url))
if (Url == null)
return null;

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

try
{
if (!string.IsNullOrEmpty(BaseAddress))
client.BaseAddress = new Uri(BaseAddress);
if (BaseAddress != null)
client.BaseAddress = BaseAddress;

var headers = GetHeaders();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ public CommunicationApi() : base(Endpoints.BaseAddress)
{
}

protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
var customer = (CustomerResult)parentApiResult;
return string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id);
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Communication, customer.Id), UriKind.Absolute);
}
}
}
4 changes: 2 additions & 2 deletions tests/ApiAggregator.Tests/ApiAggregate/WebApis/CustomerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public CustomerApi() : base(Endpoints.BaseAddress)
{
}

protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
// Executes as root or level 1 api.
var customerContext = (CustomerContext)context;

return string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId);
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Customer, customerContext.CustomerId), UriKind.Absolute);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public OrderItemsApi() : base(Endpoints.BaseAddress)
{
}

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

return string.Format(Endpoints.BaseAddress + Endpoints.OrderItems, customerContext.CustomerId, orders.Select(o => o.OrderId).ToCSV());
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.OrderItems, customerContext.CustomerId, orders.Select(o => o.OrderId).ToCSV()), UriKind.Absolute);
}
}
}
4 changes: 2 additions & 2 deletions tests/ApiAggregator.Tests/ApiAggregate/WebApis/OrdersApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ public OrdersApi() : base(Endpoints.BaseAddress)
{
}

protected override string GetUrl(IRequestContext context, IApiResult parentApiResult)
protected override Uri GetUrl(IRequestContext context, IApiResult parentApiResult)
{
// Execute as child to customer api.
var customer = (CustomerResult)parentApiResult;

return string.Format(Endpoints.BaseAddress + Endpoints.Orders, customer.Id);
return new Uri(string.Format(Endpoints.BaseAddress + Endpoints.Orders, customer.Id), UriKind.Absolute);
}
}
}

0 comments on commit 9b67e1d

Please sign in to comment.