-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Request builder added. StarlingClient wrapper
- Loading branch information
1 parent
b3d98ca
commit d31886e
Showing
12 changed files
with
270 additions
and
127 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Rowell Heria | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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 @@ | ||
# Starling Bank |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using StarlingBank.Entities.SavingsGoals; | ||
|
||
namespace StarlingBank.Api | ||
{ | ||
public class StarlingClient : IStarlingClient, IDisposable | ||
{ | ||
public StarlingClient() | ||
{ | ||
this.RequestBuilder = new StarlingRequestBuilder(); | ||
} | ||
|
||
private StarlingRequestBuilder RequestBuilder { get; } | ||
|
||
public string BaseUrl { get; set; } | ||
|
||
public string AccessToken { get; set; } | ||
|
||
public void Dispose() | ||
{ | ||
this.RequestBuilder.Dispose(); | ||
} | ||
|
||
public Task<(HttpStatusCode status, CreateOrUpdateSavingsGoalResponse response)> CreateGoal(SavingsGoalRequest goal) | ||
{ | ||
var goalId = Guid.NewGuid(); | ||
|
||
return this.RequestBuilder | ||
.WithUrl($"{this.BaseUrl}v1/savings-goals/{goalId}") | ||
.WithMethod(HttpMethod.Put) | ||
.WithHeader("Authorization", $"Bearer {this.AccessToken}") | ||
.WithBody(goal) | ||
.SendAsync<CreateOrUpdateSavingsGoalResponse>(); | ||
} | ||
|
||
public Task<(HttpStatusCode status, SavingsGoal goal)> GetGoal(Guid goalId) | ||
{ | ||
return this.RequestBuilder | ||
.WithUrl($"{this.BaseUrl}v1/savings-goals/{goalId}") | ||
.WithMethod(HttpMethod.Get) | ||
.WithHeader("Authorization", $"Bearer {this.AccessToken}") | ||
.SendAsync<SavingsGoal>(); | ||
} | ||
|
||
public Task<(HttpStatusCode status, CreateOrUpdateSavingsGoalResponse response)> AddMoney(Guid goalId, TopUpRequest topup) | ||
{ | ||
var transferId = Guid.NewGuid(); | ||
|
||
return this.RequestBuilder | ||
.WithUrl($"{this.BaseUrl}v1/savings-goals/{goalId}/add-money/{transferId}") | ||
.WithMethod(HttpMethod.Put) | ||
.WithHeader("Authorization", $"Bearer {this.AccessToken}") | ||
.WithBody(topup) | ||
.SendAsync<CreateOrUpdateSavingsGoalResponse>(); | ||
} | ||
} | ||
} |
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,119 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Serialization; | ||
|
||
namespace StarlingBank.Api | ||
{ | ||
public class StarlingRequestBuilder : IDisposable, IStarlingRequestBuilder, IStarlingRequestBuilderMethod, IStarlingRequestBuilderHeadersBodySend | ||
{ | ||
public StarlingRequestBuilder() | ||
{ | ||
this.Client = new HttpClient(); | ||
} | ||
|
||
private StarlingRequestBuilder(HttpClient client, HttpRequestMessage request) | ||
{ | ||
this.Client = client; | ||
this.Request = request; | ||
} | ||
|
||
protected HttpClient Client { get; } | ||
|
||
protected HttpRequestMessage Request { get; } | ||
|
||
private JsonSerializerSettings SerializerSettings { get; } = new JsonSerializerSettings | ||
{ | ||
ContractResolver = new CamelCasePropertyNamesContractResolver() | ||
}; | ||
|
||
|
||
public IStarlingRequestBuilderMethod WithUrl(string url) | ||
{ | ||
var request = new HttpRequestMessage | ||
{ | ||
RequestUri = new Uri(url) | ||
}; | ||
|
||
return new StarlingRequestBuilder(this.Client, request); | ||
} | ||
|
||
public IStarlingRequestBuilderHeadersBodySend WithMethod(HttpMethod method) | ||
{ | ||
this.Request.Method = method; | ||
|
||
return this; | ||
} | ||
|
||
public IStarlingRequestBuilderHeadersBodySend WithHeader(string header, string value) | ||
{ | ||
this.Request.Headers.Add(header, value); | ||
|
||
return this; | ||
} | ||
|
||
public IStarlingRequestBuilderHeadersBodySend WithHeaders(params KeyValuePair<string, string>[] headers) | ||
{ | ||
foreach (var kvp in headers) | ||
{ | ||
this.Request.Headers.Add(kvp.Key, kvp.Value); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
public IStarlingRequestBuilderHeadersBodySend WithBody<T>(T body) where T : class | ||
{ | ||
var json = JsonConvert.SerializeObject(body, this.SerializerSettings); | ||
var content = new StringContent(json, Encoding.UTF8, "application/json"); | ||
this.Request.Content = content; | ||
|
||
return this; | ||
} | ||
|
||
public async Task<(HttpStatusCode code, TResult result)> SendAsync<TResult>() | ||
{ | ||
var response = await this.Client.SendAsync(this.Request); | ||
var result = await response.Content.ReadAsAsync<TResult>(); | ||
|
||
return (response.StatusCode, result); | ||
} | ||
|
||
public Task<HttpResponseMessage> SendAsync() | ||
{ | ||
return this.Client.SendAsync(this.Request); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this.Client?.Dispose(); | ||
} | ||
} | ||
|
||
public interface IStarlingRequestBuilder | ||
{ | ||
IStarlingRequestBuilderMethod WithUrl(string url); | ||
} | ||
|
||
public interface IStarlingRequestBuilderMethod | ||
{ | ||
IStarlingRequestBuilderHeadersBodySend WithMethod(HttpMethod method); | ||
} | ||
|
||
public interface IStarlingRequestBuilderHeadersBodySend | ||
{ | ||
IStarlingRequestBuilderHeadersBodySend WithHeader(string header, string value); | ||
|
||
IStarlingRequestBuilderHeadersBodySend WithHeaders(params KeyValuePair<string, string>[] headers); | ||
|
||
IStarlingRequestBuilderHeadersBodySend WithBody<T>(T body) where T : class; | ||
|
||
Task<(HttpStatusCode code, TResult result)> SendAsync<TResult>(); | ||
|
||
Task<HttpResponseMessage> SendAsync(); | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.