Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
adityakdevin committed Sep 11, 2020
1 parent 666df4a commit 1966e72
Show file tree
Hide file tree
Showing 21 changed files with 936 additions and 0 deletions.
61 changes: 61 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "adityafullstackdeveloper/getgologisticsapi",
"description": "Getgologisticsapi API (V1) Laravel SDK.",
"keywords": [
"getgologisticsapi",
"laravel getgologisticsapi Api",
"Laravel getgologisticsapi Wrapper",
"getgologisticsapi SDK Module"
],
"homepage": "https://github.com/adityafullstackdeveloper/getgologisticsapi",
"license": "MIT",
"authors": [
{
"name": "adityafullstackdeveloper",
"email": "adityafullstackdeveloper@gmail.com",
"homepage": "https://github.com/adityafullstackdeveloper",
"role": "Developer"
}
],
"require": {
"php": "^7.2.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.16",
"orchestra/testbench": "^5.0",
"phpunit/phpunit": "^7.5|^9.0",
"psalm/plugin-laravel": "^1.2",
"vimeo/psalm": "^3.11"
},
"autoload": {
"psr-4": {
"Adityafullstackdeveloper\\GetGoLogistics\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Adityafullstackdeveloper\\GetGoLogistics\\Tests\\": "tests"
}
},
"scripts": {
"psalm": "vendor/bin/psalm",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"format": "vendor/bin/php-cs-fixer fix --allow-risky=yes"
},
"config": {
"sort-packages": true
},
"extra": {
"laravel": {
"providers": [
"Adityafullstackdeveloper\\GetGoLogistics\\ShiprocketServiceProvider"
],
"aliases": {
"Shiprocket": "Adityafullstackdeveloper\\GetGoLogistics\\GetGoLogistics"
}
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
31 changes: 31 additions & 0 deletions config/getgologistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Default Shiprocket Credentilas
|--------------------------------------------------------------------------
|
| Here you can set the default shiprocket credentilas. However, you can pass the credentials while connecting to shiprocket client
|
*/

'credentials' => [
'email' => env('SHIPROCKET_EMAIL', 'youemail@email.com'),
'password' => env('SHIPROCKET_PASSWORD', 'secret'),
],


/*
|--------------------------------------------------------------------------
| Default output response type
|--------------------------------------------------------------------------
|
| Here you may specify which of the output response you need.
|
| Supported: "collection" , "object", "array"
|
*/

'responseType' => 'collection',
];
15 changes: 15 additions & 0 deletions src/Clients/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Adityafullstackdeveloper\GetGoLogistics\Clients;

interface Client
{
public function setEndpoint(string $endpoint);

public function setHeaders(string $token);

public function get();

public function post(array $data);

public function patch(array $data);
}
158 changes: 158 additions & 0 deletions src/Clients/GetGoLogisticsApiClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
<?php
namespace Adityafullstackdeveloper\GetGoLogisticsApi\Clients;

class GetGoLogisticsApiClient implements Client
{
protected $url = 'https://apiv2.shiprocket.in/v1/external/';

protected $endpoint;

protected $headers;

protected $responseType;

public function __construct()
{
$this->responseType = config('getgologistics.responseType');
}

/**
* set the endpoint
*
* @param string $endpoint
* @return object $this
*/
public function setEndpoint(string $endpoint) :object
{
$this->endpoint = $this->url . $endpoint;

return $this;
}

/**
* set the header
*
* @param string $token
* @return object
*/
public function setHeaders(string $token) :object
{
$this->headers = [ "Content-Type: application/json" ];
if ($token != 'login') {
array_push($this->headers, "Authorization: Bearer {$token}");
}

return $this;
}

/**
* Send the data using post request
*
* @param array $data
* @return mixed
*/
public function post(array $data, $type = "POST")
{
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => $this->endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $type,
CURLOPT_POSTFIELDS => json_encode($data),
CURLOPT_HTTPHEADER => $this->headers,
]);

$response = curl_exec($curl);

if (! $this->isValid($response)) {
$response = json_encode([ 'curl_error' => curl_error($curl) ]);
}

curl_close($curl);

return $this->responseType($response);
}

/**
* Send a data using PATCH Request
*
* @param array $data
* @return mixed
*/
public function patch(array $data)
{
return $this->post($data, 'PATCH');
}

/**
* get the requested data using get request
*
* @return mixed
*/
public function get()
{
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => $this->endpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => $this->headers,
]);

$response = curl_exec($curl);

if (! $this->isValid($response)) {
$response = json_encode(['curl_error' => curl_error($curl)]);
}

curl_close($curl);

return $this->responseType($response);
}

/**
* Check the return data is valid
*
* @param mixed $string
* @return bool
*/
private function isValid($string) :bool
{
if (! $string) {
return false;
}

return json_decode($string) ? true : false;
}

/**
* Return the response type based on config responseType
*
* @param $response
* @return mixed
*/
private function responseType($response)
{
if ($this->responseType == 'collection') {
return collect(json_decode($response, true));
}

if ($this->responseType == 'object') {
return json_decode($response);
}

return json_decode($response, true);
}
}
17 changes: 17 additions & 0 deletions src/Commands/SketchCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Adityafullstackdeveloper\GetGoLogistics\Commands;

use Illuminate\Console\Command;

class SketchCommand extends Command
{
public $signature = 'sketch';

public $description = 'My command';

public function handle()
{
$this->comment('All done');
}
}
8 changes: 8 additions & 0 deletions src/Exceptions/GetGoLogisticsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
namespace Adityafullstackdeveloper\GetGoLogistics\Exceptions;

use Exception;

class GetGoLogisticsException extends Exception
{
}
16 changes: 16 additions & 0 deletions src/GetGoLogistics.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Adityafullstackdeveloper\GetGoLogistics;

use Illuminate\Support\Facades\Facade;

/**
* @see \Seshac\GetGoLogistics\GetGoLogisticsApi
*/
class GetGoLogistics extends Facade
{
protected static function getFacadeAccessor()
{
return 'getgologistics';
}
}
Loading

0 comments on commit 1966e72

Please sign in to comment.