Skip to content

Commit a7dcd48

Browse files
author
Chi Ho Yeung
committed
made changes per CR
1 parent 0ce64f3 commit a7dcd48

15 files changed

+233
-145
lines changed

PowerDms.Api.Rest.Client.Demo/DemoConsoleApp/Program.cs

+24-26
Original file line numberDiff line numberDiff line change
@@ -41,67 +41,65 @@ public static async Task DoDemo()
4141
ClientSecret = configuration["Credentials:ClientSecret"]
4242
};
4343

44-
var httpClient = PowerDmsHttpClientFactory.CreateHttpClient(configuration["Host"], ApiVersion.Version1);
45-
var requestManager = new HttpRequestManagerFactory().CreateInstance(httpClient);
46-
47-
Console.WriteLine("Create Group - please delete from Site");
44+
var requestManager =
45+
new HttpRequestManagerBuilder()
46+
.SetApiHost(configuration["Host"]) // only needed if not using public PowerDMS API
47+
.SetOAuthHost(configuration["Host"]) // only needed if not using public PowerDMS API
48+
.Build(credentials);
4849

4950
var newGroup = new GroupDto
5051
{
5152
Name = "ApiCreated" + Guid.NewGuid()
5253
};
5354

54-
var createdGroup = await NewGroup(requestManager, credentials, newGroup);
55+
var createdGroup = await NewGroup(requestManager, newGroup);
5556

5657
Console.WriteLine($" ID: {createdGroup.Id}");
5758
Console.WriteLine($" Name: {createdGroup.Name}");
5859

59-
var getItAgain = await GetGroup(requestManager, credentials, createdGroup.Id);
60+
var getItAgain = await GetGroup(requestManager, createdGroup.Id);
6061

61-
var getBogusOne = await GetGroup(requestManager, credentials, "0");
62+
var getBogusOne = await GetGroup(requestManager, "0");
6263

6364
Console.WriteLine("-- press any key to exit --");
6465
Console.ReadKey();
6566
}
6667

67-
private static async Task<GroupDto> GetGroup(HttpRequestManager requestManager, Credentials credentials, string groupId)
68+
private static async Task<GroupDto> GetGroup(HttpRequestManager requestManager, string groupId)
6869
{
6970
// I don't like the fact credentials need to be set on each builder, why not in the manager?
7071
var requestBuilder = requestManager.PowerDmsRestApiClient.Groups
71-
.GetGroupRequestBuilder(groupId)
72-
.AuthenticateWith(credentials);
72+
.GetGroupRequestBuilder(groupId);
7373

74-
var result = await requestManager.SendAsync(requestBuilder);
74+
var result = await requestManager
75+
.SendAsync(requestBuilder)
76+
.AwaitGetServiceResponse<GroupDto>();
7577

76-
if (!result.IsSuccessStatusCode)
78+
if (!result.IsSuccessful)
7779
{
78-
// CR note: why would I need to pass a <T> for errors?
79-
var error = await result.GetErrorResponse<GroupDto>();
80-
Console.WriteLine($"-- Error! Code: {error.Code}, Message: {error.Messages?.FirstOrDefault()} --");
80+
Console.WriteLine($"-- Error! Code: {result.Error.Code}, Message: {result.Error.Messages?.FirstOrDefault()} --");
8181
return null;
8282
}
8383

84-
return await result.GetSuccessfulResponse<GroupDto>();
84+
return result.Data;
8585
}
8686

87-
private static async Task<GroupDto> NewGroup(HttpRequestManager requestManager, Credentials credentials, GroupDto groupDto)
87+
private static async Task<GroupDto> NewGroup(HttpRequestManager requestManager, GroupDto groupDto)
8888
{
8989
var requestBuilder = requestManager.PowerDmsRestApiClient.Groups
90-
.PostGroupRequestBuilder(groupDto)
91-
.AuthenticateWith(credentials);
90+
.PostGroupRequestBuilder(groupDto);
9291

93-
var result = await requestManager.SendAsync(requestBuilder);
92+
var result = await requestManager
93+
.SendAsync(requestBuilder)
94+
.AwaitGetServiceResponse<GroupDto>();
9495

95-
if (!result.IsSuccessStatusCode)
96+
if (!result.IsSuccessful)
9697
{
97-
// CR note: why would I need to pass a <T> for errors?
98-
var error = await result.GetErrorResponse<GroupDto>();
99-
Console.WriteLine($"-- Error! Code: {error.Code}, Message: {error.Messages?.FirstOrDefault()} --");
98+
Console.WriteLine($"-- Error! Code: {result.Error.Code}, Message: {result.Error.Messages?.FirstOrDefault()} --");
10099
return null;
101100
}
102101

103-
return await result
104-
.GetSuccessfulResponse<GroupDto>();
102+
return result.Data;
105103
}
106104
}
107105
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Net.Http.Headers;
22
using System.Threading.Tasks;
33
using PowerDms.Api.Rest.Client.Clients;
44
using PowerDms.Api.Rest.Dto;
@@ -7,49 +7,35 @@ namespace PowerDms.Api.Rest.Client
77
{
88
public class AuthenticationTokenProvider : IAuthenticationTokenProvider
99
{
10-
private readonly OAuthClient _OAuthClient;
10+
// TODO: must implement token refreshing at some point
1111

12-
private readonly string _OAuthClientSecret;
12+
private readonly OAuthClient _oAuthClient;
1313

14-
private IDictionary<Credentials, string> _CachedCredentials;
14+
private readonly Credentials _credentials;
1515

16-
public AuthenticationTokenProvider(
17-
OAuthClient oAuthClient,
18-
string oAuthClientSecret)
16+
private OAuthAuthorizationDto _authAuthorization;
17+
18+
public AuthenticationTokenProvider(OAuthClient oAuthClient, Credentials credentials)
1919
{
20-
_OAuthClient = oAuthClient;
21-
_OAuthClientSecret = oAuthClientSecret;
22-
_CachedCredentials = new Dictionary<Credentials, string>();
20+
_oAuthClient = oAuthClient;
21+
_credentials = credentials;
2322
}
2423

25-
public async Task<HttpAuthorization> GetAccessToken(Credentials credentials)
24+
public async Task<AuthenticationHeaderValue> GetAccessToken()
2625
{
27-
if (_CachedCredentials.ContainsKey(credentials))
26+
if (_authAuthorization?.access_token == null)
2827
{
29-
return new HttpAuthorization
30-
{
31-
Type = "Bearer",
32-
Credentials = _CachedCredentials[credentials]
33-
};
28+
var response = await _oAuthClient.GetAccessToken(
29+
_credentials.Username,
30+
_credentials.Password,
31+
_credentials.SiteKey,
32+
_credentials.ClientSecret
33+
);
34+
35+
_authAuthorization = await response.GetContent<OAuthAuthorizationDto>();
3436
}
3537

36-
var response = await _OAuthClient.GetAccessToken(
37-
credentials.Username,
38-
credentials.Password,
39-
credentials.SiteKey,
40-
credentials.ClientSecret
41-
);
42-
43-
var authorization = await response.GetContent<OAuthAuthorizationDto>();
44-
45-
// CR note: missing storing to cache
46-
// but I don't know, I'll rather token be stored in the Manager
47-
48-
return new HttpAuthorization
49-
{
50-
Type = "Bearer",
51-
Credentials = authorization.access_token
52-
};
38+
return new AuthenticationHeaderValue("Bearer", _authAuthorization.access_token);
5339
}
5440
}
5541
}

PowerDms.Api.Rest.Client/PowerDms.Api.Rest.Client/HttpAuthorization.cs

-12
This file was deleted.

PowerDms.Api.Rest.Client/PowerDms.Api.Rest.Client/HttpRequestBuilder.cs

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
using System.Net.Http;
22
using System.Text;
33
using Newtonsoft.Json;
4-
using PowerDms.Api.Rest.Dto;
54

65
namespace PowerDms.Api.Rest.Client
76
{
87
public class HttpRequestBuilder<TResponse>
98
{
10-
public HttpRequestMessage HttpRequestMessage;
9+
// TODO: replace this when .net std 2.1 is out:
10+
// https://github.com/dotnet/corefx/issues/26201
11+
private static string _applicationJson = "application/json";
1112

12-
public Credentials Credentials;
13+
public HttpRequestMessage HttpRequestMessage;
1314

1415
public object Body { get; private set; }
1516

@@ -19,18 +20,12 @@ public HttpRequestBuilder(
1920
HttpRequestMessage = httpRequestMessage;
2021
}
2122

22-
public HttpRequestBuilder<TResponse> AuthenticateWith(
23-
Credentials credentials)
24-
{
25-
Credentials = credentials;
26-
return this;
27-
}
28-
2923
public HttpRequestBuilder<TResponse> AddJsonBody(object body)
3024
{
25+
// NOTE: I defined this here instead of using an extension method, because FakeItEasy can't fake extension methods
3126
Body = body;
3227
var json = JsonConvert.SerializeObject(body);
33-
HttpRequestMessage.Content = new StringContent(json, Encoding.UTF8, "application/json");
28+
HttpRequestMessage.Content = new StringContent(json, Encoding.UTF8, _applicationJson);
3429
return this;
3530
}
3631
}

PowerDms.Api.Rest.Client/PowerDms.Api.Rest.Client/HttpRequestManager.cs

+7-28
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,31 @@ namespace PowerDms.Api.Rest.Client
77
{
88
public class HttpRequestManager
99
{
10-
private readonly HttpClient _HttpClient;
10+
private readonly HttpClient _httpClient;
1111

1212
public readonly PowerDmsRestApiClient PowerDmsRestApiClient;
1313

14-
private IAuthenticationTokenProvider _AuthenticationTokenProvider;
15-
16-
public HttpRequestManager(
17-
HttpClient httpClient,
18-
AuthenticationTokenProvider authenticationTokenProvider)
19-
{
20-
_HttpClient = httpClient;
21-
PowerDmsRestApiClient = new PowerDmsRestApiClient(httpClient);
22-
_AuthenticationTokenProvider = authenticationTokenProvider;
23-
}
14+
private readonly IAuthenticationTokenProvider _authenticationTokenProvider;
2415

2516
public HttpRequestManager(HttpClient httpClient, IAuthenticationTokenProvider authenticationTokenProvider)
2617
{
27-
_HttpClient = httpClient;
18+
_httpClient = httpClient;
2819
PowerDmsRestApiClient = new PowerDmsRestApiClient(httpClient);
29-
_AuthenticationTokenProvider = authenticationTokenProvider;
30-
}
31-
32-
public virtual HttpRequestManager ReplaceAuthenticationTokenProvider(
33-
IAuthenticationTokenProvider authenticationTokenProvider)
34-
{
35-
_AuthenticationTokenProvider = authenticationTokenProvider;
36-
return this;
20+
_authenticationTokenProvider = authenticationTokenProvider;
3721
}
3822

3923
public virtual async Task<HttpResponseMessage> SendAsync<T>(HttpRequestBuilder<T> httpRequestBuilder)
4024
{
4125
// virtual to facilitate faking during unit testing
4226
await AddAuthentication(httpRequestBuilder);
43-
return await _HttpClient.SendAsync(httpRequestBuilder.HttpRequestMessage);
27+
return await _httpClient.SendAsync(httpRequestBuilder.HttpRequestMessage);
4428
}
4529

4630
private async Task<HttpRequestBuilder<T>> AddAuthentication<T>(
4731
HttpRequestBuilder<T> httpRequestBuilder)
4832
{
49-
if (httpRequestBuilder.Credentials != null)
50-
{
51-
httpRequestBuilder.HttpRequestMessage
52-
.AddAccessToken(
53-
await _AuthenticationTokenProvider.GetAccessToken(httpRequestBuilder.Credentials));
54-
55-
}
33+
httpRequestBuilder.HttpRequestMessage
34+
.AddAccessToken(await _authenticationTokenProvider.GetAccessToken());
5635

5736
return httpRequestBuilder;
5837
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using PowerDms.Api.Rest.Client.Clients;
2+
using PowerDms.Api.Rest.Dto;
3+
4+
namespace PowerDms.Api.Rest.Client
5+
{
6+
public class HttpRequestManagerBuilder
7+
{
8+
private string _apiHost;
9+
10+
private string _oAuthHost;
11+
12+
private ApiVersion _version;
13+
14+
private IAuthenticationTokenProvider _authenticationTokenProvider;
15+
16+
public HttpRequestManagerBuilder()
17+
{
18+
_apiHost = RestApiRoutes.PowerDmsRestApiDomain;
19+
_oAuthHost = RestApiRoutes.PowerDmsOAuthDomain;
20+
_version = ApiVersion.Version1;
21+
}
22+
23+
/// <summary>
24+
/// Override the PowerDMS REST API host, normally not needed
25+
/// </summary>
26+
/// <param name="hostDomain"></param>
27+
/// <returns></returns>
28+
public virtual HttpRequestManagerBuilder SetApiHost(string hostDomain)
29+
{
30+
_apiHost = hostDomain;
31+
return this;
32+
}
33+
34+
/// <summary>
35+
/// Override the PowerDMS REST API OAuth server address, normally not needed
36+
/// </summary>
37+
/// <param name="hostDomain"></param>
38+
/// <returns></returns>
39+
public virtual HttpRequestManagerBuilder SetOAuthHost(string hostDomain)
40+
{
41+
_oAuthHost = hostDomain;
42+
return this;
43+
}
44+
45+
public virtual HttpRequestManagerBuilder SetApiVersion(ApiVersion version)
46+
{
47+
_version = version;
48+
return this;
49+
}
50+
51+
public virtual HttpRequestManagerBuilder SetAuthenticationTokenProvider(IAuthenticationTokenProvider authenticationTokenProvider)
52+
{
53+
_authenticationTokenProvider = authenticationTokenProvider;
54+
return this;
55+
}
56+
57+
public virtual HttpRequestManager Build(Credentials credentials)
58+
{
59+
var apiHttpClient = PowerDmsHttpClientFactory.CreateHttpClient(_apiHost, _version);
60+
var oAuthHttpClient = PowerDmsHttpClientFactory.CreateHttpClient(_oAuthHost, null);
61+
62+
var authProvider = _authenticationTokenProvider ??
63+
new AuthenticationTokenProvider(
64+
new OAuthClient(oAuthHttpClient), credentials
65+
);
66+
67+
return new HttpRequestManager(apiHttpClient, authProvider);
68+
}
69+
}
70+
}

PowerDms.Api.Rest.Client/PowerDms.Api.Rest.Client/HttpRequestManagerFactory.cs

-17
This file was deleted.

PowerDms.Api.Rest.Client/PowerDms.Api.Rest.Client/HttpRequestMessageExtensions.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ public static class HttpRequestMessageExtensions
77
{
88
public static HttpRequestMessage AddAccessToken(
99
this HttpRequestMessage httpRequestMessage,
10-
HttpAuthorization httpAuthorization)
10+
AuthenticationHeaderValue httpAuthorization)
1111
{
12-
httpRequestMessage.Headers.Authorization =
13-
new AuthenticationHeaderValue(httpAuthorization.Type, httpAuthorization.Credentials);
12+
httpRequestMessage.Headers.Authorization = httpAuthorization;
1413

1514
return httpRequestMessage;
1615
}

0 commit comments

Comments
 (0)