Skip to content

Commit

Permalink
Change interface data functions to http method equivalent (updateData…
Browse files Browse the repository at this point in the history
… > 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.
  • Loading branch information
drsdre committed Feb 1, 2017
1 parent 6ec0bcb commit 1f6276d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
24 changes: 14 additions & 10 deletions Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
class Client extends \yii\base\Object {

/**
* @var string API endpoint (default production)
* @var string API url endpoint
*/
public $endpoint = '';

Expand Down Expand Up @@ -174,15 +174,15 @@ public function getData(
}

/**
* Update with entity url
* Put data with entity url
*
* @param string $entity_url
* @param string $context view or edit
* @param array $data
*
* @return self
*/
public function updateData(
public function putData(
$entity_url,
$context = 'edit',
array $data
Expand All @@ -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
Expand Down Expand Up @@ -232,15 +232,15 @@ public function patchData(
}

/**
* Add data with entity url
* Post data with entity url
*
* @param string $entity_url
* @param string $context view or edit
* @param array $data
*
* @return self
*/
public function addData(
public function postData(
$entity_url,
$context = 'view',
array $data
Expand Down Expand Up @@ -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 ) .
Expand All @@ -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
);
}
Expand Down
14 changes: 3 additions & 11 deletions OAuth1.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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';
}

/**
Expand Down

0 comments on commit 1f6276d

Please sign in to comment.