This repository has been archived by the owner on Nov 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-78: Extract building of request messages into a request builder
- Loading branch information
1 parent
23d9fb6
commit 6d67406
Showing
8 changed files
with
196 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
Source/Lib/TraktApiSharp/Requests/Handler/RequestBuilder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
namespace TraktApiSharp.Requests.Handler | ||
{ | ||
using Base; | ||
using Exceptions; | ||
using Interfaces; | ||
using Interfaces.Base; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net.Http.Headers; | ||
using UriTemplates; | ||
|
||
internal sealed class RequestBuilder | ||
{ | ||
private const string AUTHENTICATION_SCHEME = "Bearer"; | ||
|
||
private IRequest _request; | ||
private IRequestBody _requestBody; | ||
private readonly TraktClient _client; | ||
|
||
internal RequestBuilder(TraktClient client) | ||
{ | ||
_client = client ?? throw new ArgumentNullException(nameof(client)); | ||
} | ||
|
||
internal RequestBuilder(IRequest request, TraktClient client) : this(client) | ||
{ | ||
_request = request; | ||
} | ||
|
||
internal RequestBuilder WithRequestBody(IRequestBody requestBody) | ||
{ | ||
_requestBody = requestBody; | ||
return this; | ||
} | ||
|
||
internal RequestBuilder Reset(IRequest request) | ||
{ | ||
_request = request; | ||
_requestBody = null; | ||
return this; | ||
} | ||
|
||
internal ExtendedHttpRequestMessage Build() | ||
{ | ||
ExtendedHttpRequestMessage requestMessage = CreateRequestMessage(); | ||
AddRequestBodyContent(requestMessage); | ||
SetRequestMessageHeadersForAuthorization(requestMessage); | ||
return requestMessage; | ||
} | ||
|
||
private ExtendedHttpRequestMessage CreateRequestMessage() | ||
{ | ||
if (_request == null) | ||
throw new ArgumentNullException(nameof(_request)); | ||
|
||
const string seasonKey = "season"; | ||
const string episodeKey = "episode"; | ||
|
||
string url = BuildUrl(); | ||
var requestMessage = new ExtendedHttpRequestMessage(_request.Method, url) { Url = url }; | ||
|
||
if (_request is IHasId) | ||
{ | ||
var idRequest = _request as IHasId; | ||
|
||
requestMessage.ObjectId = idRequest?.Id; | ||
requestMessage.RequestObjectType = idRequest?.RequestObjectType; | ||
} | ||
|
||
IDictionary<string, object> parameters = _request.GetUriPathParameters(); | ||
|
||
if (parameters.Count != 0) | ||
{ | ||
if (parameters.ContainsKey(seasonKey)) | ||
{ | ||
var strSeasonNumber = (string)parameters[seasonKey]; | ||
|
||
if (uint.TryParse(strSeasonNumber, out uint seasonNumber)) | ||
requestMessage.SeasonNumber = seasonNumber; | ||
} | ||
|
||
if (parameters.ContainsKey(episodeKey)) | ||
{ | ||
var strEpisodeNumber = (string)parameters[episodeKey]; | ||
|
||
if (uint.TryParse(strEpisodeNumber, out uint episodeNumber)) | ||
requestMessage.EpisodeNumber = episodeNumber; | ||
} | ||
} | ||
|
||
return requestMessage; | ||
} | ||
|
||
private string BuildUrl() | ||
{ | ||
var uriTemplate = new UriTemplate(_request.UriTemplate); | ||
IDictionary<string, object> requestUriParameters = _request.GetUriPathParameters(); | ||
|
||
foreach (KeyValuePair<string, object> parameter in requestUriParameters) | ||
uriTemplate.AddParameterFromKeyValuePair(parameter.Key, parameter.Value); | ||
|
||
string uri = uriTemplate.Resolve(); | ||
return $"{_client.Configuration.BaseUrl}{uri}"; | ||
} | ||
|
||
private void AddRequestBodyContent(ExtendedHttpRequestMessage requestMessage) | ||
{ | ||
if (_requestBody != null) | ||
{ | ||
requestMessage.Content = _requestBody.ToHttpContent(); | ||
requestMessage.RequestBodyJson = _requestBody.HttpContentAsString; | ||
} | ||
} | ||
|
||
private void SetRequestMessageHeadersForAuthorization(ExtendedHttpRequestMessage requestMessage) | ||
{ | ||
AuthorizationRequirement authorizationRequirement = _request.AuthorizationRequirement; | ||
|
||
if (authorizationRequirement == AuthorizationRequirement.Required) | ||
{ | ||
if (!_client.Authentication.IsAuthorized) | ||
throw new TraktAuthorizationException("authorization is required for this request, but the current authorization parameters are invalid"); | ||
|
||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AUTHENTICATION_SCHEME, _client.Authentication.Authorization.AccessToken); | ||
} | ||
|
||
if (authorizationRequirement == AuthorizationRequirement.Optional && _client.Configuration.ForceAuthorization && _client.Authentication.IsAuthorized) | ||
requestMessage.Headers.Authorization = new AuthenticationHeaderValue(AUTHENTICATION_SCHEME, _client.Authentication.Authorization.AccessToken); | ||
} | ||
} | ||
} |
Oops, something went wrong.