Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In light of private apps requirement, modified code to support Bearer token authentication #93

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Associations/HubSpotAssociationsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class HubSpotAssociationsClient : HubSpotBaseClient, IHubSpotAssociations
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotAssociationsClient(
IRapidHttpClient httpClient,
ILogger logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -40,14 +40,14 @@ public HubSpotAssociationsClient(
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotAssociationsClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotAssociationsClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }

/// <summary>
Expand Down
14 changes: 6 additions & 8 deletions src/Company/HubSpotCompanyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
using Flurl;
using Microsoft.Extensions.Logging;
using RapidCore.Network;
using Skarp.HubSpotClient.Common.Dto.Properties;
using Skarp.HubSpotClient.Common.Interfaces;
using Skarp.HubSpotClient.Company.Dto;
using Skarp.HubSpotClient.Company.Interfaces;
using Skarp.HubSpotClient.Core;
Expand All @@ -24,14 +22,14 @@ public class HubSpotCompanyClient : HubSpotBaseClient, IHubSpotCompanyClient
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotCompanyClient(
IRapidHttpClient httpClient,
ILogger<HubSpotCompanyClient> logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -43,14 +41,14 @@ public HubSpotCompanyClient(
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotCompanyClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotCompanyClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/Contact/HubSpotContactClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class HubSpotContactClient : HubSpotBaseClient, IHubSpotContactClient
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotContactClient(
IRapidHttpClient httpClient,
ILogger<HubSpotContactClient> logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -42,14 +42,14 @@ public HubSpotContactClient(
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotContactClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotContactClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }

/// <summary>
Expand Down
17 changes: 10 additions & 7 deletions src/Core/HubSpotBaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Flurl;
using Microsoft.Extensions.Logging;
Expand All @@ -19,20 +20,20 @@ public abstract class HubSpotBaseClient
protected readonly RequestSerializer _serializer;
protected readonly string HubSpotBaseUrl;

private readonly string _apiKey;
private readonly string _apiToken;

protected HubSpotBaseClient(
IRapidHttpClient httpClient,
ILogger logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
string apiToken)
{
HttpClient = httpClient;
Logger = logger;
_serializer = serializer;
HubSpotBaseUrl = hubSpotBaseUrl.TrimEnd('/');
_apiKey = apiKey;
_apiToken = apiToken;
}

/// <summary>
Expand Down Expand Up @@ -252,20 +253,22 @@ await SendRequestAsync<T>(
/// <param name="httpMethod">HTTP method to use for the request</param>
/// <param name="json">Optional json to send with the request</param>
/// <param name="deserializeFunc">Func to handle deserialization of data when the request goes well</param>
/// <returns>A deserialized entity with data when things go well, exceptionns otherwise</returns>
/// <returns>A deserialized entity with data when things go well, exceptions otherwise</returns>
protected async Task<T> SendRequestAsync<T>(string absoluteUriPath, HttpMethod httpMethod, string json,
Func<string, T> deserializeFunc)
{
var fullUrl = $"{HubSpotBaseUrl}{absoluteUriPath}"
.SetQueryParam("hapikey", _apiKey);

var fullUrl = $"{HubSpotBaseUrl}{absoluteUriPath}";

Logger.LogDebug("Full url: '{0}'", fullUrl);

var request = new HttpRequestMessage
{
Method = httpMethod,
RequestUri = new Uri(fullUrl)
};

request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _apiToken);

Comment on lines +269 to +271
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to make this a non-breaking change. I know that Hubspot will effectively stop allowing the apiKey from 30'th Nov, but since we're still in 0.x semver, the breaking change might be missed by people upgrading.

It seems like the new private app keys start with pat-, so I think that we could do a check on this and then either attach the Authorization header, or use the SetQueryParam to use the legacy auth mode. Which should then issue a Warning via the logger - that the legacy auth is used which will be removed Dec 2022.

if (!string.IsNullOrWhiteSpace(json))
{
request.Content = new JsonContent(json);
Expand Down
12 changes: 6 additions & 6 deletions src/Deal/HubSpotDealClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class HubSpotDealClient : HubSpotBaseClient, IHubSpotDealClient
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotDealClient(
IRapidHttpClient httpClient,
ILogger<HubSpotDealClient> logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -42,14 +42,14 @@ public HubSpotDealClient(
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotDealClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotDealClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }

/// <summary>
Expand Down
12 changes: 6 additions & 6 deletions src/LineItem/HubSpotLineItemClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public class HubSpotLineItemClient : HubSpotBaseClient, IHubSpotLineItemClient
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotLineItemClient(
IRapidHttpClient httpClient,
ILogger<HubSpotLineItemClient> logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -42,14 +42,14 @@ public HubSpotLineItemClient(
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotLineItemClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotLineItemClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }

public Task<T> CreateAsync<T>(ILineItemHubSpotEntity entity) where T : IHubSpotEntity, new()
Expand Down
12 changes: 6 additions & 6 deletions src/ListOfContacts/HubSpotListOfContactsClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public class HubSpotListOfContactsClient : HubSpotBaseClient, IHubSpotListOfCont
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotListOfContactsClient(
IRapidHttpClient httpClient,
ILogger<HubSpotListOfContactsClient> logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -41,14 +41,14 @@ public HubSpotListOfContactsClient(
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotListOfContactsClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotListOfContactsClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }


Expand Down
14 changes: 7 additions & 7 deletions src/Owner/HubSpotOwnerClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ public class HubSpotOwnerClient : HubSpotBaseClient, IHubSpotOwnerClient
/// <param name="logger"></param>
/// <param name="serializer"></param>
/// <param name="hubSpotBaseUrl"></param>
/// <param name="apiKey"></param>
/// <param name="apiToken"></param>
public HubSpotOwnerClient(
IRapidHttpClient httpClient,
ILogger<HubSpotOwnerClient> logger,
RequestSerializer serializer,
string hubSpotBaseUrl,
string apiKey)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey)
string apiToken)
: base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken)
{
}

Expand All @@ -38,16 +38,16 @@ public HubSpotOwnerClient(
/// <remarks>
/// This constructor creates a HubSpotOwnerClient using "real" dependencies that will send requests
/// via the network - if you wish to have support for functional tests and mocking use the "eager" constructor
/// that takes in all underlying dependecies
/// that takes in all underlying dependencies
/// </remarks>
/// <param name="apiKey">Your API key</param>
public HubSpotOwnerClient(string apiKey)
/// <param name="apiToken">Your API token</param>
public HubSpotOwnerClient(string apiToken)
: base(
new RealRapidHttpClient(new HttpClient()),
NoopLoggerFactory.Get(),
new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get<RequestDataConverter>())),
"https://api.hubapi.com",
apiKey)
apiToken)
{ }

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/hubspot-client.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461;net5.0;net6.0</TargetFrameworks>
<Version>0.21.0</Version>
<AssemblyName>Skarp.HubSpotClient</AssemblyName>
<RootNamespace>Skarp.HubSpotClient</RootNamespace>
Expand All @@ -24,6 +23,7 @@

<!-- Recommended: Embed symbols containing Source Link in the main file (exe/dll) -->
<DebugType>embedded</DebugType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
Expand Down
4 changes: 2 additions & 2 deletions test/functional/functional.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>Skarp.HubSpotClient.FunctionalTests</AssemblyName>
<RootNamespace>Skarp.HubSpotClient.FunctionalTests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="RapidCore.Xunit" Version="0.27.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
4 changes: 2 additions & 2 deletions test/integration/integration.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="RapidCore.Xunit" Version="0.27.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down
4 changes: 2 additions & 2 deletions test/unit/unit.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
<AssemblyName>Skarp.HubSpotClient.UnitTest</AssemblyName>
<RootNamespace>Skarp.HubSpotClient.UnitTest</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="FakeItEasy" Version="7.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="RapidCore.Xunit" Version="0.27.0" />
<PackageReference Include="xunit" Version="2.4.1" />
Expand Down