Example #1
0
 /**
  * Store the new limits
  * @param integer $limit
  * @param integer $remaining
  * @param integer $reset Timestamp
  */
 public function update($limit, $remaining, $reset)
 {
     $this->limit = $limit;
     $this->remaining = $remaining;
     $this->reset = $reset;
     App::getInstance()->storage->set('rateLimits', $this, $reset - time());
 }
Example #2
0
 /**
  * Add data to the page
  */
 public function handle()
 {
     $exception = $this->getException();
     //Catch the client exception
     if ($exception instanceof ClientException) {
         $error = ErrorResponse::model()->setAttributes(json_decode((string) $exception->getResponse()->getBody(), true));
         $this->addDataTable('API result', $error->getBody());
     }
     //Read from the api request log and show the data
     foreach (array_reverse(Api::getRequestLog(), true) as $key => $log) {
         $this->addDataTable("API Request #" . ($key + 1), $log->getBody());
     }
     //Add rate limits
     $this->addDataTable('Rate limits', ['limit' => App::getInstance()->getRateLimit()->limit, 'remaining' => App::getInstance()->getRateLimit()->remaining, 'reset' => App::getInstance()->getRateLimit()->getDateTime()]);
     $this->setPageTitle('Error ' . $this->getException()->getMessage());
     parent::handle();
 }
Example #3
0
 * This sample file demonstrates to flow when you want to authorize your application
 * to connect to Afosto on behalf of the Afosto user.
 * 
 * Session storage is used to demonstrate the storage of the accessToken and 
 * demostrate persistence of the authorization
 */
namespace Afosto\ApiClient;

use Afosto\ApiClient\Components\Storage\SessionStorage;
//Change these paths accordingly
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/config.php';
//Set the caching parameters
$storage = new SessionStorage();
App::run($storage, CLIENT_ID, CLIENT_SECRET);
//Verify if we allready have a token or not, otherwise try to connect
if (App::getInstance()->hasToken()) {
    //Interact with the API
    echo App::getInstance()->getAccessToken();
    exit;
} else {
    if (isset($_GET['code'])) {
        //Obtain the code from the uri
        App::getInstance()->authorize($_GET['code']);
        exit('Authorized, you can refresh the page as the accessToken is now stored in the cache');
    } else {
        //No code give, no authorzation in place, redirect to the application to
        //obtain grant
        header('Location: ' . App::getInstance()->getAuthorizationUrl(REDIRECT_URI));
    }
}
Example #4
0
 /**
  * Update the totalCount
  * @param integer $totalCount
  */
 public function update($totalCount)
 {
     $this->_totalCount = (int) $totalCount;
     App::getInstance()->storage->set('pagination' . $this->_owner->getName(), $this->_totalCount);
 }
Example #5
0
 * This samplefile demostrates the retreival of a product, adjustment of 
 * the descriptor and mutation of the inventory
 */
namespace Afosto\ApiClient;

use Afosto\ApiClient\Components\Storage\SessionStorage;
use Afosto\ApiClient\Models\Products\Product;
use Afosto\ApiClient\Models\Products\Descriptor;
//Change these paths accordingly
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/config.php';
//Set the caching parameters
$storage = new SessionStorage();
App::run($storage, CLIENT_ID, CLIENT_SECRET);
//Verify if we allready have a token or not, otherwise try to connect
if (App::getInstance()->hasToken()) {
    //Change the product id
    $product = Product::model()->find(1);
    $descriptor = new Descriptor();
    $descriptor->name = 'New name, changed at: ' . time();
    $descriptor->short_description = 'New short description: ' . time();
    $descriptor->description = 'new long description: ' . time();
    $product->descriptors = [$descriptor];
    //Add two items to the inventory amount (make sure product that you searched
    //keeps inventory
    foreach ($product->items as $item) {
        foreach ($item->inventory->warehouses as $warehouse) {
            $warehouse->amount += 2;
        }
    }
    $product->save();
Example #6
0
<?php

/**
 * This sample file demonstrates the functionality to receive incoming 
 * webhooks. 
 * Use this file as a starting point to receive incoming webhooks from the API.
 * 
 * Developing on your local machine? Install ngrok to transfer webhooks to your local machine.
 * https://ngrok.com/
 */
namespace Afosto\ApiClient;

use Afosto\ApiClient\Components\Storage\SessionStorage;
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/config.php';
$cache = new SessionStorage();
App::run($cache, CLIENT_ID, CLIENT_SECRET);
$object = App::getWebhookModel();
switch ($object->getName()) {
    case 'Product':
        //Do things with this product
        break;
    case 'Sale':
        //Do things with this sale
}
Example #7
0
<?php

/**
 * Login to the api using an earlier obtained accessToken, probably how you
 * want to use the client in production.
 */
namespace Afosto\ApiClient;

use Afosto\ApiClient\Components\Storage\SessionStorage;
//Change these paths accordingly
require_once dirname(__FILE__) . '/vendor/autoload.php';
require_once dirname(__FILE__) . '/config.php';
//Set the caching parameters
$storage = new SessionStorage();
App::run($storage, CLIENT_ID, CLIENT_SECRET);
//Place your obtained access token here
App::getInstance()->login($accessToken);
Example #8
0
 /**
  * Insert a log
  */
 private function _addLog()
 {
     $log = new Log();
     $log->method = $this->_request->getMethod();
     $log->uri = (string) $this->_request->getUri();
     $config = App::getInstance()->getClient()->getConfig();
     $log->options = array_merge_recursive(['headers' => $config['headers']], $this->_options);
     self::$_log[] = $log;
     //Keep only the last three requests in the log
     self::$_log = array_slice(self::$_log, -3);
 }