Skip to content

Commit

Permalink
v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
allanvb committed Apr 4, 2023
0 parents commit 6170f95
Show file tree
Hide file tree
Showing 22 changed files with 1,539 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor
.idea
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2023 MerchOne

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 the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 changes: 33 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "merch-one/php-api-sdk",
"description": "SDK for MerchOne API integration",
"keywords": [
"merch-one",
"the customization group",
"api",
"sdk",
"integration",
"print on demand"
],
"type": "library",
"license": "MIT",
"version": "1.0.0",
"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": "^6|^7",
"tightenco/collect": "^8.83",
"ext-json": "*",
"ext-ctype": "*"
},
"autoload": {
"psr-4": {
"MerchOne\\PhpSdk\\": "src/"
}
},
"authors": [
{
"name": "The Customization Group"
}
],
"minimum-stability": "dev"
}
107 changes: 107 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<h2 align="center">
PHP SDK for MerchOne API integration
</h2>

This package provide a set of tools that allow developers to easily integrate with MerchOne API.

## Installation
```shell
composer require merch-one/php-sdk
```

## Overview

- [Introduction](#introduction)
- [Basic Usage](#basic-usage)
- [Helpers](#helpers)
- [Exceptions](#exceptions)

---

### Introduction
**Client provide 3 different API's to interact with.**
- Catalog API
- Orders API
- Shipping API

**To get the list of available endpoints, please check
[MerchOne API Documentation](https://docs.merchone.com/api-reference)**

---

### Basic Usage

**Create an instance of `MerchOne\PhpSdk\Http\Client`**

```php
use MerchOne\PhpSdk\Http\Client;

class MyService
{
private Client $httpClient;

public function __construct()
{
$this->httpClient = new Client();
}

public function doSomething(): void
{
// authenticate client using credentials
$this->httpClient->auth(
'your-store-user',
'your-store-key'
);

// or authenticate client using base64 encoded credentials
$this->httpClient->basicAuth(
base64_encode('your-store-user:your-store-key'),
);

/* Interact with Catalog API */
/** @var \MerchOne\PhpSdk\Contracts\Clients\CatalogApi $catalogApi */
$catalogApi = $this->httpClient->catalog();

/* Interact with Orders API */
/** @var \MerchOne\PhpSdk\Contracts\Clients\OrdersApi $ordersApi */
$ordersApi = $this->httpClient->orders();

/* Interact with Shipping API */
/** @var \MerchOne\PhpSdk\Contracts\Clients\ShippingApi $shippingApi */
$shippingApi = $this->httpClient->shipping();

// switch API version you interact with
$this->httpClient->setVersion($version);

// get current API version
$this->httpClient->getVersion();
}
}
```

---

### Helpers

```php
use MerchOne\PhpSdk\Util\MerchOneApi;

// get the list of all available API versions
MerchOneApi::getVersions();
```
- Class `MerchOne\PhpSdk\Util\OrderStatus` provides a full list of Order statuses.

Check more in [MerchOne API Documentation](https://docs.merchone.com/api-reference/orders#order-status)

---

### Exceptions

The package can throw the following exceptions:

| Exception | Reason |
|-------------------------------|-----------------------------------------------------|
| *MerchOneApiClientException* | Request is not correct or validation did not pass. |
| *MerchOneApiServerException* | A server error occurred. |
| *InvalidApiVersionException* | An invalid API version was provided to the Client. |
| *InvalidCredentialsException* | Invalid API credentials was provided to the Client. |
100 changes: 100 additions & 0 deletions src/Clients/BaseApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace MerchOne\PhpSdk\Clients;

use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Response as PsrResponse;
use MerchOne\PhpSdk\Exceptions\InvalidCredentialsException;
use MerchOne\PhpSdk\Exceptions\MerchOneApiClientException;
use MerchOne\PhpSdk\Exceptions\MerchOneApiServerException;
use MerchOne\PhpSdk\Http\Response;
use MerchOne\PhpSdk\Util\Data;
use Tightenco\Collect\Support\Collection;
use Tightenco\Collect\Support\Enumerable;

abstract class BaseApiClient
{
/**
* @var GuzzleClient
*/
protected GuzzleClient $httpClient;

/**
* @param GuzzleClient $httpClient
*/
public function __construct(GuzzleClient $httpClient)
{
$this->httpClient = $httpClient;
}

/**
* @param string $method
* @param string $path
* @param array $options
* @param string|null $responseKey
* @return Enumerable
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
protected function request(
string $method,
string $path,
array $options = [],
?string $responseKey = 'data'
): Enumerable {
try {
$response = $this->handleResponse(
$this->httpClient->request($method, $path, $options)
);

return new Data($response->json($responseKey));
} catch (GuzzleException $exception) {
throw new MerchOneApiClientException($exception->getMessage());
}
}

/**
* @param PsrResponse $psrResponse
* @return Response
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
protected function handleResponse(PsrResponse $psrResponse): Response
{
return (new Response($psrResponse))
->onError(fn (Response $response) => $this->handleError($response));
}

/**
* @param Response $response
* @return void
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
protected function handleError(Response $response): void
{
if ($response->serverError()) {
throw new MerchOneApiServerException($response->json('message'));
}

if ($response->unauthorized()) {
throw new InvalidCredentialsException('Invalid API credentials.');
}

if ($response->clientError()) {
$errors = $response->json('errors');

if ($errors) {
throw new MerchOneApiClientException(
(new Collection($errors))->collapse()->implode('|')
);
}

throw new MerchOneApiClientException($response->json('message'));
}
}
}
59 changes: 59 additions & 0 deletions src/Clients/Beta/Catalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace MerchOne\PhpSdk\Clients\Beta;

use MerchOne\PhpSdk\Clients\BaseApiClient;
use MerchOne\PhpSdk\Contracts\Clients\CatalogApi;
use MerchOne\PhpSdk\Exceptions\MerchOneApiClientException;
use MerchOne\PhpSdk\Exceptions\MerchOneApiServerException;
use Tightenco\Collect\Support\Enumerable;

class Catalog extends BaseApiClient implements CatalogApi
{
/**
* @return Enumerable
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
public function getProducts(): Enumerable
{
return $this->request('GET', 'products');
}

/**
* @param int $productID
* @return Enumerable
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
public function getProductVariants(int $productID): Enumerable
{
return $this->request('GET', 'products/' . $productID);
}

/**
* @param int $variantID
* @return Enumerable
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
public function getVariantOptions(int $variantID): Enumerable
{
return $this->request('GET', 'variants/' . $variantID);
}

/**
* @param int $variantID
* @return Enumerable
*
* @throws MerchOneApiClientException
* @throws MerchOneApiServerException
*/
public function getVariantCombinations(int $variantID): Enumerable
{
return $this->request('GET', "variants/{$variantID}/combinations");
}
}
Loading

0 comments on commit 6170f95

Please sign in to comment.