Commit 05b7b8a1 authored by Dzulfqar Ridha's avatar Dzulfqar Ridha

updated

parent 49789b61
...@@ -7,13 +7,14 @@ ...@@ -7,13 +7,14 @@
*/ */
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Classes\MResponse; use App\Classes\MResponse;
use GuzzleHttp\Client; use GuzzleHttp\Client;
class MasheryController extends Controller class MasheryController extends Controller
{ {
public function getToken() public function getToken()
{ {
$mResponse = new MResponse(); $mResponse = new MResponse();
...@@ -21,16 +22,15 @@ class MasheryController extends Controller ...@@ -21,16 +22,15 @@ class MasheryController extends Controller
try { try {
$client = new Client([ $client = new Client([
'base_uri' => 'https://api.mashery.com/', 'base_uri' => 'https://api.mashery.com/v3/',
'auth' => ['y4a5rz5ccv374qu33558f65b', 'yuZRnvKvYY'], 'auth' => ['y4a5rz5ccv374qu33558f65b', 'yuZRnvKvYY'],
'headers' => [ 'headers' => [
'Authorization' => 'Basic eTRhNXJ6NWNjdjM3NHF1MzM1NThmNjViOnl1WlJudkt2WVk=', 'Authorization' => 'Basic eTRhNXJ6NWNjdjM3NHF1MzM1NThmNjViOnl1WlJudkt2WVk=',
'Content-Type' => 'application/x-www-form-urlencoded'
], ],
'http_errors' => true 'http_errors' => true
]); ]);
$response = $client->request('POST', 'https://api.mashery.com/v3/token', [ $response = $client->request('POST', 'token', [
'form_params' => [ 'form_params' => [
'grant_type' => 'password', 'grant_type' => 'password',
'username' => 'marketplace.dev', 'username' => 'marketplace.dev',
...@@ -49,6 +49,97 @@ class MasheryController extends Controller ...@@ -49,6 +49,97 @@ class MasheryController extends Controller
$mResponse->message = 'success'; $mResponse->message = 'success';
$mResponse->data = $resBody; $mResponse->data = $resBody;
} }
} catch (\Exception $e) {
$statusCode = 500;
$mResponse->message = $e->getMessage();
}
$this->token = $resBody['access_token'];
return response()->json($mResponse, $statusCode);
}
public function getUser(Request $request)
{
$token = $request->get('token');
$mashUsername = $request->get('username');
$mResponse = new MResponse();
$statusCode = 200;
try {
$client = new Client([
'base_uri' => 'https://api.mashery.com/v3/',
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
'http_errors' => true
]);
$response = $client->request('GET', 'rest/members?filter=username:' . $mashUsername);
$resBody = json_decode($response->getBody()->getContents(), true);
if ($response->getStatusCode() != 200) {
$statusCode = $response->getStatusCode();
$mResponse->message = $resBody;
} else {
$mResponse->success = true;
$mResponse->message = 'success';
$mResponse->data = $resBody;
}
} catch (\Exception $e) {
$statusCode = 500;
$mResponse->message = $e->getMessage();
}
return response()->json($mResponse, $statusCode);
}
public function createUser(Request $request)
{
$token = $request->get('token');
$username = $request->get('username');
$email = $request->get('email');
$dname = $request->get('dname');
$pass = $request->get('pass');
$mResponse = new MResponse();
$statusCode = 200;
try {
$client = new Client([
'base_uri' => 'https://api.mashery.com/v3/',
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
'http_errors' => false
]);
$response = $client->request('POST', 'rest/members', [
'json' => [
'username' => $username,
'email' => $email,
'displayName' => $dname,
'areaStatus' => 'active',
'externalId' => '',
'passwdNew' => $pass
]
]);
$resBody = json_decode($response->getBody()->getContents(), true);
if ($response->getStatusCode() != 200) {
$statusCode = $response->getStatusCode();
$mResponse->message = $resBody;
} else {
$mResponse->success = true;
$mResponse->message = 'success';
$mResponse->data = $resBody;
}
} catch (\Exception $e) { } catch (\Exception $e) {
$statusCode = 500; $statusCode = 500;
$mResponse->message = $e->getMessage(); $mResponse->message = $e->getMessage();
......
...@@ -14,8 +14,7 @@ Class MasheryGetUserController extends controller ...@@ -14,8 +14,7 @@ Class MasheryGetUserController extends controller
public function __construct() public function __construct()
{ {
$apiClient = new MasheryAPIClient('y4a5rz5ccv374qu33558f65b','yuZRnvKvYY');
$this->token = $apiClient->prepareAccessToken();
} }
public function getUser() public function getUser()
......
...@@ -15,10 +15,15 @@ $router->get('/', function () use ($router) { ...@@ -15,10 +15,15 @@ $router->get('/', function () use ($router) {
return $router->app->version(); return $router->app->version();
}); });
$router->get('/token', 'MasheryGetTokenController@getToken');
$router->get('/getUser', 'MasheryGetUserController@getUser'); $router->get('/getUser', 'MasheryGetUserController@getUser');
$router->group(['prefix' => 'mashery'], function () use ($router) { $router->group(['prefix' => 'mashery'], function () use ($router) {
//mashery/token
$router->get('token', 'MasheryController@getToken'); $router->get('token', 'MasheryController@getToken');
//mashery/getUser?token={token}&username={mashery_username}
$router->get('getUser', 'MasheryController@getUser');
//mashery/createUser?token={}&username={}&email={}&dname={}&pass={}
$router->get('createUser', 'MasheryController@createUser');
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment