Skip to content

Commit

Permalink
Fix issue #120
Browse files Browse the repository at this point in the history
  • Loading branch information
richardrandak committed Nov 20, 2020
1 parent 4a9071c commit 5e141e5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
15 changes: 14 additions & 1 deletion FortnoxAPILibrary.Tests/BaseConnectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public void Test_Find_ParamsAdded()
{
var connector = new CustomerConnector
{
Search = new CustomerSearch(){
Search = new CustomerSearch()
{
Name = "TestName",
City = "TestCity",
LastModified = new DateTime(2000, 01, 01, 20, 10, 05), //2000-01-20 20:10:05
Expand Down Expand Up @@ -198,5 +199,17 @@ private static int GetNeededPages(int totalSize, int pageSize)
{
return (int) Math.Ceiling(totalSize / (float) pageSize);
}

[Ignore("Requires new authorization code.")]
[TestMethod]
public void TestAuth()
{
var authorizationCode = "Placeholder";
var authConnector = new AuthorizationConnector();
var token = authConnector.GetAccessToken(authorizationCode, TestCredentials.Client_Secret);

MyAssert.HasNoError(authConnector);
Assert.IsNotNull(token);
}
}
}
2 changes: 1 addition & 1 deletion FortnoxAPILibrary.Tests/MyAssert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static class MyAssert
/// Checks if the last request was successfull
/// </summary>
/// <param name="connector"></param>
public static void HasNoError(IConnector connector)
public static void HasNoError(IBaseConnector connector)
{
if (connector.HasError)
throw new AssertFailedException($"Request failed due to '{connector.Error.Message}'.");
Expand Down
9 changes: 6 additions & 3 deletions FortnoxAPILibrary/BaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ public BaseClient()
UseRateLimiter = ConnectionSettings.UseRateLimiter;
}

public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool addAuthHeaders = true)
{
request.Headers.Add(AccessTokenHeader, AccessToken);
request.Headers.Add(ClientSecretHeader, ClientSecret);
if (addAuthHeaders)
{
request.Headers.Add(AccessTokenHeader, AccessToken);
request.Headers.Add(ClientSecretHeader, ClientSecret);
}

if (UseRateLimiter)
await Throttle();
Expand Down
14 changes: 7 additions & 7 deletions FortnoxAPILibrary/Connectors/AuthorizationConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,19 @@ public string GetAccessToken(string authorizationCode, string clientSecret)
public async Task<string> GetAccessTokenAsync(string authorizationCode, string clientSecret)
{
if (string.IsNullOrEmpty(authorizationCode))
return string.Empty;
return null;
if (string.IsNullOrEmpty(clientSecret))
return string.Empty;
return null;

try
{
var wr = SetupRequest(BaseUrl, authorizationCode, clientSecret);
using var response = await HttpClient.SendAsync(wr).ConfigureAwait(false);
using var response = await HttpClient.SendAsync(wr, false).ConfigureAwait(false);

if (!response.IsSuccessStatusCode)
{
Error = ErrorHandler.HandleError(response);
return string.Empty;
return null;
}

var json = response.Content.ReadAsStringAsync().Result;
Expand All @@ -51,7 +51,7 @@ public async Task<string> GetAccessTokenAsync(string authorizationCode, string c
catch (HttpRequestException we)
{
Error = ErrorHandler.HandleConnectionException(we);
return string.Empty;
return null;
}
}

Expand All @@ -60,8 +60,8 @@ private HttpRequestMessage SetupRequest(string requestUriString, string authoriz
Error = null;

var wr = new HttpRequestMessage(HttpMethod.Get, requestUriString);
wr.Headers.Add("authorization-code", authorizationCode);
wr.Headers.Add("client-secret", clientSecret);
wr.Headers.Add("Authorization-Code", authorizationCode);
wr.Headers.Add("Client-Secret", clientSecret);
wr.Headers.Add("Accept", "application/json");
return wr;
}
Expand Down
2 changes: 1 addition & 1 deletion FortnoxAPILibrary/FortnoxAPILibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
<PackageId>Fortnox.NET.SDK</PackageId>
<Version>3.5.0</Version>
<Version>3.5.1</Version>
<Authors>Richard Randak</Authors>
<Company>Softwerk AB</Company>
<Description>Official .NET SDK for Fortnox API. This package is on behalf of Fortnox AB developed and maintained by Softwerk AB. For more information please visit our repository on Github.</Description>
Expand Down

0 comments on commit 5e141e5

Please sign in to comment.