Skip to content

Commit

Permalink
Nuvemshop/Tiendanube SDK 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lombo committed Feb 5, 2023
1 parent 7e2c025 commit 513ac75
Show file tree
Hide file tree
Showing 102 changed files with 10,968 additions and 671 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
; https://editorconfig.org/

root = true

[*]
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
end_of_line = lf
charset = utf-8

[*.{json,yml}]
indent_size = 2

[*.neon]
indent_style = tab

[Makefile]
indent_style = tab
11 changes: 11 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

# A list of files and folders those will be excluded from archives and the
# Composer package (for purposes of making it smaller).
/.editorconfig export-ignore
/.gitattributes export-ignore
/.github export-ignore
/.php-cs-fixer.php export-ignore
/.vscode export-ignore
/phpunit.xml export-ignore
/tests export-ignore
33 changes: 31 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
composer.lock
vendor
# Ignore our deprecation warnings
.last_api_deprecation_warning

# Ignore Mac OS X fies.
.DS_Store

# Ignore the /vendor/ directory for people using composer
/vendor/

# Ignore PHPUnit coverage file
clover.xml

# Ignore IDE's configuration files
.idea
.vscode

# Ignore PHP CS Fixer local config and cache
.php_cs
.php_cs.cache
.php-cs-fixer.cache

# Ignore PHPStan local config
.phpstan.neon

# Ignore phpDocumentor's local config and artifacts
.phpdoc/*
phpdoc.xml

# Ignore cached PHPUnit results.
.phpunit.result.cache
.phpunit.cache
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

"⚠️" symbol highlights breaking changes.

## 2.0.0 - 2023-02-04
* [#4](https://github.com/tiendanube/tiendanube-php-sdk/pull/4) API Updates
* Relaunched our PHP API to follow current standards.
* Support Composer and Packagist
* ⚠️ Drop support for PHP 5.6
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 Tienda Nube
Copyright (c) 2013 Tiendanube/Nuvemshop

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
Expand Down
192 changes: 79 additions & 113 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,71 @@
Tienda Nube/Nuvem Shop SDK for PHP
==================================
# Nuvemshop/Tiendanube SDK for PHP

This SDK provides a simplified access to the [API](https://github.com/TiendaNube/api-docs) of [Nuvem Shop](https://www.nuvemshop.com.br) / [Tienda Nube](https://www.tiendanube.com).
This SDK provides a simplified access to the [API](https://tiendanube.github.io/api-documentation/) of [Nuvemshop](https://www.nuvemshop.com.br) / [Tiendanube](https://www.tiendanube.com).

Installation
------------
This SDK is mounted on top of [Requests for PHP](https://github.com/rmccue/Requests), so we recommend using [Composer](https://github.com/composer/composer) for installing.
## Requirements

Simply add the `tiendanube/php-sdk` requirement to composer.json.
PHP 7.4.0 and later.

```json
{
"require": {
"tiendanube/php-sdk": ">=1.0"
}
}
```
## Composer

You can install the bindings via [Composer](http://getcomposer.org/). Run the following command:

Then run `composer install` or `composer update` to complete the installation.
```bash
composer require stripe/stripe-php
```

If you need an autoloader, you can use the one provided by Composer:
To use the bindings, use Composer's [autoload](https://getcomposer.org/doc/01-basic-usage.md#autoloading):

```php
require 'vendor/autoload.php';
require_once('vendor/autoload.php');
```

## Authenticating Your App

Authenticating Your App
-----------------------
When a user installs your app, he will be taken to your specified Redirect URI with a parameter called `code` containing your temporary authorization code.

With this code you can request a permanent access token.

```php
$code = $_GET['code'];

$auth = new TiendaNube\Auth(CLIENT_ID, CLIENT_SECRET);
$store_info = $auth->request_access_token($code);
use Tiendanube\Context;
use Tiendanube\Auth\OAuth;

$context = Context::initialize(
CLIENT_ID,
CLIENT_SECRET,
APP_BASE_URL,
APP_USER_AGENT_PREFIX,
);

$oauth = new OAuth();
$session = $oauth->callback($_GET);
```

The returned value will contain the id of the authenticated store, as well as the access token and the authorized scopes.
The returned session will contain the id of the authenticated store, as well as the access token and the authorized scopes.

```php
var_dump($store_info);
//array (size=3)
// 'store_id' => string '1234' (length=4)
// 'access_token' => string 'a2b544066ee78926bd0dfc8d7bd784e2e016b422' (length=40)
// 'scope' => string 'read_products,read_orders,read_customers' (length=40)
var_dump($session);
//object(Tiendanube\Auth\Session)#5 (3) {
// ["storeId":"Tiendanube\Auth\Session":private]=>
// string(4) "1234"
// ["scope":"Tiendanube\Auth\Session":private]=>
// string(40) "read_products,read_orders,read_customers"
// ["accessToken":"Tiendanube\Auth\Session":private]=>
// string(40) "a2b544066ee78926bd0dfc8d7bd784e2e016b422"
//}
```

Keep in mind that future visits to your app will not go through the Redirect URI, so you should store the store id in a session.
Keep in mind that future visits to your app will not go through the Redirect URI, so you should store the session.

However, if you need to authenticate a user that has already installed your app (or invite them to install it), you can redirect them to login to the Tienda Nube/Nuvem Shop site.
However, if you need to authenticate a user that has already installed your app (or invite them to install it), you can redirect them to login to the Nuvemshop/Tiendanube site.

```php
$auth = new TiendaNube\Auth(CLIENT_ID, CLIENT_SECRET);
use Tiendanube\Auth\OAuth;
$auth = new OAuth();

//You can use one of these to obtain a url to login to your app
$url = $auth->login_url_brazil();
$url = $auth->login_url_spanish();
$url = $auth->loginUrlBrazil();
$url = $auth->loginUrlSpLATAM();

//Redirect to $url
```
Expand All @@ -71,92 +78,51 @@ Making a Request
The first step is to instantiate the `API` class with a store id and an access token, as well as a [user agent to identify your app](https://github.com/TiendaNube/api-docs#identify-your-app). Then you can use the `get`, `post`, `put` and `delete` methods.

```php
$api = new TiendaNube\API(STORE_ID, ACCESS_TOKEN, 'Awesome App ([email protected])');
$response = $api->get("products");
var_dump($response->body);
```

You can access the headers of the response via `$response->headers` as if it were an array:
use Tiendanube\Context;
use Tiendanube\Auth\Session;
use Tiendanube\Rest\Adminv1\Product;

```php
var_dump(isset($response->headers['X-Total-Count']));
//boolean true
$context = Context::initialize(
CLIENT_ID,
CLIENT_SECRET,
'www.awesome-app.com',
'Awesome App ([email protected])'
);

var_dump($response->headers['X-Total-Count']);
//string '48' (length=2)
```
$session = new Session(
STORE_ID,
ACCESS_TOKEN,
SCOPES
);

For convenience, the `X-Main-Language` header can be obtained from `$response->main_language`:
$productsFromFirstPage = Product::all($session);
var_dump($productsFromFirstPage);

```php
$response = $api->get("products/123456");
$language = $response->main_language;
var_dump($response->body->name->$language);
//You can then access following pages with the same object
$productsFromSecondPage = Product::all($session, Product::$nextPageQuery);
```

Other examples:
You can also call the endpoints directly

```php
//Create a product
$response = $api->post("products", [
'name' => 'Tienda Nube',
]);
$product_id = $response->body->id;

//Change its name
$response = $api->put("products/$product_id", [
'name' => 'Nuvem Shop',
]);

//And delete it
$response = $api->delete("products/$product_id");

//You can also send arguments to GET requests
$response = $api->get("orders", [
'since_id' => 10000,
]);
use Tiendanube\Context;
use Tiendanube\Auth\Session;

$context = Context::initialize(
CLIENT_ID,
CLIENT_SECRET,
'www.awesome-app.com',
'Awesome App ([email protected])'
);

$session = new Session(
STORE_ID,
ACCESS_TOKEN,
SCOPES
);

$client = new \Tiendanube\Clients\Rest($session->getStoreId(), $session->getAccessToken());
$response = $client->get('products');
var_dump($response->getStatusCode(), $response->getDecodedBody(), $response->getHeaders(), $response->getPageInfo());
```

For list results you can use the `next`, `prev`, `first` and `last` methods to retrieve the corresponding page as a new response object.

```php
$response = $api->get('products');
while($response != null){
foreach($response->body as $product){
var_dump($product->id);
}
$response = $response->next();
}
```

Exceptions
----------
Calls to `Auth` may throw a `Tiendanube\Auth\Exception`:

```php
try{
$auth->request_access_token($code);
} catch(Tiendanube\Auth\Exception $e){
var_dump($e->getMessage());
//string '[invalid_grant] The authorization code has expired' (length=50)
}
```

Likewise, calls to `API` may throw a `Tiendanube\API\Exception`. You can retrieve the original response from these exceptions:

```php
try{
$api->get('products');
} catch(Tiendanube\API\Exception $e){
var_dump($e->getMessage());
//string 'Returned with status code 401: Invalid access token' (length=43)

var_dump($e->response->body);
//object(stdClass)[165]
// public 'code' => int 401
// public 'message' => string 'Unauthorized' (length=12)
// public 'description' => string 'Invalid access token' (length=20)
}
```

Requests that return 404 will throw a subclass called `Tiendanube\API\NotFoundException`.
21 changes: 21 additions & 0 deletions RELEASING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Releasing tiendanube-php-sdk

1. Check the Semantic Versioning page for info on how to version the new release: [http://semver.org](http://semver.org)

2. Ensure your local repo is up-to-date

```bash
git checkout master && git pull
```

3. Add an entry for the new release to `CHANGELOG.md`, and/or move the contents from the _Unreleased_ to the new release

4. Increment the version in `src/Context.php`

5. Stage the `CHANGELOG.md` and `src/Context.php` files

```bash
git add CHANGELOG.md src/Context.php
```

6. To update the version, commit and push the changes and create the appropriate tag - Packagist will pick it up and release it
36 changes: 33 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,42 @@
{
"name": "tiendanube/php-sdk",
"description": "A PHP SDK for the Tienda Nube/Nuvem Shop API.",
"description": "Tiendanube/Nuvemshop PHP library",
"license": "MIT",
"keywords": [
"tiendanube",
"nuvemshop",
"commerce",
"api"
],
"homepage": "https://tiendanube.com/",
"authors": [
{
"name": "Tiendanube/Nuvemshop and contributors",
"homepage": "https://github.com/tiendanube/tiendanube-php-sdk/contributors"
}
],
"require": {
"rmccue/requests": ">=1.0"
"php": "^7.4 || ^8.0 || ^8.1 || ^8.2",
"ext-json": "*",
"ext-mbstring": "*",
"psr/log": "^1.1 || ^2.0 || ^3.0",
"psr/http-client": "^1.0",
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.6",
"mikey179/vfsstream": "^1.6"
},
"autoload": {
"psr-0": {"TiendaNube": "src/"}
"psr-4": {
"Tiendanube\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tiendanube\\": "tests/"
}
}
}

Loading

0 comments on commit 513ac75

Please sign in to comment.