Skip to content

Commit

Permalink
Merge branch 'eRankCo-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysnhall committed Jun 29, 2023
2 parents c0a1f0d + 94180cc commit 83005ce
Show file tree
Hide file tree
Showing 12 changed files with 441 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
composer.lock
.DS_Store
19 changes: 19 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
# Changelog

## v0.4.0
* Make api key optional when initializing client.
* Etsy::getUser now supports an optional user ID.
* Add expiry value to access tokens.
* Change Listing update request to use new PATCH method.
* Add new associations to Listing resource.
* Update endpoints for various Listing associated resources.

### New
* Added Etsy::getMe() method: [User getMe method](https://developers.etsy.com/documentation/reference#operation/getMe)
* Added Etsy::tokenScopes method: [tokenScopes](https://developers.etsy.com/documentation/reference#operation/tokenScopes)
* Added new [BuyerTaxonomy](https://developers.etsy.com/documentation/reference#tag/BuyerTaxonomy) and BuyerTaxonomyProperty resource and supporting methods.
* Added Etsy::findShops() method: [findShops](https://developers.etsy.com/documentation/reference#operation/findShops)
* Add new [ListingVideo](https://developers.etsy.com/documentation/reference#tag/ShopListing-Video) resource and supporting methods.
* Add new [ProductionPartner](https://developers.etsy.com/documentation/reference#tag/Shop-ProductionPartner) resource and supporting methods.
* Add new [ReturnPolicy](https://developers.etsy.com/documentation/reference#tag/Shop-Return-Policy) resource and supporting methods.

---

## v0.3.4
### Fixed issues
* Add pagination support for the Receipt resource. [Issue #15](https://github.com/rhysnhall/etsy-php-sdk/issues/15)
Expand Down
7 changes: 6 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rhysnhall/etsy-php-sdk",
"version": "0.3.4",
"version": "0.4.0",
"description": "PHP SDK for Etsy API v3.",
"type": "library",
"keywords": [
Expand All @@ -15,6 +15,11 @@
"name": "Rhys Hall",
"email": "[email protected]",
"homepage": "https://github.com/rhysnhall"
},
{
"name": "Andrew Christensen",
"email": "[email protected]",
"homepage": "https://github.com/erankco"
}
],
"require": {
Expand Down
65 changes: 60 additions & 5 deletions src/Etsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Etsy {

public function __construct(
string $client_id,
string $api_key,
string $api_key = null,
array $config = []
) {
$this->client_id = $client_id;
Expand Down Expand Up @@ -118,16 +118,38 @@ public function ping() {
}

/**
* Only supports getting the user for who the current API KEY is associated with.
* Gets the user id and shop id for the logged in user.
*
* @return array
*/
public function getMe() {
$response = static::$client->get("/application/users/me");
return $response ?? [];
}

/**
* Gets the scopes for the current token
*
* @return array
*/
public function tokenScopes() {
$response = static::$client->post("/application/scopes");
return $response ?? [];
}

/**
* Get the user for the current tokened user or for a specified user.
*
* @params int|null $user_id
* @return Etsy\Resources\User
*/
public function getUser() {
$user_id = explode(".", $this->api_key)[0];
$response = static::$client->get("/application/users/{$user_id}");
public function getUser($user_id = null) {
$user_id = $user_id ?? explode(".", $this->api_key)[0];
$response = static::$client->get("/application/users/{$user_id}", []);
return static::getResource($response, "User");
}


/**
* Gets an Etsy shop. If no shop_id is specified the current user will be queried for an associated shop.
*
Expand Down Expand Up @@ -166,6 +188,7 @@ public function getShops($keyword, $params = []) {
/**
* Retrieves the full hierarchy tree of seller taxonomy nodes.
*
* @link https://developers.etsy.com/documentation/reference#tag/getSellerTaxonomyNodes
* @return Etsy\Collection[Etsy\Resources\Taxonomy]
*/
public function getSellerTaxonomy() {
Expand All @@ -175,6 +198,19 @@ public function getSellerTaxonomy() {
return static::getResource($response, "Taxonomy");
}

/**
* Retrieves the full hierarchy tree of buyer taxonomy nodes.
*
* @link https://developers.etsy.com/documentation/reference#operation/getBuyerTaxonomyNodes
* @return Etsy\Collection[Etsy\Resources\BuyerTaxonomy]
*/
public function getBuyerTaxonomy() {
$response = static::$client->get(
"/application/buyer-taxonomy/nodes"
);
return static::getResource($response, "BuyerTaxonomy");
}

/**
* Retrieves a list of available shipping carriers and the mail classes associated with them for a given country
*
Expand Down Expand Up @@ -253,4 +289,23 @@ public function getListings(
return static::getResource($response, "Listing");
}

/**
* Find shops based on a shop name
*
* @param array $params
* @return Etsy\Collection[Etsy\Resources\Shop]
*/
public function findShops(
array $params
) {
if(!isset($params['shop_name'])) {
throw new ApiException("Etsy findShops operation expects a `shop_name` param.");
}
$response = static::$client->get(
"/application/shops",
$params
);
return static::getResource($response, "Shop");
}

}
25 changes: 18 additions & 7 deletions src/OAuth/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ class Client {
*/
protected $config = [];

/**
* @var array
*/
public $headers = [];

/**
* Create a new instance of Client.
*
Expand Down Expand Up @@ -79,11 +84,14 @@ public function setConfig($config) {
* @param string $api_key
* @return void
*/
public function setApiKey($api_key) {
$this->headers = [
'x-api-key' => $this->client_id,
'Authorization' => "Bearer {$api_key}"
public function setApiKey($api_key = null) {
$headers = [
'x-api-key' => $this->client_id
];
if ($api_key) {
$headers['Authorization'] = "Bearer {$api_key}";
}
$this->headers = $headers;
}

public function __call($method, $args) {
Expand Down Expand Up @@ -190,7 +198,8 @@ public function requestAccessToken(
$response = json_decode($response->getBody(), false);
return [
'access_token' => $response->access_token,
'refresh_token' => $response->refresh_token
'refresh_token' => $response->refresh_token,
'expires_at' => (time() + $response->expires_in)
];
}
catch(\Exception $e) {
Expand Down Expand Up @@ -219,7 +228,8 @@ public function refreshAccessToken(
$response = json_decode($response->getBody(), false);
return [
'access_token' => $response->access_token,
'refresh_token' => $response->refresh_token
'refresh_token' => $response->refresh_token,
'expires_at' => (time() + $response->expires_in)
];
}
catch(\Exception $e) {
Expand Down Expand Up @@ -248,7 +258,8 @@ public function exchangeLegacyToken(
$response = json_decode($response->getBody(), false);
return [
'access_token' => $response->access_token,
'refresh_token' => $response->refresh_token
'refresh_token' => $response->refresh_token,
'expires_at' => (time() + $response->expires_in)
];
}
catch(\Exception $e) {
Expand Down
28 changes: 28 additions & 0 deletions src/Resources/BuyerTaxonomy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Etsy\Resources;

use Etsy\Resource;

/**
* BuyerTaxonomy resource class. Represents a buyer taxonomy within Etsy. These are essentially categories used for listing.
*
* @link https://developers.etsy.com/documentation/reference/#tag/BuyerTaxonomy
* @author Andrew Christensen [email protected]
*/
class BuyerTaxonomy extends Resource {

/**
* Get the properties for this Buyer Taxonomy node.
*
* @return \Etsy\Collection
*/
public function getProperties() {
return $this->request(
'GET',
"/application/buyer-taxonomy/nodes/{$this->id}/properties",
"BuyerTaxonomyProperty"
);
}

}
13 changes: 13 additions & 0 deletions src/Resources/BuyerTaxonomyProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Etsy\Resources;

use Etsy\Resource;

/**
* BuyerTaxonomyProperty resource class. Represents an optional listing field for a specific buyer taxonomy.
*
* @link https://developers.etsy.com/documentation/reference/#operation/getPropertiesByBuyerTaxonomyId
* @author Andrew Christensen [email protected]
*/
class BuyerTaxonomyProperty extends Resource {}
Loading

0 comments on commit 83005ce

Please sign in to comment.