Skip to content

Commit

Permalink
Merge pull request #1 from rickkuilman/refactor-to-php-package
Browse files Browse the repository at this point in the history
Refactor to php package
  • Loading branch information
rickkuilman authored Oct 5, 2021
2 parents d7b069d + 45d722f commit 7c48d8e
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 130 deletions.
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Changelog

All notable changes to `digital-humani-sdk` will be documented in this file
All notable changes to `digital-humani-php-sdk` will be documented in this file

## 1.0.0 - 2021-08-08

- Initial release 🎉

## 1.1.0 - 2021-10-05

- Refactored package to plain PHP package (removed Laravel specific files)
- Added ability to switch between sandbox and production environment
- Renamed package (and namespace) to emphasize the fact that this is a "PHP" SDK
45 changes: 26 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 🌲 Digital Humani - PHP SDK
# 🌳 Digital Humani - PHP SDK

[![Latest Version on Packagist](https://img.shields.io/packagist/v/rickkuilman/digital-humani-sdk.svg?style=flat-square)](https://packagist.org/packages/rickkuilman/digital-humani-sdk)
[![Total Downloads](https://img.shields.io/packagist/dt/rickkuilman/digital-humani-sdk.svg?style=flat-square)](https://packagist.org/packages/rickkuilman/digital-humani-sdk)
![GitHub Actions](https://github.com/rickkuilman/digital-humani-sdk/actions/workflows/main.yml/badge.svg)
![](https://user-images.githubusercontent.com/7881219/136075581-dff52c1f-d99d-459e-8ca7-734082daa686.png)

[![Latest Version on Packagist](https://img.shields.io/packagist/v/rickkuilman/digital-humani-php-sdk.svg?style=flat-square)](https://packagist.org/packages/rickkuilman/digital-humani-php-sdk) [![Total Downloads](https://img.shields.io/packagist/dt/rickkuilman/digital-humani-php-sdk.svg?style=flat-square)](https://packagist.org/packages/rickkuilman/digital-humani-php-sdk) ![GitHub Actions](https://github.com/rickkuilman/digital-humani-php-sdk/actions/workflows/main.yml/badge.svg)

Unofficial PHP SDK for DigitalHumani's RaaS (Reforestation-as-a-Service)

Expand All @@ -11,28 +11,25 @@ Unofficial PHP SDK for DigitalHumani's RaaS (Reforestation-as-a-Service)
You can install the package via composer:

```bash
composer require rickkuilman/digital-humani-sdk
composer require rickkuilman/digital-humani-php-sdk
```

## Preparation

- Create an account on [DigitalHumani,com](https://my.digitalhumani.com/register) and grab your API key from the "
Developer" menu item. You use the sandbox mode by default.

- Publish the config file and update `.env` file if needed.

```bash
php artisan vendor:publish --provider="Rickkuilman\DigitalHumaniSdk\DigitalHumaniServiceProvider" --tag=config
```
- Create a [sandbox](https://my.sandbox.digitalhumani.com/register) or [production](https://my.digitalhumani.com/register) account on DigitalHumani.com.
- Grab your Enterprise ID and API Key from the "Developer" tab.

## Basic Usage

```php
$digitalHumani = new DigitalHumani(API_TOKEN);
// Create new instance
$digitalHumani = new DigitalHumani($apiKey);

// Plant a tree
$digitalHumani->plantTree($organisationId, 'rick@example.com');
```

Using the `DigitalHumani` instance you may perform multiple actions as well as retrieve the different
resources [DigitalHumani's API](https://digitalhumani.com/docs/) provides:
Using the `DigitalHumani` instance you may perform multiple actions as well as retrieve the different resources [DigitalHumani's API](https://digitalhumani.com/docs/) provides:

### Managing Enterprises

Expand Down Expand Up @@ -72,7 +69,17 @@ $digitalHumani->tree('9f05511e-56c6-40f7-b5ca-e25567991dc1');
$digitalHumani->countTreesPlantedByUser('4c6e672d', 'rick@example.com');
```

Happy planting! 🌳🌳🌳
### Switch to production environment

```php
// Set the second parameter to "true"
$digitalHumani = new DigitalHumani($apiKey, true);

// ..or use a method
$digitalHumani->useProductionEnvironment();
```

Happy planting! 🌳

## Changelog

Expand All @@ -95,6 +102,6 @@ If you discover any security related issues, please email rickkuilman@gmail.com

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

## Laravel Package Boilerplate
## PHP Package Boilerplate

This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com). ❤️
This package was generated using the [PHP Package Boilerplate](https://laravelpackageboilerplate.com) by [Beyond Code](http://beyondco.de/).
24 changes: 6 additions & 18 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "rickkuilman/digital-humani-sdk",
"name": "rickkuilman/digital-humani-php-sdk",
"description": "PHP SDK for DigitalHumani's RaaS (Reforestation-as-a-Service)",
"keywords": [
"rickkuilman",
"digital-humani-sdk"
"digital-humani-php-sdk"
],
"homepage": "https://github.com/rickkuilman/digital-humani-sdk",
"homepage": "https://github.com/rickkuilman/digital-humani-php-sdk",
"license": "MIT",
"type": "library",
"authors": [
Expand All @@ -16,21 +16,19 @@
}
],
"require": {
"php": "^7.4|^8.0",
"illuminate/support": "^8.0"
"php": "^7.4|^8.0"
},
"require-dev": {
"orchestra/testbench": "^6.0",
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"Rickkuilman\\DigitalHumaniSdk\\": "src"
"Rickkuilman\\DigitalHumaniPhpSdk\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Rickkuilman\\DigitalHumaniSdk\\Tests\\": "tests"
"Rickkuilman\\DigitalHumaniPhpSdk\\Tests\\": "tests"
}
},
"scripts": {
Expand All @@ -39,15 +37,5 @@
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Rickkuilman\\DigitalHumaniSdk\\DigitalHumaniServiceProvider"
],
"aliases": {
"DigitalHumaniSdk": "Rickkuilman\\DigitalHumaniSdk\\DigitalHumaniSdkFacade"
}
}
}
}
33 changes: 0 additions & 33 deletions config/config.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Actions/ManagesEnterprises.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk\Actions;
namespace Rickkuilman\DigitalHumaniPhpSdk\Actions;

use Carbon\Carbon;
use Rickkuilman\DigitalHumaniSdk\Resources\Enterprise;
use Rickkuilman\DigitalHumaniPhpSdk\Resources\Enterprise;

trait ManagesEnterprises
{
Expand Down
4 changes: 2 additions & 2 deletions src/Actions/ManagesProjects.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk\Actions;
namespace Rickkuilman\DigitalHumaniPhpSdk\Actions;

use Rickkuilman\DigitalHumaniSdk\Resources\Project;
use Rickkuilman\DigitalHumaniPhpSdk\Resources\Project;

trait ManagesProjects
{
Expand Down
6 changes: 3 additions & 3 deletions src/Actions/ManagesTrees.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk\Actions;
namespace Rickkuilman\DigitalHumaniPhpSdk\Actions;

use Rickkuilman\DigitalHumaniSdk\Resources\Tree;
use Rickkuilman\DigitalHumaniPhpSdk\Resources\Tree;

trait ManagesTrees
{
Expand All @@ -24,7 +24,7 @@ public function plantTree(string $enterpriseId, string $user, int $amount = 1, s
}

return new Tree($this->post('tree', [
'enterpriseId' => '4c6e672d',
'enterpriseId' => $enterpriseId,
'user' => $user,
'treeCount' => $amount,
'projectId' => (string)$projectId,
Expand Down
63 changes: 51 additions & 12 deletions src/DigitalHumani.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk;
namespace Rickkuilman\DigitalHumaniPhpSdk;

use GuzzleHttp\Client as HttpClient;
use Rickkuilman\DigitalHumaniSdk\Actions\ManagesEnterprises;
use Rickkuilman\DigitalHumaniSdk\Actions\ManagesProjects;
use Rickkuilman\DigitalHumaniSdk\Actions\ManagesTrees;
use Rickkuilman\DigitalHumaniPhpSdk\Actions\ManagesEnterprises;
use Rickkuilman\DigitalHumaniPhpSdk\Actions\ManagesProjects;
use Rickkuilman\DigitalHumaniPhpSdk\Actions\ManagesTrees;

class DigitalHumani
{
Expand All @@ -32,15 +32,24 @@ class DigitalHumani
*/
public HttpClient $guzzle;

/**
* Base URL of Digital Humani
*
* @var string
*/
public string $baseUrl;

/**
* Create a new Forge instance.
*
* @param string|null $apiKey
* @param HttpClient|null $guzzle
* @return void
*/
public function __construct(string $apiKey = null, HttpClient $guzzle = null)
public function __construct(string $apiKey = null, bool $useProduction = false, HttpClient $guzzle = null)
{
$this->useProductionEnvironment($useProduction);

if (!is_null($apiKey)) {
$this->setApiKey($apiKey, $guzzle);
}
Expand All @@ -61,13 +70,8 @@ public function setApiKey(string $apiKey, HttpClient $guzzle = null): DigitalHum
{
$this->apiKey = $apiKey;

$baseUrl = self::SANDBOX_URL;
if (config('digital-humani.use_production')) {
$baseUrl = self::PRODUCTION_URL;
}

$this->guzzle = $guzzle ?: new HttpClient([
'base_uri' => $baseUrl,
'base_uri' => $this->baseUrl,
'headers' => [
'X-Api-Key' => $this->apiKey,
'Accept' => 'application/json',
Expand All @@ -85,7 +89,7 @@ public function setApiKey(string $apiKey, HttpClient $guzzle = null): DigitalHum
*/
protected function defaultProjectId(): string
{
return config('digital-humani.default_project') ?? self::DEFAULT_PROJECT_ID;
return self::DEFAULT_PROJECT_ID;
}

/**
Expand All @@ -102,5 +106,40 @@ protected function transformCollection(array $collection, $class, array $extraDa
return new $class($data + $extraData, $this);
}, $collection);
}

/**
* Use the production environment
*
* @param bool $useProduction
*/
public function useProductionEnvironment(bool $useProduction = true)
{
$useProduction
? $this->setBaseUrl(self::PRODUCTION_URL)
: $this->useSandboxEnvironment();
}

/**
* Use the sandbox environment
*
* @param bool $useSandbox
*/
public function useSandboxEnvironment(bool $useSandbox = true)
{
$useSandbox
? $this->setBaseUrl(self::SANDBOX_URL)
: $this->useProductionEnvironment();
}

/**
* Set the base url
*
* @param string $baseUrl
*/
public function setBaseUrl(string $baseUrl)
{
$this->baseUrl = $baseUrl;
}

}

33 changes: 0 additions & 33 deletions src/DigitalHumaniServiceProvider.php

This file was deleted.

3 changes: 1 addition & 2 deletions src/MakesHttpRequests.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk;
namespace Rickkuilman\DigitalHumaniPhpSdk;

use Exception;
use Psr\Http\Message\ResponseInterface;

trait MakesHttpRequests
{
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Enterprise.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk\Resources;
namespace Rickkuilman\DigitalHumaniPhpSdk\Resources;

use Carbon\Carbon;

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Project.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rickkuilman\DigitalHumaniSdk\Resources;
namespace Rickkuilman\DigitalHumaniPhpSdk\Resources;

class Project extends Resource
{
Expand Down
Loading

0 comments on commit 7c48d8e

Please sign in to comment.