Skip to content

Commit 5e141e5

Browse files
committed
Fix issue #120
1 parent 4a9071c commit 5e141e5

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

FortnoxAPILibrary.Tests/BaseConnectorTests.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public void Test_Find_ParamsAdded()
2323
{
2424
var connector = new CustomerConnector
2525
{
26-
Search = new CustomerSearch(){
26+
Search = new CustomerSearch()
27+
{
2728
Name = "TestName",
2829
City = "TestCity",
2930
LastModified = new DateTime(2000, 01, 01, 20, 10, 05), //2000-01-20 20:10:05
@@ -198,5 +199,17 @@ private static int GetNeededPages(int totalSize, int pageSize)
198199
{
199200
return (int) Math.Ceiling(totalSize / (float) pageSize);
200201
}
202+
203+
[Ignore("Requires new authorization code.")]
204+
[TestMethod]
205+
public void TestAuth()
206+
{
207+
var authorizationCode = "Placeholder";
208+
var authConnector = new AuthorizationConnector();
209+
var token = authConnector.GetAccessToken(authorizationCode, TestCredentials.Client_Secret);
210+
211+
MyAssert.HasNoError(authConnector);
212+
Assert.IsNotNull(token);
213+
}
201214
}
202215
}

FortnoxAPILibrary.Tests/MyAssert.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public static class MyAssert
99
/// Checks if the last request was successfull
1010
/// </summary>
1111
/// <param name="connector"></param>
12-
public static void HasNoError(IConnector connector)
12+
public static void HasNoError(IBaseConnector connector)
1313
{
1414
if (connector.HasError)
1515
throw new AssertFailedException($"Request failed due to '{connector.Error.Message}'.");

FortnoxAPILibrary/BaseClient.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,13 @@ public BaseClient()
4848
UseRateLimiter = ConnectionSettings.UseRateLimiter;
4949
}
5050

51-
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request)
51+
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, bool addAuthHeaders = true)
5252
{
53-
request.Headers.Add(AccessTokenHeader, AccessToken);
54-
request.Headers.Add(ClientSecretHeader, ClientSecret);
53+
if (addAuthHeaders)
54+
{
55+
request.Headers.Add(AccessTokenHeader, AccessToken);
56+
request.Headers.Add(ClientSecretHeader, ClientSecret);
57+
}
5558

5659
if (UseRateLimiter)
5760
await Throttle();

FortnoxAPILibrary/Connectors/AuthorizationConnector.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ public string GetAccessToken(string authorizationCode, string clientSecret)
2727
public async Task<string> GetAccessTokenAsync(string authorizationCode, string clientSecret)
2828
{
2929
if (string.IsNullOrEmpty(authorizationCode))
30-
return string.Empty;
30+
return null;
3131
if (string.IsNullOrEmpty(clientSecret))
32-
return string.Empty;
32+
return null;
3333

3434
try
3535
{
3636
var wr = SetupRequest(BaseUrl, authorizationCode, clientSecret);
37-
using var response = await HttpClient.SendAsync(wr).ConfigureAwait(false);
37+
using var response = await HttpClient.SendAsync(wr, false).ConfigureAwait(false);
3838

3939
if (!response.IsSuccessStatusCode)
4040
{
4141
Error = ErrorHandler.HandleError(response);
42-
return string.Empty;
42+
return null;
4343
}
4444

4545
var json = response.Content.ReadAsStringAsync().Result;
@@ -51,7 +51,7 @@ public async Task<string> GetAccessTokenAsync(string authorizationCode, string c
5151
catch (HttpRequestException we)
5252
{
5353
Error = ErrorHandler.HandleConnectionException(we);
54-
return string.Empty;
54+
return null;
5555
}
5656
}
5757

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

6262
var wr = new HttpRequestMessage(HttpMethod.Get, requestUriString);
63-
wr.Headers.Add("authorization-code", authorizationCode);
64-
wr.Headers.Add("client-secret", clientSecret);
63+
wr.Headers.Add("Authorization-Code", authorizationCode);
64+
wr.Headers.Add("Client-Secret", clientSecret);
6565
wr.Headers.Add("Accept", "application/json");
6666
return wr;
6767
}

FortnoxAPILibrary/FortnoxAPILibrary.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<LangVersion>8</LangVersion>
66
<PackageId>Fortnox.NET.SDK</PackageId>
7-
<Version>3.5.0</Version>
7+
<Version>3.5.1</Version>
88
<Authors>Richard Randak</Authors>
99
<Company>Softwerk AB</Company>
1010
<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>

0 commit comments

Comments
 (0)