From dfcd93f31a11af6769b2adf1cc4b8586f19b982b Mon Sep 17 00:00:00 2001 From: "Alex T. Silverstein" Date: Mon, 8 Aug 2022 17:48:32 -0400 Subject: [PATCH 1/3] Modified to support Bearer token authentication instead of non-private API key --- src/Associations/HubSpotAssociationsClient.cs | 12 ++++++------ src/Company/HubSpotCompanyClient.cs | 14 ++++++-------- src/Contact/HubSpotContactClient.cs | 12 ++++++------ src/Core/HubSpotBaseClient.cs | 17 ++++++++++------- src/Deal/HubSpotDealClient.cs | 12 ++++++------ src/LineItem/HubSpotLineItemClient.cs | 12 ++++++------ .../HubSpotListOfContactsClient.cs | 12 ++++++------ src/Owner/HubSpotOwnerClient.cs | 14 +++++++------- 8 files changed, 53 insertions(+), 52 deletions(-) diff --git a/src/Associations/HubSpotAssociationsClient.cs b/src/Associations/HubSpotAssociationsClient.cs index 6ea9b89..9507042 100644 --- a/src/Associations/HubSpotAssociationsClient.cs +++ b/src/Associations/HubSpotAssociationsClient.cs @@ -21,14 +21,14 @@ public class HubSpotAssociationsClient : HubSpotBaseClient, IHubSpotAssociations /// /// /// - /// + /// 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) { } @@ -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 /// - /// Your API key - public HubSpotAssociationsClient(string apiKey) + /// Your API token + public HubSpotAssociationsClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } /// diff --git a/src/Company/HubSpotCompanyClient.cs b/src/Company/HubSpotCompanyClient.cs index bae1de5..caddc68 100644 --- a/src/Company/HubSpotCompanyClient.cs +++ b/src/Company/HubSpotCompanyClient.cs @@ -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; @@ -24,14 +22,14 @@ public class HubSpotCompanyClient : HubSpotBaseClient, IHubSpotCompanyClient /// /// /// - /// + /// public HubSpotCompanyClient( IRapidHttpClient httpClient, ILogger logger, RequestSerializer serializer, string hubSpotBaseUrl, - string apiKey) - : base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey) + string apiToken) + : base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken) { } @@ -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 /// - /// Your API key - public HubSpotCompanyClient(string apiKey) + /// Your API token + public HubSpotCompanyClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } /// diff --git a/src/Contact/HubSpotContactClient.cs b/src/Contact/HubSpotContactClient.cs index e75568d..6238958 100644 --- a/src/Contact/HubSpotContactClient.cs +++ b/src/Contact/HubSpotContactClient.cs @@ -23,14 +23,14 @@ public class HubSpotContactClient : HubSpotBaseClient, IHubSpotContactClient /// /// /// - /// + /// public HubSpotContactClient( IRapidHttpClient httpClient, ILogger logger, RequestSerializer serializer, string hubSpotBaseUrl, - string apiKey) - : base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey) + string apiToken) + : base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken) { } @@ -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 /// - /// Your API key - public HubSpotContactClient(string apiKey) + /// Your API token + public HubSpotContactClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } /// diff --git a/src/Core/HubSpotBaseClient.cs b/src/Core/HubSpotBaseClient.cs index 3ae1513..df84ebf 100644 --- a/src/Core/HubSpotBaseClient.cs +++ b/src/Core/HubSpotBaseClient.cs @@ -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; @@ -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; } /// @@ -252,13 +253,12 @@ await SendRequestAsync( /// HTTP method to use for the request /// Optional json to send with the request /// Func to handle deserialization of data when the request goes well - /// A deserialized entity with data when things go well, exceptionns otherwise + /// A deserialized entity with data when things go well, exceptions otherwise protected async Task SendRequestAsync(string absoluteUriPath, HttpMethod httpMethod, string json, Func deserializeFunc) { - var fullUrl = $"{HubSpotBaseUrl}{absoluteUriPath}" - .SetQueryParam("hapikey", _apiKey); - + var fullUrl = $"{HubSpotBaseUrl}{absoluteUriPath}"; + Logger.LogDebug("Full url: '{0}'", fullUrl); var request = new HttpRequestMessage @@ -266,6 +266,9 @@ protected async Task SendRequestAsync(string absoluteUriPath, HttpMethod h Method = httpMethod, RequestUri = new Uri(fullUrl) }; + + request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _apiToken); + if (!string.IsNullOrWhiteSpace(json)) { request.Content = new JsonContent(json); diff --git a/src/Deal/HubSpotDealClient.cs b/src/Deal/HubSpotDealClient.cs index a9a4a2b..1635761 100644 --- a/src/Deal/HubSpotDealClient.cs +++ b/src/Deal/HubSpotDealClient.cs @@ -23,14 +23,14 @@ public class HubSpotDealClient : HubSpotBaseClient, IHubSpotDealClient /// /// /// - /// + /// public HubSpotDealClient( IRapidHttpClient httpClient, ILogger logger, RequestSerializer serializer, string hubSpotBaseUrl, - string apiKey) - : base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey) + string apiToken) + : base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken) { } @@ -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 /// - /// Your API key - public HubSpotDealClient(string apiKey) + /// Your API token + public HubSpotDealClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } /// diff --git a/src/LineItem/HubSpotLineItemClient.cs b/src/LineItem/HubSpotLineItemClient.cs index 0719c9a..f2012d7 100644 --- a/src/LineItem/HubSpotLineItemClient.cs +++ b/src/LineItem/HubSpotLineItemClient.cs @@ -23,14 +23,14 @@ public class HubSpotLineItemClient : HubSpotBaseClient, IHubSpotLineItemClient /// /// /// - /// + /// public HubSpotLineItemClient( IRapidHttpClient httpClient, ILogger logger, RequestSerializer serializer, string hubSpotBaseUrl, - string apiKey) - : base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey) + string apiToken) + : base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken) { } @@ -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 /// - /// Your API key - public HubSpotLineItemClient(string apiKey) + /// Your API token + public HubSpotLineItemClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } public Task CreateAsync(ILineItemHubSpotEntity entity) where T : IHubSpotEntity, new() diff --git a/src/ListOfContacts/HubSpotListOfContactsClient.cs b/src/ListOfContacts/HubSpotListOfContactsClient.cs index 8503fd7..5aeb635 100644 --- a/src/ListOfContacts/HubSpotListOfContactsClient.cs +++ b/src/ListOfContacts/HubSpotListOfContactsClient.cs @@ -22,14 +22,14 @@ public class HubSpotListOfContactsClient : HubSpotBaseClient, IHubSpotListOfCont /// /// /// - /// + /// public HubSpotListOfContactsClient( IRapidHttpClient httpClient, ILogger logger, RequestSerializer serializer, string hubSpotBaseUrl, - string apiKey) - : base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey) + string apiToken) + : base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken) { } @@ -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 /// - /// Your API key - public HubSpotListOfContactsClient(string apiKey) + /// Your API token + public HubSpotListOfContactsClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } diff --git a/src/Owner/HubSpotOwnerClient.cs b/src/Owner/HubSpotOwnerClient.cs index 93ccec8..3168ca2 100644 --- a/src/Owner/HubSpotOwnerClient.cs +++ b/src/Owner/HubSpotOwnerClient.cs @@ -21,14 +21,14 @@ public class HubSpotOwnerClient : HubSpotBaseClient, IHubSpotOwnerClient /// /// /// - /// + /// public HubSpotOwnerClient( IRapidHttpClient httpClient, ILogger logger, RequestSerializer serializer, string hubSpotBaseUrl, - string apiKey) - : base(httpClient, logger, serializer, hubSpotBaseUrl, apiKey) + string apiToken) + : base(httpClient, logger, serializer, hubSpotBaseUrl, apiToken) { } @@ -38,16 +38,16 @@ public HubSpotOwnerClient( /// /// 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 /// - /// Your API key - public HubSpotOwnerClient(string apiKey) + /// Your API token + public HubSpotOwnerClient(string apiToken) : base( new RealRapidHttpClient(new HttpClient()), NoopLoggerFactory.Get(), new RequestSerializer(new RequestDataConverter(NoopLoggerFactory.Get())), "https://api.hubapi.com", - apiKey) + apiToken) { } /// From 8c346c26d09702754efb49ead2ccd694792a143d Mon Sep 17 00:00:00 2001 From: "Alex T. Silverstein" Date: Tue, 27 Sep 2022 13:18:32 -0400 Subject: [PATCH 2/3] Changed .NET target to 5 --- src/hubspot-client.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hubspot-client.csproj b/src/hubspot-client.csproj index 4e55a46..2495a51 100644 --- a/src/hubspot-client.csproj +++ b/src/hubspot-client.csproj @@ -1,6 +1,5 @@ - netstandard2.0;net461;net5.0;net6.0 0.21.0 Skarp.HubSpotClient Skarp.HubSpotClient @@ -24,6 +23,7 @@ embedded + net5.0 From 6308c25a39c90015716075171d854fe045d3c59a Mon Sep 17 00:00:00 2001 From: "Alex T. Silverstein" Date: Tue, 27 Sep 2022 13:54:58 -0400 Subject: [PATCH 3/3] Changed .NET Framework version to 5 --- test/functional/functional.csproj | 4 ++-- test/integration/integration.csproj | 4 ++-- test/unit/unit.csproj | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/test/functional/functional.csproj b/test/functional/functional.csproj index 8a69e19..cfed6f3 100644 --- a/test/functional/functional.csproj +++ b/test/functional/functional.csproj @@ -1,12 +1,12 @@  - net6.0 + net5.0 false Skarp.HubSpotClient.FunctionalTests Skarp.HubSpotClient.FunctionalTests - + diff --git a/test/integration/integration.csproj b/test/integration/integration.csproj index f6261b0..2bb4c5b 100644 --- a/test/integration/integration.csproj +++ b/test/integration/integration.csproj @@ -1,10 +1,10 @@  - net6.0 + net5.0 false - + diff --git a/test/unit/unit.csproj b/test/unit/unit.csproj index d4cd625..9bc79d4 100644 --- a/test/unit/unit.csproj +++ b/test/unit/unit.csproj @@ -1,13 +1,13 @@  - net6.0 + net5.0 false Skarp.HubSpotClient.UnitTest Skarp.HubSpotClient.UnitTest - +