-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e904559
commit 350030f
Showing
16 changed files
with
295 additions
and
227 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using FortnoxAPILibrary.Serialization; | ||
|
||
namespace FortnoxAPILibrary | ||
{ | ||
public class AdaptableSerializer | ||
{ | ||
private readonly JsonEntitySerializer serializer; | ||
|
||
public Func<string, string> FixRequestContent; //Needed for fixing irregular json requests //TODO: rename to "adapt serialization" //post-serialization | ||
public Func<string, string> FixResponseContent; //Needed for fixing irregular json responses //TODO: rename to adapt deserialization //pre-deserialization | ||
|
||
public AdaptableSerializer() | ||
{ | ||
serializer = new JsonEntitySerializer(); | ||
} | ||
|
||
public string Serialize<T>(T entity) | ||
{ | ||
var json = serializer.Serialize(entity); | ||
|
||
if (FixRequestContent != null) | ||
json = FixRequestContent(json); | ||
|
||
FixRequestContent = null; | ||
return json; | ||
} | ||
|
||
public T Deserialize<T>(string content) | ||
{ | ||
try | ||
{ | ||
if (FixResponseContent != null) | ||
content = FixResponseContent(content); | ||
FixResponseContent = null; | ||
return serializer.Deserialize<T>(content); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new SerializationException("An error occured while deserializing the response. Check ResponseContent.", e.InnerException); | ||
} | ||
} | ||
} | ||
} |
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,87 @@ | ||
using System; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using ComposableAsync; | ||
using RateLimiter; | ||
|
||
namespace FortnoxAPILibrary | ||
{ | ||
public class BaseClient | ||
{ | ||
private const string AccessTokenHeader = "Access-Token"; | ||
private const string ClientSecretHeader = "Client-Secret"; | ||
|
||
/// <summary> | ||
/// Http client used under-the-hood for all request (except file upload due to a server-side limitation) | ||
/// </summary> | ||
private HttpClient HttpClient { get; } | ||
|
||
/// <summary> | ||
/// Optional Fortnox Access Token, if used it will override the static version. | ||
/// </summary> | ||
public string AccessToken { get; set; } | ||
|
||
/// <summary> | ||
/// Optional Fortnox Client Secret, if used it will override the static version. | ||
/// </summary> | ||
public string ClientSecret { get; set; } | ||
|
||
/// <summary> | ||
/// Timeout of requests sent to the Fortnox API in miliseconds | ||
/// </summary> | ||
public int Timeout | ||
{ | ||
get => (int) HttpClient.Timeout.TotalMilliseconds; | ||
set => HttpClient.Timeout = TimeSpan.FromMilliseconds(value); | ||
} | ||
|
||
/// <summary> | ||
/// Base fortnox server URI | ||
/// </summary> | ||
public string BaseUrl | ||
{ | ||
get => HttpClient.BaseAddress.AbsoluteUri; | ||
set => HttpClient.BaseAddress = new Uri(value); | ||
} | ||
|
||
public bool UseRateLimiter { get; set; } | ||
|
||
private const int LimitPerSecond = 3; | ||
private static readonly TimeLimiter RateLimiter = TimeLimiter.GetFromMaxCountByInterval(LimitPerSecond, TimeSpan.FromSeconds(1)); | ||
|
||
public BaseClient() | ||
{ | ||
HttpClient = new HttpClient(); | ||
|
||
AccessToken = ConnectionCredentials.AccessToken; | ||
ClientSecret = ConnectionCredentials.ClientSecret; | ||
BaseUrl = ConnectionSettings.FortnoxAPIServer; | ||
Timeout = 30 * 10000; | ||
UseRateLimiter = ConnectionSettings.UseRateLimiter; | ||
} | ||
|
||
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request) | ||
{ | ||
request.Headers.Add(AccessTokenHeader, AccessToken); | ||
request.Headers.Add(ClientSecretHeader, ClientSecret); | ||
|
||
if (UseRateLimiter) | ||
await RateLimiter; //throttle every call to Fortnox | ||
|
||
return await HttpClient.SendAsync(request).ConfigureAwait(false); | ||
} | ||
|
||
public async Task<HttpWebResponse> SendAsync(HttpWebRequest request) | ||
{ | ||
request.Headers.Add(AccessTokenHeader, AccessToken); | ||
request.Headers.Add(ClientSecretHeader, ClientSecret); | ||
request.Timeout = Timeout; | ||
|
||
if (UseRateLimiter) | ||
await RateLimiter; //throttle every call to Fortnox | ||
|
||
return (HttpWebResponse) await request.GetResponseAsync().ConfigureAwait(false); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.