From 849e01b5d909ff20626100102a988885c4092037 Mon Sep 17 00:00:00 2001 From: ksvirkou Date: Tue, 14 Jan 2020 12:57:13 +0300 Subject: [PATCH 1/2] add products --- README.md | 2 +- src/Http/Client.php | 2 +- src/Resources/HubDB.php | 4 +- src/Resources/Products.php | 195 +++++++++++++++++++ tests/Integration/Resources/ProductsTest.php | 185 ++++++++++++++++++ 5 files changed, 384 insertions(+), 4 deletions(-) create mode 100644 src/Resources/Products.php create mode 100644 tests/Integration/Resources/ProductsTest.php diff --git a/README.md b/README.md index 755df8c5..99d66acc 100644 --- a/README.md +++ b/README.md @@ -211,7 +211,7 @@ If you see something not planned, that you want, make an [issue](https://github. - [ ] Line Items API - [ ] Marketing Email API - [x] Owners API :upadated: -- [ ] Products API +- [x] Products API :new: - [x] Social Media API - [x] Tickets API - [x] Timeline API :upadated: diff --git a/src/Http/Client.php b/src/Http/Client.php index 593123c2..6af1819c 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -43,7 +43,7 @@ class Client protected $wrapResponse = true; /** @var string */ - private $user_agent = 'SevenShores_Hubspot_PHP/1.0.0-rc.1 (https://github.com/ryanwinchester/hubspot-php)'; + protected $user_agent = 'SevenShores_Hubspot_PHP/2.0.1 (https://github.com/HubSpot/hubspot-php)'; /** * Make it, baby. diff --git a/src/Resources/HubDB.php b/src/Resources/HubDB.php index 874673b6..a7c32584 100644 --- a/src/Resources/HubDB.php +++ b/src/Resources/HubDB.php @@ -202,7 +202,7 @@ public function addRow($tableId, array $values, bool $draft = false, string $nam "https://api.hubapi.com/hubdb/api/v2/tables/{$tableId}/rows", $draft ); - + return $this->client->request( 'post', $endpoint, @@ -378,7 +378,7 @@ public function import($tableId, string $file, array $cofig = [], bool $draft = ], ]); } - + /** * Get body. */ diff --git a/src/Resources/Products.php b/src/Resources/Products.php new file mode 100644 index 00000000..de97624b --- /dev/null +++ b/src/Resources/Products.php @@ -0,0 +1,195 @@ +client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); + } + + /** + * Get a product by ID. + * + * @param int $id + * + * @see https://developers.hubspot.com/docs/methods/products/get_product_by_id + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getById($id, array $params = []) + { + $endpoint = "https://api.hubapi.com/crm-objects/v1/objects/products/{$id}"; + + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); + } + + /** + * Get a group of products by ID. + * + * @see https://developers.hubspot.com/docs/methods/products/batch-get-products + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getBatchByIds(array $ids, array $params = []) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/products/batch-read'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => ['ids' => $ids]], + build_query_string($params) + ); + } + + /** + * Create a product. + * + * @see https://developers.hubspot.com/docs/methods/products/create-product + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function create(array $properties) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/products'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => $properties] + ); + } + + /** + * Create a group of products. + * + * @see https://developers.hubspot.com/docs/methods/products/batch-create-products + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function createBatch(array $contacts) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/products/batch-create'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => $contacts] + ); + } + + /** + * Update a product. + * + * @param int $id + * + * @see https://developers.hubspot.com/docs/methods/products/update-products + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function update($id, array $properties) + { + $endpoint = "https://api.hubapi.com/crm-objects/v1/objects/products/{$id}"; + + return $this->client->request( + 'put', + $endpoint, + ['json' => $properties] + ); + } + + /** + * Update a group of products. + * + * @see https://developers.hubspot.com/docs/methods/products/batch-update-products + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function updateBatch(array $products) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/products/batch-update'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => $products] + ); + } + + /** + * Delete a product. + * + * @param int $id + * + * @see https://developers.hubspot.com/docs/methods/products/delete-product + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function delete($id) + { + $endpoint = "https://api.hubapi.com/crm-objects/v1/objects/products/{$id}"; + + return $this->client->request('delete', $endpoint); + } + + /** + * Delete a group of products. + * + * @see https://developers.hubspot.com/docs/methods/products/batch-delete-products + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function deleteBatch(array $ids) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/products/batch-delete'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => ['ids' => $ids]] + ); + } + + /** + * Get a log of changes for products. + * + * @see https://developers.hubspot.com/docs/methods/products/get-product-changes + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getProductChanges(array $params = []) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/change-log/products'; + + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); + } +} diff --git a/tests/Integration/Resources/ProductsTest.php b/tests/Integration/Resources/ProductsTest.php new file mode 100644 index 00000000..031155cc --- /dev/null +++ b/tests/Integration/Resources/ProductsTest.php @@ -0,0 +1,185 @@ +resource->all(); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertGreaterThanOrEqual(1, count($response->objects)); + } + + /** @test */ + public function getById() + { + $response = $this->resource->getById($this->entity->objectId); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** @test */ + public function getBatchByIds() + { + $product = $this->createEntity(); + + $ids = [ + $this->entity->objectId, + $product->objectId, + ]; + + $response = $this->resource->getBatchByIds($ids); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertCount(2, $response->toArray()); + + $this->resource->delete($product->objectId); + } + + /** @test */ + public function create() + { + $this->assertEquals(200, $this->entity->getStatusCode()); + } + + /** @test */ + public function createBatch() + { + $response = $this->resource->createBatch([ + $this->getData(), + $this->getData(), + ]); + + $this->assertEquals(200, $response->getStatusCode()); + + sleep(1); + + $this->resource->deleteBatch(array_map(function ($product) { + return $product->objectId; + }, array_values($response->getData()))); + } + + /** @test */ + public function update() + { + $response = $this->resource->update($this->entity->objectId, [ + ['name' => 'name', 'value' => 'An updated product'], + ]); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** @test */ + public function updateBatch() + { + $product = $this->createEntity(); + + $response = $this->resource->updateBatch([ + [ + 'objectId' => $this->entity->objectId, + 'properties' => [ + [ + 'name' => 'price', + 'value' => 85.00, + ], + [ + 'name' => 'description', + 'value' => 'This is an updated product, it\'s getting a price change.', + ], + ], + ], + [ + 'objectId' => $product->objectId, + 'properties' => [ + [ + 'name' => 'description', + 'value' => 'Updated product name now with discount', + ], + [ + 'name' => 'discount', + 'value' => 20, + ], + ], + ], + ]); + + $this->assertEquals(200, $response->getStatusCode()); + + $this->resource->delete($product->objectId); + } + + /** @test */ + public function delete() + { + $response = $this->deleteEntity(); + + $this->assertEquals(204, $response->getStatusCode()); + + $this->entity = null; + } + + /** @test */ + public function deleteBatch() + { + $response = $this->resource->createBatch([ + $this->getData(), + $this->getData(), + ]); + + sleep(1); + + $deleteResponse = $this->resource->deleteBatch(array_map(function ($product) { + return $product->objectId; + }, array_values($response->getData()))); + + $this->assertEquals(204, $deleteResponse->getStatusCode()); + } + + /** @test */ + public function getProductChanges() + { + $response = $this->resource->getProductChanges(); + + $this->assertEquals(200, $response->getStatusCode()); + } + + protected function createEntity() + { + return $this->resource->create($this->getData()); + } + + protected function deleteEntity() + { + return $this->resource->delete($this->entity->objectId); + } + + protected function getData(string $name = 'A new product'): array + { + return [ + ['name' => 'name', 'value' => $name], + ['name' => 'description', 'value' => 'A description of this product.'], + ['name' => 'price', 'value' => 27.50], + ['name' => 'recurringbillingfrequency', 'value' => 'quarterly'], + ]; + } +} From b940c173837d226d0ea43769c65533a0929de1eb Mon Sep 17 00:00:00 2001 From: ksvirkou Date: Tue, 14 Jan 2020 14:33:55 +0300 Subject: [PATCH 2/2] rename class --- tests/Integration/Resources/ProductsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Integration/Resources/ProductsTest.php b/tests/Integration/Resources/ProductsTest.php index 031155cc..dcb0f52d 100644 --- a/tests/Integration/Resources/ProductsTest.php +++ b/tests/Integration/Resources/ProductsTest.php @@ -9,7 +9,7 @@ * @internal * @coversNothing */ -class ContactsTest extends EntityTestCase +class ProductsTest extends EntityTestCase { /** * @var SevenShores\Hubspot\Resources\Products