Skip to content

Commit

Permalink
Merge pull request #32 from Astrotomic/saloon-v3
Browse files Browse the repository at this point in the history
upgrade to saloon v3
  • Loading branch information
Gummibeer authored Feb 7, 2024
2 parents cce7ccf + c7c62d7 commit a6ed6cc
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 236 deletions.
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,13 @@
"require": {
"php": "^8.1",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.5.5 || ^7.0.1",
"moneyphp/money": "^4.0.5",
"sammyjo20/saloon": "^1.4.4"
"saloonphp/saloon": "^3.0"
},
"require-dev": {
"laravel/pint": "^1.2",
"pestphp/pest": "^1.21",
"phpstan/phpstan": "^1.8"
"laravel/pint": "^1.13.10",
"pestphp/pest": "^2.33.4",
"phpstan/phpstan": "^1.10.57"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 0 additions & 4 deletions src/Data/CarbonOffsetPurchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
class CarbonOffsetPurchase
{
/**
* @param float $number
* @param float $numberInTonnes
* @param float $amount
* @param non-empty-string $currency
* @param array $projectDetails
*/
final public function __construct(
public readonly float $number,
Expand Down
4 changes: 0 additions & 4 deletions src/Data/TreePurchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
class TreePurchase
{
/**
* @param int $trees
* @param float $amount
* @param non-empty-string $currency
* @param string $treeUrl
* @param string|null $name
*/
final public function __construct(
public readonly int $trees,
Expand Down
29 changes: 13 additions & 16 deletions src/Ecologi.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,30 @@

namespace Astrotomic\Ecologi;

use Astrotomic\Ecologi\RequestCollections\Purchasing;
use Astrotomic\Ecologi\RequestCollections\Reporting;
use Astrotomic\Ecologi\Responses\EcologiResponse;
use Sammyjo20\Saloon\Http\Auth\TokenAuthenticator;
use Sammyjo20\Saloon\Http\SaloonConnector;
use Sammyjo20\Saloon\Interfaces\AuthenticatorInterface;
use Sammyjo20\Saloon\Traits\Plugins\AcceptsJson;
use Sammyjo20\Saloon\Traits\Plugins\AlwaysThrowsOnErrors;

class Ecologi extends SaloonConnector
use Astrotomic\Ecologi\Resources\Purchasing;
use Astrotomic\Ecologi\Resources\Reporting;
use Saloon\Contracts\Authenticator;
use Saloon\Http\Auth\TokenAuthenticator;
use Saloon\Http\Connector;
use Saloon\Traits\Plugins\AcceptsJson;
use Saloon\Traits\Plugins\AlwaysThrowOnErrors;

class Ecologi extends Connector
{
use AcceptsJson;
use AlwaysThrowsOnErrors;

protected ?string $response = EcologiResponse::class;
use AlwaysThrowOnErrors;

public function __construct(
protected string $token
) {
}

public function defineBaseUrl(): string
public function resolveBaseUrl(): string
{
return 'https://public.ecologi.com';
}

public function defaultAuth(): ?AuthenticatorInterface
public function defaultAuth(): ?Authenticator
{
return new TokenAuthenticator($this->token);
}
Expand All @@ -47,6 +44,6 @@ public function reporting(): Reporting

public function purchasing(bool $test = false): Purchasing
{
return new Purchasing($this, $test);
return (new Purchasing($this))->test($test);
}
}
27 changes: 0 additions & 27 deletions src/Exceptions/BadResponseException.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exceptions/ClientException.php

This file was deleted.

7 changes: 0 additions & 7 deletions src/Exceptions/ServerException.php

This file was deleted.

26 changes: 12 additions & 14 deletions src/Requests/Purchasing/BuyCarbonOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
namespace Astrotomic\Ecologi\Requests\Purchasing;

use Astrotomic\Ecologi\Data\CarbonOffsetPurchase;
use Astrotomic\Ecologi\Ecologi;
use Astrotomic\Ecologi\Enums\CarbonUnit;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
use Sammyjo20\Saloon\Traits\Plugins\HasJsonBody;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;
use Saloon\Traits\Request\CreatesDtoFromResponse;

/**
* @link https://docs.ecologi.com/docs/public-api-docs/e07bbee7fa605-purchase-carbon-offsets
*/
class BuyCarbonOffset extends SaloonRequest
class BuyCarbonOffset extends Request implements HasBody
{
use CastsToDto;
use CreatesDtoFromResponse;
use HasJsonBody;

protected ?string $connector = Ecologi::class;

protected ?string $method = Saloon::POST;
protected Method $method = Method::POST;

public function __construct(
public readonly float $number,
Expand All @@ -31,7 +29,7 @@ public function __construct(
) {
}

public function defineEndpoint(): string
public function resolveEndpoint(): string
{
return '/impact/carbon';
}
Expand All @@ -43,7 +41,7 @@ public function defaultHeaders(): array
]);
}

public function defaultData(): array
public function defaultBody(): array
{
return array_filter([
'number' => $this->number,
Expand All @@ -52,7 +50,7 @@ public function defaultData(): array
]);
}

protected function castToDto(SaloonResponse $response): CarbonOffsetPurchase
public function createDtoFromResponse(Response $response): CarbonOffsetPurchase
{
return CarbonOffsetPurchase::fromArray($response->json());
}
Expand Down
25 changes: 12 additions & 13 deletions src/Requests/Purchasing/BuyTrees.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,22 @@

use Astrotomic\Ecologi\Data\TreePurchase;
use Astrotomic\Ecologi\Ecologi;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
use Sammyjo20\Saloon\Traits\Plugins\HasJsonBody;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;
use Saloon\Traits\Request\CreatesDtoFromResponse;

/**
* @link https://docs.ecologi.com/docs/public-api-docs/004342d262f93-purchase-trees
*/
class BuyTrees extends SaloonRequest
class BuyTrees extends Request implements HasBody
{
use CastsToDto;
use CreatesDtoFromResponse;
use HasJsonBody;

protected ?string $connector = Ecologi::class;

protected ?string $method = Saloon::POST;
protected Method $method = Method::POST;

public function __construct(
public readonly int $number,
Expand All @@ -30,7 +29,7 @@ public function __construct(
) {
}

public function defineEndpoint(): string
public function resolveEndpoint(): string
{
return '/impact/trees';
}
Expand All @@ -42,7 +41,7 @@ public function defaultHeaders(): array
]);
}

public function defaultData(): array
public function defaultBody(): array
{
return array_filter([
'number' => $this->number,
Expand All @@ -51,7 +50,7 @@ public function defaultData(): array
]);
}

protected function castToDto(SaloonResponse $response): TreePurchase
public function createDtoFromResponse(Response $response): TreePurchase
{
return TreePurchase::fromArray($this->number, $response->json());
}
Expand Down
20 changes: 9 additions & 11 deletions src/Requests/Reporting/GetCarbonOffset.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@
namespace Astrotomic\Ecologi\Requests\Reporting;

use Astrotomic\Ecologi\Ecologi;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Request\CreatesDtoFromResponse;

/**
* @link https://docs.ecologi.com/docs/public-api-docs/6046ba6f68449-get-total-tonnes-of-co-2e-offset
*/
class GetCarbonOffset extends SaloonRequest
class GetCarbonOffset extends Request
{
use CastsToDto;
use CreatesDtoFromResponse;

protected ?string $connector = Ecologi::class;

protected ?string $method = Saloon::GET;
protected Method $method = Method::GET;

public function __construct(
public readonly string $username
) {
}

public function defineEndpoint(): string
public function resolveEndpoint(): string
{
return "/users/{$this->username}/carbon-offset";
}

protected function castToDto(SaloonResponse $response): float
public function createDtoFromResponse(Response $response): float
{
return (float) $response->json('total');
}
Expand Down
20 changes: 9 additions & 11 deletions src/Requests/Reporting/GetImpact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,31 @@

use Astrotomic\Ecologi\Data\Impact;
use Astrotomic\Ecologi\Ecologi;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Request\CreatesDtoFromResponse;

/**
* @link https://docs.ecologi.com/docs/public-api-docs/0eb5caf374377-get-total-impact
*/
class GetImpact extends SaloonRequest
class GetImpact extends Request
{
use CastsToDto;
use CreatesDtoFromResponse;

protected ?string $connector = Ecologi::class;

protected ?string $method = Saloon::GET;
protected Method $method = Method::GET;

public function __construct(
public readonly string $username
) {
}

public function defineEndpoint(): string
public function resolveEndpoint(): string
{
return "/users/{$this->username}/impact";
}

protected function castToDto(SaloonResponse $response): Impact
public function createDtoFromResponse(Response $response): Impact
{
return Impact::fromArray($response->json());
}
Expand Down
20 changes: 9 additions & 11 deletions src/Requests/Reporting/GetTrees.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,31 @@
namespace Astrotomic\Ecologi\Requests\Reporting;

use Astrotomic\Ecologi\Ecologi;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Http\SaloonResponse;
use Sammyjo20\Saloon\Traits\Plugins\CastsToDto;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Request\CreatesDtoFromResponse;

/**
* @link https://docs.ecologi.com/docs/public-api-docs/2531efb510c5b-get-total-number-of-trees
*/
class GetTrees extends SaloonRequest
class GetTrees extends Request
{
use CastsToDto;
use CreatesDtoFromResponse;

protected ?string $connector = Ecologi::class;

protected ?string $method = Saloon::GET;
protected Method $method = Method::GET;

public function __construct(
public readonly string $username
) {
}

public function defineEndpoint(): string
public function resolveEndpoint(): string
{
return "/users/{$this->username}/trees";
}

protected function castToDto(SaloonResponse $response): int
public function createDtoFromResponse(Response $response): int
{
return (int) $response->json('total');
}
Expand Down
Loading

0 comments on commit a6ed6cc

Please sign in to comment.