Skip to content

Commit

Permalink
Merge pull request #37 from rossjcooper/feature/new-hubspot-api
Browse files Browse the repository at this point in the history
Upgrade to use new Hubspot API client package
  • Loading branch information
rossjcooper authored Jun 26, 2023
2 parents 6af7827 + 961fe31 commit 6a36194
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 102 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Loads in during testing as we require an API key
HUBSPOT_ACCESS_TOKEN=""

# Learn how to get a key here https://knowledge.hubspot.com/articles/kcs_article/integrations/how-do-i-get-my-hubspot-api-key
HUBSPOT_API_KEY=""
HUBSPOT_DEVELOPER_KEY=""
7 changes: 3 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [8.1, 8.0, 7.4]
php: [8.2, 8.1, 8.0]
os: [ubuntu-latest]

name: PHP${{ matrix.php }} on ${{ matrix.os }}
Expand Down Expand Up @@ -41,9 +41,8 @@ jobs:
- name: Execute CS check
env:
PHP_CS_FIXER_IGNORE_ENV: 1
run: vendor/bin/php-cs-fixer fix --dry-run
run: composer lint
- name: Execute tests
env:
HUBSPOT_API_KEY: ${{secrets.HUBSPOT_PRIVATE_APP_KEY}}
HUBSPOT_USE_OAUTH2: true
HUBSPOT_ACCESS_TOKEN: ${{secrets.HUBSPOT_PRIVATE_APP_KEY}}
run: vendor/bin/phpunit
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ composer.lock
.DS_Store
.env
.phpunit.result.cache
.php_cs.cache
.php_cs.cache
.idea
.php-cs-fixer.cache
12 changes: 5 additions & 7 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

$finder = PhpCsFixer\Finder::create()
->exclude('vendor')
->in(__DIR__)
;
->in(__DIR__);

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config)
->setRules([
'@Symfony' => true,
'@Symfony' => true,
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'php_unit_method_casing' => ['case' => 'snake_case'],
])
->setIndent("\t")
->setIndent("\t")
->setLineEnding("\r\n")
->setFinder($finder)
;
->setFinder($finder);
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ php:
install:
- 'composer install'
script:
- 'vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no'
- 'vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.dist.php -v --dry-run --stop-on-violation --using-cache=no'
- 'vendor/bin/phpunit'
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [6.0.0] - 2023-06-23

- Updated to use the new [Hubspot PHP API Client](https://github.com/HubSpot/hubspot-api-php).

## [5.0.0] - 2022-11-12

- Updated Hubspot package to v5
Expand Down
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,35 @@ You can use either the facade or inject the HubSpot class as a dependency:
### Facade
```php
// Echo all contacts first and last names
$response = HubSpot::contacts()->all();
$response = HubSpot::crm()->contacts()->basicApi()->getPage();
foreach ($response->contacts as $contact) {
echo sprintf(
"Contact name is %s %s." . PHP_EOL,
$contact->properties->firstname->value,
$contact->properties->lastname->value
$contact->getProperties()['firstname'],
$contact->getProperties()['lastname']
);
}
```
### Dependency Injection
```php
Route::get('/', function (Rossjcooper\LaravelHubSpot\HubSpot $hubspot) {
$response = $hubspot->contacts()->all();
Route::get('/', function (HubSpot\Discovery\Discovery $hubspot) {
$response = $hubspot->crm()->contacts()->basicApi()->getPage();
foreach ($response->contacts as $contact) {
echo sprintf(
"Contact name is %s %s." . PHP_EOL,
$contact->properties->firstname->value,
$contact->properties->lastname->value
$contact->getProperties()['firstname'],
$contact->getProperties()['lastname']
);
}
});
```

For more info on using the actual API see the main repo [hubspot/hubspot-php](https://github.com/HubSpot/hubspot-php)
For more info on using the actual API see the main repo [Hubspot/hubspot-api-php](https://github.com/HubSpot/hubspot-api-php)

## Testing

We're using the brilliant [Orchestra Testbench](https://github.com/orchestral/testbench) v4 to run unit tests in a Laravel based environment. If you wish to run tests be sure to have a HubSpot API key inside your `.env` file and run `./vendor/bin/phpunit`
We're using the brilliant [Orchestra Testbench](https://github.com/orchestral/testbench) v4 to run unit tests in a Laravel based environment. If you wish to run tests be sure to have a HubSpot API key inside your `.env` file and run `composer run test`

Current unit test access the HubSpot API and expect to see the demo contacts/leads that HubSpot provides to its developer accounts.

## Issues
Please only report issues relating to the Laravel side of things here, main API issues should be reported [here](https://github.com/ryanwinchester/hubspot-php/issues)
Please only report issues relating to the Laravel side of things here, main API issues should be reported [here](https://github.com/HubSpot/hubspot-api-php/issues)
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.0.0
6.0.0
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
],
"require": {
"illuminate/support": ">=5.3",
"hubspot/hubspot-php": "^5.0"
"hubspot/api-client": "^10.0",
"php": ">=8.0"
},
"autoload": {
"psr-4": {
Expand All @@ -37,6 +38,11 @@
],
"require-dev": {
"orchestra/testbench": ">=3.4",
"friendsofphp/php-cs-fixer": "^2.15"
"friendsofphp/php-cs-fixer": "^v3.19"
},
"scripts": {
"test": "./vendor/bin/phpunit",
"lint": "./vendor/bin/php-cs-fixer fix -v --dry-run",
"fix": "./vendor/bin/php-cs-fixer fix"
}
}
45 changes: 19 additions & 26 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="unit">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="api">
<directory>./tests/API/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="./vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.2/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="unit">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="api">
<directory>./tests/API/</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
28 changes: 28 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="unit">
<directory>./tests/Unit/</directory>
</testsuite>
<testsuite name="api">
<directory>./tests/API/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>
3 changes: 2 additions & 1 deletion src/Facades/HubSpot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rossjcooper\LaravelHubSpot\Facades;

use HubSpot\Discovery\Discovery;
use Illuminate\Support\Facades\Facade;

class HubSpot extends Facade
Expand All @@ -13,6 +14,6 @@ class HubSpot extends Facade
*/
protected static function getFacadeAccessor()
{
return 'Rossjcooper\LaravelHubSpot\HubSpot';
return Discovery::class;
}
}
9 changes: 0 additions & 9 deletions src/HubSpot.php

This file was deleted.

67 changes: 53 additions & 14 deletions src/HubSpotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,60 @@

namespace Rossjcooper\LaravelHubSpot;

use GuzzleHttp\Client;
use HubSpot\Discovery\Discovery;
use HubSpot\Factory;
use Illuminate\Support\ServiceProvider;

class HubSpotServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*/
public function register()
{
//Bind the HubSpot wrapper class
$this->app->bind('Rossjcooper\LaravelHubSpot\HubSpot', function ($app) {
if (env('HUBSPOT_USE_OAUTH2', config('hubspot.use_oauth2'))) {
return HubSpot::createWithOAuth2Token(
env('HUBSPOT_API_KEY', config('hubspot.api_key')),
null,
config('hubspot.client_options', [])
$this->includeConfig();

$this->app->bind(Discovery::class, function ($app) {
$this->includeConfig();

$this->mergeConfigFrom(
__DIR__.'/config/hubspot.php',
'hubspot'
);

$handlerStack = \GuzzleHttp\HandlerStack::create();

if (config('hubspot.enable_constant_delay')) {
$handlerStack->push(
\HubSpot\RetryMiddlewareFactory::createRateLimitMiddleware(
\HubSpot\Delay::getConstantDelayFunction()
)
);
}

return HubSpot::create(
env('HUBSPOT_API_KEY', config('hubspot.api_key')),
null,
config('hubspot.client_options', [])
if ($exponentialDelay = config('hubspot.exponential_delay')) {
$handlerStack->push(
\HubSpot\RetryMiddlewareFactory::createRateLimitMiddleware(
\HubSpot\Delay::getExponentialDelayFunction($exponentialDelay)
)
);
}

$client = new Client(array_merge(
config('hubspot.client_options'),
[
'handler' => $handlerStack,
]
));

if ($accessToken = config('hubspot.access_token')) {
return Factory::createWithAccessToken(
$accessToken,
$client,
);
}

return Factory::createWithDeveloperApiKey(
config('hubspot.developer_key'),
$client,
);
});
}
Expand All @@ -39,4 +70,12 @@ public function boot()
__DIR__.'/config/hubspot.php' => config_path('hubspot.php'),
], 'config');
}

protected function includeConfig(): void
{
$this->mergeConfigFrom(
__DIR__.'/config/hubspot.php',
'hubspot'
);
}
}
22 changes: 17 additions & 5 deletions src/config/hubspot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,28 @@

return [
/*
* Either the API key or the private app access token
* Connect to the Hubspot API using a private app access token
*/
'api_key' => env('HUBSPOT_API_KEY'),
'access_token' => env('HUBSPOT_ACCESS_TOKEN'),

/*
* Should the library connect via OAuth2 or use the API key. The usage of the API key will be deprecated on
* November 30th, 2022.
* Connect to the Hubspot API using a Developer API Key
*/
'use_oauth2' => env('HUBSPOT_USE_OAUTH2', false),
'developer_key' => env('HUBSPOT_DEVELOPER_KEY'),

/*
* Options to enable built in middlewares to handle rate limiting
*
* @see https://github.com/HubSpot/hubspot-api-php#api-client-comes-with-middleware-for-implementation-of-rate-and-concurrent-limiting
*/
'enable_constant_delay' => false,
'exponential_delay' => null,

/*
* Guzzle Client options that are user for Hubspot API requests
*
* @see https://docs.guzzlephp.org/en/stable/request-options.html
*/
'client_options' => [
'http_errors' => true,
],
Expand Down
Loading

0 comments on commit 6a36194

Please sign in to comment.