Skip to content
This repository has been archived by the owner on Nov 24, 2020. It is now read-only.

Commit

Permalink
GH-78: Extract building of request messages into a request builder
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikfroehling committed Oct 28, 2017
1 parent 23d9fb6 commit 6d67406
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace TraktApiSharp.Requests.Authentication
{
using Core;
using Extensions;
using Interfaces;
using Objects.Authentication.Implementations;
using System;
Expand All @@ -24,11 +25,20 @@ public void Validate()
if (Device == null)
throw new ArgumentNullException(nameof(Device));

if (string.IsNullOrEmpty(ClientId))
throw new ArgumentNullException(nameof(ClientId));
if (Device.IsExpiredUnused)
throw new ArgumentException("device code expired unused", nameof(Device));

if (string.IsNullOrEmpty(ClientSecret))
throw new ArgumentNullException(nameof(ClientSecret));
if (!Device.IsValid)
throw new ArgumentException("device not valid", nameof(Device));

if (Device.DeviceCode.ContainsSpace())
throw new ArgumentException("device code not valid", nameof(Device.DeviceCode));

if (string.IsNullOrEmpty(ClientId) || ClientId.ContainsSpace())
throw new ArgumentException("client id not valid", nameof(ClientId));

if (string.IsNullOrEmpty(ClientSecret) || ClientSecret.ContainsSpace())
throw new ArgumentException("client secret not valid", nameof(ClientSecret));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using Core;
using Enums;
using Extensions;
using Interfaces;
using System;
using System.Net.Http;
Expand All @@ -25,17 +26,17 @@ internal sealed class AuthorizationRefreshRequestBody : IRequestBody

public void Validate()
{
if (string.IsNullOrEmpty(RefreshToken))
throw new ArgumentNullException(nameof(RefreshToken));
if (string.IsNullOrEmpty(RefreshToken) || RefreshToken.ContainsSpace())
throw new ArgumentException("refresh token not valid", nameof(RefreshToken));

if (string.IsNullOrEmpty(ClientId))
throw new ArgumentNullException(nameof(ClientId));
if (string.IsNullOrEmpty(ClientId) || ClientId.ContainsSpace())
throw new ArgumentException("client id not valid", nameof(ClientId));

if (string.IsNullOrEmpty(ClientSecret))
throw new ArgumentNullException(nameof(ClientSecret));
if (string.IsNullOrEmpty(ClientSecret) || ClientSecret.ContainsSpace())
throw new ArgumentException("client secret not valid", nameof(ClientSecret));

if (string.IsNullOrEmpty(RedirectUri))
throw new ArgumentNullException(nameof(RedirectUri));
if (string.IsNullOrEmpty(RedirectUri) || RedirectUri.ContainsSpace())
throw new ArgumentException("redirect uri not valid", nameof(RedirectUri));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{
using Core;
using Enums;
using Extensions;
using Interfaces;
using System;
using System.Net.Http;
Expand All @@ -25,17 +26,17 @@ internal sealed class AuthorizationRequestBody : IRequestBody

public void Validate()
{
if (string.IsNullOrEmpty(Code))
throw new ArgumentNullException(nameof(Code));
if (string.IsNullOrEmpty(Code) || Code.ContainsSpace())
throw new ArgumentException("code not valid", nameof(Code));

if (string.IsNullOrEmpty(ClientId))
throw new ArgumentNullException(nameof(ClientId));
if (string.IsNullOrEmpty(ClientId) || ClientId.ContainsSpace())
throw new ArgumentException("client id not valid", nameof(ClientId));

if (string.IsNullOrEmpty(ClientSecret))
throw new ArgumentNullException(nameof(ClientSecret));
if (string.IsNullOrEmpty(ClientSecret) || ClientSecret.ContainsSpace())
throw new ArgumentException("client secret not valid", nameof(ClientSecret));

if (string.IsNullOrEmpty(RedirectUri))
throw new ArgumentNullException(nameof(RedirectUri));
if (string.IsNullOrEmpty(RedirectUri) || RedirectUri.ContainsSpace())
throw new ArgumentException("redirect uri not valid", nameof(RedirectUri));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace TraktApiSharp.Requests.Authentication
{
using Core;
using Extensions;
using Interfaces;
using System;
using System.Net.Http;
Expand All @@ -16,8 +17,8 @@ internal sealed class AuthorizationRevokeRequestBody : IRequestBody

public void Validate()
{
if (string.IsNullOrEmpty(AccessToken))
throw new ArgumentNullException(nameof(AccessToken));
if (string.IsNullOrEmpty(AccessToken) || AccessToken.ContainsSpace())
throw new ArgumentException("access token not valid", nameof(AccessToken));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace TraktApiSharp.Requests.Authentication
{
using Core;
using Extensions;
using Interfaces;
using System;
using System.Net.Http;
Expand All @@ -16,8 +17,8 @@ internal sealed class DeviceRequestBody : IRequestBody

public void Validate()
{
if (string.IsNullOrEmpty(ClientId))
throw new ArgumentNullException(nameof(ClientId));
if (string.IsNullOrEmpty(ClientId) || ClientId.ContainsSpace())
throw new ArgumentException("client id not valid", nameof(ClientId));
}
}
}
131 changes: 131 additions & 0 deletions Source/Lib/TraktApiSharp/Requests/Handler/RequestBuilder.cs
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);
}
}
}
Loading

0 comments on commit 6d67406

Please sign in to comment.