Skip to content

Commit

Permalink
Add function guzzle_post_multipart() - Guzzle POST file with Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
vickzkater committed Sep 16, 2020
1 parent 68d2068 commit 7d55213
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 37 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Developed by [@vickzkater](https://github.com/vickzkater/) (Powered by [KINIDI T
- [x] Support Session Driver Database (please check section `Session Driver Database`)
- [x] Security update: if password has been changed, then force user to re-login
- [x] Feature logout from all sessions
- [x] Sample function sending email & email template (HTML & Plain Text)

## Admin Panel

Expand All @@ -77,6 +78,8 @@ Developed by [@vickzkater](https://github.com/vickzkater/) (Powered by [KINIDI T

## Version

***Current Version: 2.0.3 (Laravel 7.28.2)**

Laravel | Lara-S-CMS
:---------|:----------
5.8.x | 1.0 ; 1.1.0
Expand Down
118 changes: 81 additions & 37 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,55 @@ public function __construct()
});
}

/**
* Guzzle GET Public Access (without token Authorization Bearer)
*
* @param String $url (Target API URL) required
* @param Array $auth (htaccess auth) optional
*
* @return Object (API Response)
*/
protected function guzzle_get_public($url, $auth = null)
{
$config = ['http_errors' => false];
if (env('APP_DEBUG')) {
$config['verify'] = false;
}
if ($auth) {
$config['auth'] = $auth;
}
$client = new Client($config);
$request = $client->request('GET', $url);
$response = $request->getBody();

return json_decode($response);
}

/**
* Guzzle POST Public Access (without token Authorization Bearer)
*
* @param String $url (Target API URL) required
* @param Array $parameter (Paramaters) required
* @param Array $auth (htaccess auth) optional
*
* @return Object (API Response)
*/
protected function guzzle_post_public($url, $parameter, $auth = null)
{
$config = ['http_errors' => false];
if (env('APP_DEBUG')) {
$config['verify'] = false;
}
if ($auth) {
$config['auth'] = $auth;
}
$client = new Client($config);
$request = $client->request('POST', $url, ['form_params' => $parameter]);
$response = $request->getBody()->getContents();

return json_decode($response);
}

/**
* Guzzle GET with token Authorization Bearer
*
Expand Down Expand Up @@ -204,51 +253,46 @@ protected function guzzle_post($url, $token, $parameter, $auth = null)
}

/**
* Guzzle GET Public Access (without token Authorization Bearer)
*
* @param String $url (Target API URL) required
* @param Array $auth (htaccess auth) optional
*
* @return Object (API Response)
*/
protected function guzzle_get_public($url, $auth = null)
{
$config = ['http_errors' => false];
if (env('APP_DEBUG')) {
$config['verify'] = false;
}
if ($auth) {
$config['auth'] = $auth;
}
$client = new Client($config);
$request = $client->request('GET', $url);
$response = $request->getBody();

return json_decode($response);
}

/**
* Guzzle POST Public Access (without token Authorization Bearer)
* Guzzle POST file with token Authorization Bearer
*
* @param String $url (Target API URL) required
* @param String $token (Token Authorization Bearer) required
* @param Array $parameter (Paramaters) required
* @param Array $auth (htaccess auth) optional
*
* @return Object (API Response)
*/
protected function guzzle_post_public($url, $parameter, $auth = null)
// *Sample:
// // Set params
// $params = [
// [
// 'name' => 'user_id',
// 'contents' => $user_id
// ],
// [
// 'name' => 'avatar',
// 'contents' => fopen($request->file('avatar')->getRealPath(), "r"),
// 'filename' => $request->file('avatar')->hashName()
// ]
// ];
protected function guzzle_post_multipart($url, $token, $parameter, $auth = null)
{
$config = ['http_errors' => false];
if (env('APP_DEBUG')) {
$config['verify'] = false;
}
if ($auth) {
$config['auth'] = $auth;
}
$client = new Client($config);
$request = $client->request('POST', $url, ['form_params' => $parameter]);
$response = $request->getBody()->getContents();
if (empty($token)) {
return 'Unauthorized';
} else {
$config = ['http_errors' => false];
if (env('APP_DEBUG')) {
$config['verify'] = false;
}
if ($auth) {
$config['auth'] = $auth;
}
$client = new Client($config);
$headers = array('Authorization' => 'bearer ' . $token);
$request = $client->request('POST', $url, ['headers' => $headers, 'multipart' => $parameter]);
$response = $request->getBody()->getContents();

return json_decode($response);
return json_decode($response);
}
}
}

0 comments on commit 7d55213

Please sign in to comment.