Skip to content

Commit

Permalink
refactor: clean HalLink builders
Browse files Browse the repository at this point in the history
  • Loading branch information
Tr00d committed Jan 26, 2024
1 parent e3b8506 commit f52d115
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 39 deletions.
45 changes: 33 additions & 12 deletions Vonage/Conversations/GetConversations/GetConversationsResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,40 @@ public record GetConversationsHalLink(Uri Href)
/// <returns></returns>
public Result<GetConversationsRequest> BuildRequest()
{
var queryParameters = HttpUtility.ParseQueryString(this.Href.Query);
var pageSize = queryParameters["page_size"];
var order = queryParameters["order"];
var cursor = queryParameters["cursor"] ?? Maybe<string>.None;
var parameters = ExtractQueryParameters(this.Href);
var builder = new GetConversationsRequestBuilder(parameters.Cursor)
.WithPageSize(parameters.PageSize)
.WithOrder(parameters.Order);
builder = ApplyOptionalStartDate(parameters, builder);
builder = ApplyOptionalEndDate(parameters, builder);
return builder.Create();
}

private static IBuilderForOptional
ApplyOptionalStartDate(QueryParameters parameters, IBuilderForOptional builder) =>
parameters.StartDate.Match(builder.WithStartDate, () => builder);

private static IBuilderForOptional
ApplyOptionalEndDate(QueryParameters parameters, IBuilderForOptional builder) =>
parameters.EndDate.Match(builder.WithEndDate, () => builder);

private static QueryParameters ExtractQueryParameters(Uri uri)
{
var queryParameters = HttpUtility.ParseQueryString(uri.Query);
var startDate = queryParameters["date_start"] ?? Maybe<string>.None;
var endDate = queryParameters["date_end"] ?? Maybe<string>.None;
var builder = new GetConversationsRequestBuilder(cursor)
.WithOrder(Enums.Parse<FetchOrder>(order, false, EnumFormat.Description))
.WithPageSize(int.Parse(pageSize));
startDate.Map(value => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture))
.IfSome(value => builder = builder.WithStartDate(value));
endDate.Map(value => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture))
.IfSome(value => builder = builder.WithEndDate(value));
return builder.Create();
return new QueryParameters(
queryParameters["cursor"],
int.Parse(queryParameters["page_size"]),
Enums.Parse<FetchOrder>(queryParameters["order"], false, EnumFormat.Description),
startDate.Map(value => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture)),
endDate.Map(value => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture)));
}

private record QueryParameters(
Maybe<string> Cursor,
int PageSize,
FetchOrder Order,
Maybe<DateTimeOffset> StartDate,
Maybe<DateTimeOffset> EndDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class GetUserConversationsRequestBuilder : IBuilderForUserId, IBuilderF
private readonly Maybe<string> cursor;
private bool includeCustomData;
private FetchOrder order = FetchOrder.Ascending;
private string orderBy = DefaultOrderBy;
private Maybe<string> orderBy;
private int pageSize = 10;
private Maybe<DateTimeOffset> startDate;
private Maybe<State> state;
Expand All @@ -29,7 +29,7 @@ public Result<GetUserConversationsRequest> Create() => Result<GetUserConversatio
IncludeCustomData = this.includeCustomData,
StartDate = this.startDate,
Order = this.order,
OrderBy = this.orderBy,
OrderBy = this.orderBy.IfNone(DefaultOrderBy),
State = this.state,
Cursor = this.cursor,
})
Expand All @@ -53,11 +53,11 @@ public IBuilderForOptional WithOrder(FetchOrder value) =>
};

/// <inheritdoc />
public IBuilderForOptional WithOrderBy(Maybe<string> value) =>
public IBuilderForOptional WithOrderBy(string value) =>
new GetUserConversationsRequestBuilder(this.cursor)
{
includeCustomData = this.includeCustomData,
orderBy = value.IfNone(DefaultOrderBy),
orderBy = value,
order = this.order,
state = this.state,
pageSize = this.pageSize,
Expand Down Expand Up @@ -170,7 +170,7 @@ public interface IBuilderForOptional : IVonageRequestBuilder<GetUserConversation
/// </summary>
/// <param name="value">The order by.</param>
/// <returns>The builder.</returns>
IBuilderForOptional WithOrderBy(Maybe<string> value);
IBuilderForOptional WithOrderBy(string value);

/// <summary>
/// Sets the state on the builder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,57 @@ public record GetUserConversationsHalLink(Uri Href)
/// <returns></returns>
public Result<GetUserConversationsRequest> BuildRequest()
{
var queryParameters = HttpUtility.ParseQueryString(this.Href.Query);
var userId = this.Href.AbsolutePath.Replace("/v1/users/", string.Empty).Replace("/conversations", string.Empty);
var pageSize = queryParameters["page_size"];
var order = queryParameters["order"];
var orderBy = queryParameters["order_by"] ?? Maybe<string>.None;
var cursor = queryParameters["cursor"] ?? Maybe<string>.None;
var parameters = ExtractQueryParameters(this.Href);
var builder = new GetUserConversationsRequestBuilder(parameters.Cursor)
.WithUserId(parameters.UserId)
.WithPageSize(parameters.PageSize)
.WithOrder(parameters.Order);
builder = ApplyOptionalOrderBy(parameters, builder);
builder = ApplyOptionalStartDate(parameters, builder);
builder = ApplyOptionalState(parameters, builder);
builder = ApplyOptionalIncludeCustomData(parameters, builder);
return builder.Create();
}

private static IBuilderForOptional
ApplyOptionalIncludeCustomData(QueryParameters parameters, IBuilderForOptional builder) =>
parameters.IncludeCustomData.IfNone(false) ? builder.IncludeCustomData() : builder;

private static IBuilderForOptional ApplyOptionalState(QueryParameters parameters, IBuilderForOptional builder) =>
parameters.State.Match(builder.WithState, () => builder);

private static IBuilderForOptional
ApplyOptionalStartDate(QueryParameters parameters, IBuilderForOptional builder) =>
parameters.StartDate.Match(builder.WithStartDate, () => builder);

private static IBuilderForOptional ApplyOptionalOrderBy(QueryParameters parameters, IBuilderForOptional builder) =>
parameters.OrderBy.Match(builder.WithOrderBy, () => builder);

private static QueryParameters ExtractQueryParameters(Uri uri)
{
var queryParameters = HttpUtility.ParseQueryString(uri.Query);
var userId = uri.AbsolutePath.Replace("/v1/users/", string.Empty).Replace("/conversations", string.Empty);
var startDate = queryParameters["date_start"] ?? Maybe<string>.None;
var includeCustomData = queryParameters["include_custom_data"] ?? Maybe<string>.None;
var state = queryParameters["state"] ?? Maybe<string>.None;
var builder = new GetUserConversationsRequestBuilder(cursor)
.WithUserId(userId)
.WithPageSize(int.Parse(pageSize))
.WithOrder(Enums.Parse<FetchOrder>(order, false, EnumFormat.Description))
.WithOrderBy(orderBy);
startDate.Map(value => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture))
.IfSome(value => builder = builder.WithStartDate(value));
state.Map(value => Enums.Parse<State>(value, false, EnumFormat.Description))
.IfSome(value => builder = builder.WithState(value));
if (includeCustomData.Match(bool.Parse, () => false))
{
builder = builder.IncludeCustomData();
}

return builder.Create();
return new QueryParameters(
userId,
queryParameters["cursor"],
int.Parse(queryParameters["page_size"]),
Enums.Parse<FetchOrder>(queryParameters["order"], false, EnumFormat.Description),
queryParameters["order_by"],
startDate.Map(value => DateTimeOffset.Parse(value, CultureInfo.InvariantCulture)),
includeCustomData.Match(bool.Parse, () => false),
state.Map(value => Enums.Parse<State>(value, false, EnumFormat.Description)));
}

private record QueryParameters(
string UserId,
Maybe<string> Cursor,
int PageSize,
FetchOrder Order,
Maybe<string> OrderBy,
Maybe<DateTimeOffset> StartDate,
Maybe<bool> IncludeCustomData,
Maybe<State> State);
}
2 changes: 1 addition & 1 deletion Vonage/Users/GetUsers/GetUsersResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Result<GetUsersRequest> BuildRequest()
{
var queryParameters = HttpUtility.ParseQueryString(this.Href.Query);
var name = queryParameters["name"] ?? Maybe<string>.None;
var cursor = queryParameters["cursor"] ?? Maybe<string>.None;
var cursor = queryParameters["cursor"];
var pageSize = queryParameters["page_size"];
var order = queryParameters["order"];
if (pageSize is null)
Expand Down

0 comments on commit f52d115

Please sign in to comment.