Skip to content

Commit

Permalink
Merge pull request #5 from amir-shadanfar/dev
Browse files Browse the repository at this point in the history
add service module
  • Loading branch information
amir-shadanfar committed Jul 9, 2021
2 parents 187ca0b + 739c1e7 commit 7ad5d47
Show file tree
Hide file tree
Showing 3 changed files with 251 additions and 91 deletions.
94 changes: 93 additions & 1 deletion src/Modules/AbstractModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Suite\Suite\Modules;

use Illuminate\Support\Facades\Http;
use Suite\Suite\Auth;
use Suite\Suite\Exceptions\SuiteException;
use Suite\Suite\Models\Token;

/**
Expand Down Expand Up @@ -39,7 +41,97 @@ public function __construct(Token $token)
/**
* @return string
*/
protected function getAccessToken(){
protected function getAccessToken()
{
return $this->token->getAccessToken();
}

/**
* @param string $url
* @param string $moduleName
*
* @return array|mixed
* @throws \Suite\Suite\Exceptions\SuiteException
*/
public function get(string $url, string $moduleName)
{
$response = Http::acceptJson()
->withToken($this->getAccessToken())
->get($url);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling all $moduleName" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
}

/**
* @param string $url
* @param string $moduleName
* @param array $data
*
* @return array|mixed
* @throws \Suite\Suite\Exceptions\SuiteException
*/
public function post(string $url, string $moduleName, array $data)
{
$response = Http::acceptJson()
->withToken($this->getAccessToken())
->post($url, $data);

if ($response->status() == 201) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling create in $moduleName" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
}

/**
* @param string $url
* @param string $moduleName
* @param array $data
*
* @return array|mixed
* @throws \Suite\Suite\Exceptions\SuiteException
*/
public function put(string $url, string $moduleName, array $data)
{
$route = path_join($this->url, $id);
$url = path_join($this->baseUrl, $route);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->put($url, $data);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling update in $moduleName" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
}

/**
* @param string $url
* @param string $moduleName
*
* @return array|mixed
* @throws \Suite\Suite\Exceptions\SuiteException
*/
public function delete(string $url, string $moduleName)
{
$response = Http::acceptJson()
->withToken($this->getAccessToken())
->delete($url);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling delete in $moduleName" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
}
}
123 changes: 33 additions & 90 deletions src/Modules/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

namespace Suite\Suite\Modules;

use Illuminate\Support\Facades\Http;
use Suite\Suite\Auth;
use Suite\Suite\Exceptions\SuiteException;
use Suite\Suite\Constants\ModulesType;
use Suite\Suite\Models\Token;
use Suite\Suite\Suite;

/**
* Class Customer
* @package Suite\Suite\Modules
*/
class Customer extends AbstractModule
{

/**
* @var string
*/
protected string $moduleName = ModulesType::CUSTOMER;

/**
* @var string
*/
protected string $url;

/**
* Customer constructor.
*
Expand All @@ -19,7 +30,7 @@ class Customer extends AbstractModule
public function __construct(Token $token)
{
parent::__construct($token);
$this->url = sprintf('api/%s/customers', $this->apiVersion);
$this->url = path_join($this->baseUrl, sprintf('api/%s/customers', $this->apiVersion));
}

/**
Expand All @@ -28,18 +39,7 @@ public function __construct(Token $token)
*/
public function all()
{
$url = path_join($this->baseUrl, $this->url);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->get($url);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling all in customer" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
return parent::get($this->url, $this->moduleName);
}

/**
Expand All @@ -52,22 +52,11 @@ public function all()
*/
public function create(string $name, string $workspace, array $services)
{
$url = path_join($this->baseUrl, $this->url);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->post($url, [
'name' => $name,
'workspace' => $workspace,
'services' => $services,
]);

if ($response->status() == 201) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling create in customer" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
return parent::post($this->url, $this->moduleName, [
'name' => $name,
'workspace' => $workspace,
'services' => $services,
]);
}

/**
Expand All @@ -78,19 +67,7 @@ public function create(string $name, string $workspace, array $services)
*/
public function show(int $id)
{
$route = path_join($this->url, $id);
$url = path_join($this->baseUrl, $route);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->get($url);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling show in customer" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
return parent::get(path_join($this->url, $id), $this->moduleName);
}

/**
Expand All @@ -104,23 +81,11 @@ public function show(int $id)
*/
public function update(int $id, string $name, string $workspace, array $services)
{
$route = path_join($this->url, $id);
$url = path_join($this->baseUrl, $route);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->put($url, [
'name' => $name,
'workspace' => $workspace,
'services' => $services,
]);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling update in customer" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
return parent::put(path_join($this->url, $id), $this->moduleName, [
'name' => $name,
'workspace' => $workspace,
'services' => $services,
]);
}

/**
Expand All @@ -129,21 +94,9 @@ public function update(int $id, string $name, string $workspace, array $services
* @return array|mixed
* @throws \Suite\Suite\Exceptions\SuiteException
*/
public function delete(int $id)
public function destroy(int $id)
{
$route = path_join($this->url, $id);
$url = path_join($this->baseUrl, $route);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->delete($url);

if ($response->status() == 200) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling delete in customer" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
return parent::delete(path_join($this->url, $id), $this->moduleName);
}

/**
Expand All @@ -155,18 +108,8 @@ public function delete(int $id)
*/
public function syncServices(int $id, array $services)
{
$route = path_join($this->url, $id . '/services');
$url = path_join($this->baseUrl, $route);

$response = Http::acceptJson()
->withToken($this->getAccessToken())
->put($url, ['services' => $services]);

if ($response->ok()) {
return $response->json();
} else {
$message = is_array($response->json()['message']) ? "Error in calling syncing service in customer" : $response->json()['message'];
throw new SuiteException($message, $response->json(), $response->status());
}
return parent::post(path_join($this->url, $id . '/services'), $this->moduleName, [
'services' => $services
]);
}
}
Loading

0 comments on commit 7ad5d47

Please sign in to comment.