From 1f6276df9c1cf1590322bf38454dfd23c238e03d Mon Sep 17 00:00:00 2001 From: Drsdre Date: Wed, 1 Feb 2017 15:08:38 +0100 Subject: [PATCH] Change interface data functions to http method equivalent (updateData > putData, addData > postData). Change is not backwards compatible! Show parameter errors on 400 bad request errors. Change apiBaseUrl to /wp-json to also allow other Rest API's to be called. --- Client.php | 24 ++++++++++++++---------- OAuth1.php | 14 +++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/Client.php b/Client.php index b7a1b28..250343c 100644 --- a/Client.php +++ b/Client.php @@ -21,7 +21,7 @@ class Client extends \yii\base\Object { /** - * @var string API endpoint (default production) + * @var string API url endpoint */ public $endpoint = ''; @@ -174,7 +174,7 @@ public function getData( } /** - * Update with entity url + * Put data with entity url * * @param string $entity_url * @param string $context view or edit @@ -182,7 +182,7 @@ public function getData( * * @return self */ - public function updateData( + public function putData( $entity_url, $context = 'edit', array $data @@ -203,7 +203,7 @@ public function updateData( } /** - * Update with entity url + * Patch with entity url * * @param string $entity_url * @param string $context view or edit @@ -232,7 +232,7 @@ public function patchData( } /** - * Add data with entity url + * Post data with entity url * * @param string $entity_url * @param string $context view or edit @@ -240,7 +240,7 @@ public function patchData( * * @return self */ - public function addData( + public function postData( $entity_url, $context = 'view', array $data @@ -373,19 +373,23 @@ protected function executeRequest() { // Check for response status code if ( ! $this->response->isOk ) { + $result_content = Json::decode( $this->response->content, false ); switch ( $this->response->statusCode ) { case 304: throw new Exception( 'Not Modified.' ); case 400: + $parameter_errors = []; + foreach($result_content->data->params as $param_error) { + $parameter_errors[] = $param_error; + } throw new Exception( - 'Bad Request: request ' . $this->request->getFullUrl() . ' was invalid.', + 'Bad Request: '.implode(' | ', $parameter_errors).' (request ' . $this->request->getFullUrl() . ').', Exception::FAIL ); case 401: - // Handle {"code":"json_oauth1_nonce_already_used","message":"Invalid nonce - nonce has already been used","data":{"status":401}} - $result_content = Json::decode( $this->response->content, false ); + // Nonce used can be retried, other 401 can not if ( isset( $result_content->code ) && $result_content->code == 'json_oauth1_nonce_already_used' ) { throw new Exception( ( isset( $result_content->message ) ? $result_content->message : $this->response->content ) . @@ -394,7 +398,7 @@ protected function executeRequest() { ); } else { throw new Exception( - 'Unauthorized: user does not have permission to access ' . $this->request->getFullUrl(), + 'Unauthorized: '.$result_content->message.' (request ' . $this->request->getFullUrl() . ').', Exception::FAIL ); } diff --git a/OAuth1.php b/OAuth1.php index e7d50de..78c917b 100644 --- a/OAuth1.php +++ b/OAuth1.php @@ -59,18 +59,10 @@ class OAuth1 extends \yii\authclient\OAuth1 public $accessTokenMethod = 'POST'; /** - * var $apiBaseUrl is Wordpress site url + * var $apiBaseUrl is Wordpress site url (json slug is auto added) */ public $apiBaseUrl; - /** - * @var array list of extra parameters, which should be used, while requesting user attributes from Wordpress API. - * - * @see http://oauth1.wp-api.org/docs/basics/Auth-Flow.html - * @since 2.0.6 - */ - public $attributeParams = []; - /** * @inheritdoc */ @@ -82,8 +74,8 @@ public function __construct( array $config ) { $this->requestTokenUrl = $this->apiBaseUrl.'/'.$this->requestTokenUrl; $this->accessTokenUrl = $this->apiBaseUrl.'/'.$this->accessTokenUrl; - // Set apiBaseUrl to Wordpress v2 URL - $this->apiBaseUrl = $this->apiBaseUrl.'/wp-json/wp/v2'; + // Set apiBaseUrl to Wordpress Rest API base slug + $this->apiBaseUrl = $this->apiBaseUrl.'/wp-json'; } /**