Skip to content

Commit 8f303e7

Browse files
author
Hafiizh Ghulam
committed
Adding new routes & add readme
1 parent c7d0cb2 commit 8f303e7

File tree

4 files changed

+159
-3
lines changed

4 files changed

+159
-3
lines changed

README.md

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<div align="center">
2+
3+
![GitHub top language](https://img.shields.io/github/languages/top/crenata/affiliate-connector)
4+
![GitHub all releases](https://img.shields.io/github/downloads/crenata/affiliate-connector/total)
5+
![GitHub issues](https://img.shields.io/github/issues/crenata/affiliate-connector)
6+
![GitHub](https://img.shields.io/github/license/crenata/affiliate-connector)
7+
![GitHub release (latest by date including pre-releases)](https://img.shields.io/github/v/release/crenata/affiliate-connector?display_name=tag&include_prereleases)
8+
9+
</div>
10+
11+
# Connector
12+
A Laravel package as connector between Affiliate with Products.
13+
14+
## Warning
15+
This package for internal purposes, any incoming issues won't be responded.
16+
17+
## Installation
18+
Run these command into your laravel project folder.
19+
```
20+
composer require crenata/affiliate-connector
21+
```
22+
23+
### Publish Configuration
24+
You need to publish the package's config. To publish configuration, run the following command.
25+
```
26+
php artisan vendor:publish --tag=connector-config
27+
```
28+
29+
### Generate Authentication Bridge
30+
To secure the bridge, You must generate `secret key` and `private key` to your `server` and `client` project. To generate the key, run the following command.
31+
```
32+
php artisan connector:generate
33+
```
34+
And you will see.
35+
```
36+
Server : CONNECTOR_SERVER=dc6917876732a081d1d35b225aedab5bae8e5438
37+
Client : CONNECTOR_CLIENT=ba770d272202ad9b938638687760e2ec96a7e954b19447fd5f412c615e2c7ef7
38+
39+
Copy the key to the .env file each projects.
40+
```
41+
In the server section, add `CONNECTOR_SERVER` to .env file. And also in the client section, add `CONNECTOR_CLIENT` to .env file.
42+
43+
## Usage
44+
Make sure you're following these steps.
45+
46+
### Create Get All Product
47+
Create your query to get all products, and change default controller in `config/connector.php` to your created controller.
48+
```
49+
"api" => [
50+
[
51+
"method" => HttpMethodConstant::GET,
52+
"url" => "products",
53+
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ConnectorController@getProducts" // Replace with your controller.
54+
],
55+
...
56+
]
57+
```
58+
Expected Return :
59+
```
60+
[
61+
[
62+
"id" => int,
63+
"name" => string,
64+
"description" => string,
65+
"price" => int
66+
],
67+
...
68+
]
69+
```
70+
71+
### Create Get Product With Ids
72+
Create your query to get products using `WHERE id IN()`, and change default controller in `config/connector.php` to your created controller. At this request, you will receive query string with param `ids` as `string` that contains `product ids` separated by `comma`.
73+
```
74+
"api" => [
75+
[
76+
"method" => HttpMethodConstant::GET,
77+
"url" => "product-with-ids",
78+
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ConnectorController@getProductWithIds" // Replace with your controller.
79+
],
80+
...
81+
]
82+
```
83+
Expected Return :
84+
```
85+
[
86+
[
87+
"id" => int,
88+
"name" => string,
89+
"description" => string,
90+
"price" => int
91+
],
92+
...
93+
]
94+
```
95+
96+
### Create Find Product
97+
Create your query to get product using `WHERE id`, and change default controller in `config/connector.php` to your created controller. At this request, you will receive query string with param `id` as `int`.
98+
```
99+
"api" => [
100+
[
101+
"method" => HttpMethodConstant::GET,
102+
"url" => "find-product",
103+
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ConnectorController@findProduct" // Replace with your controller.
104+
],
105+
...
106+
]
107+
```
108+
Expected Return :
109+
```
110+
[
111+
"id" => int,
112+
"name" => string,
113+
"description" => string,
114+
"price" => int
115+
]
116+
```
117+
118+
## Authors
119+
- [Hafiizh Ghulam](mailto:hafiizh.ghulam@frisidea.com)

src/Config/connector.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,17 @@
1313
[
1414
"method" => HttpMethodConstant::GET,
1515
"url" => "products",
16-
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ProductController@getProducts"
16+
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ConnectorController@getProducts"
17+
],
18+
[
19+
"method" => HttpMethodConstant::GET,
20+
"url" => "product-with-ids",
21+
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ConnectorController@getProductWithIds"
22+
],
23+
[
24+
"method" => HttpMethodConstant::GET,
25+
"url" => "find-product",
26+
"controller" => "Crenata\AffiliateConnector\Http\Controllers\ConnectorController@findProduct"
1727
]
1828
]
1929
];

src/Helpers/RequestHelper.php

+28-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ class RequestHelper implements RequestHelperInterface {
2222
*/
2323
protected $method = HttpMethodConstant::GET;
2424

25+
/**
26+
* Body request
27+
*
28+
* @var array
29+
*/
30+
protected $body = [];
31+
2532
/**
2633
* Timeout request in sec
2734
*
@@ -63,6 +70,16 @@ public function setMethod($method) {
6370
return $this;
6471
}
6572

73+
/**
74+
* @param array $body
75+
* @return $this
76+
*/
77+
public function setBody($body) {
78+
$this->body = $body;
79+
80+
return $this;
81+
}
82+
6683
/**
6784
* @param int $timeout
6885
* @return $this
@@ -99,11 +116,21 @@ public function send() {
99116
->send();
100117

101118
try {
119+
if ($this->method === HttpMethodConstant::POST ||
120+
$this->method === HttpMethodConstant::PUT ||
121+
$this->method === HttpMethodConstant::PATCH
122+
) {
123+
$body = "form_params";
124+
} else {
125+
$body = "query";
126+
}
102127
return Http::withToken($token)
103128
->connectTimeout($this->timeout)
104129
->timeout($this->timeout)
105130
->withOptions($this->options)
106-
->send($this->method, $this->url)
131+
->send($this->method, $this->url, [
132+
$body => $this->body
133+
])
107134
->json();
108135
} catch (\Exception $e) {
109136
return ConnectorResponse::getInstance()

src/Http/Controllers/ProductController.php src/Http/Controllers/ConnectorController.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Illuminate\Http\Request;
77
use Illuminate\Routing\Controller;
88

9-
class ProductController extends Controller {
9+
class ConnectorController extends Controller {
1010
public function getProducts(Request $request) {
1111
return ConnectorResponse::getInstance()->send();
1212
}

0 commit comments

Comments
 (0)