Commit fa9f8b14 authored by Dzulfqar Ridha's avatar Dzulfqar Ridha
parents 05b7b8a1 70bc875a
<?php
/**
* Created by PhpStorm.
* User: gets
* Date: 4/4/2019
* Time: 4:40 PM
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
class BaseController extends Controller
{
protected $request;
public function __construct(Request $request)
{
$this->request = $request;
}
protected function masheryClient()
{
$client = new Client([
'base_uri' => 'https://api.mashery.com/',
'headers' => [
'Authorization' => 'Bearer ' . $this->request->auth['token'],
'Content-Type' => 'application/json'
],
'http_errors' => false
]);
return $client;
}
}
<?php
/**
* Created by PhpStorm.
* User: gets
* Date: 4/4/2019
* Time: 3:24 PM
*/
namespace App\Http\Controllers;
use App\Classes\MResponse;
use App\Http\Request\UserByUsernameRequest;
class UserController extends BaseController
{
public function getByUsername(UserByUsernameRequest $request)
{
$mResponse = new MResponse();
$statusCode = 200;
try {
$response = $this->masheryClient()->request('GET', 'https://api.mashery.com/v3/rest/members', [
'query' => [
'filter' => 'username:' . $request->get('username')
]
]);
$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);
}
}
<?php
/**
* Created by PhpStorm.
* User: gets
* Date: 4/4/2019
* Time: 3:14 PM
*/
namespace App\Http\Middleware;
use App\Classes\MResponse;
use Closure;
class ClientMiddleware
{
public function handle($request, Closure $next, $guard = null)
{
$token = $request->header('Authorization');
$response = new MResponse();
if (empty($token)) {
$response->message = 'Token not provided';
return response()->json($response, 401);
}
$authUser = [
'token' => $token
];
$request->auth = $authUser;
return $next($request);
}
}
<?php
/**
* Created by PhpStorm.
* User: gets
* Date: 4/4/2019
* Time: 4:00 PM
*/
namespace App\Http\Request;
use App\Classes\MResponse;
use Illuminate\Contracts\Validation\Validator;
use Pearl\RequestValidate\RequestAbstract;
class FormRequestErrors extends RequestAbstract
{
protected function validationData()
{
return $this->json()->all();
}
/**
* Format the errors from the given Validator instance.
*
* @param \Illuminate\Contracts\Validation\Validator $validator
* @return \Illuminate\Http\JsonResponse
*/
protected function formatErrors(Validator $validator)
{
$mResponse = new MResponse();
$mResponse->message = $validator->errors();
return response()->json($mResponse, 422);
}
protected function getValidatorInstance()
{
$this->getInputSource()->replace($this->modifyData());
$validator = parent::getValidatorInstance();
return $validator;
}
}
<?php
/**
* Created by PhpStorm.
* User: gets
* Date: 4/4/2019
* Time: 3:45 PM
*/
namespace App\Http\Request;
class UserByUsernameRequest extends FormRequestErrors
{
protected function validationData()
{
return $this->all();
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'username' => 'required'
];
}
/**
* Get custom messages for validator errors.
*
* @return array
*/
public function messages()
{
return [];
}
protected function modifyData()
{
$data = $this->validationData();
return $data;
}
}
<?php
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__ . '/../vendor/autoload.php';
(new Laravel\Lumen\Bootstrap\LoadEnvironmentVariables(
dirname(__DIR__)
......@@ -21,9 +21,9 @@ $app = new Laravel\Lumen\Application(
dirname(__DIR__)
);
// $app->withFacades();
//$app->withFacades();
// $app->withEloquent();
//$app->withEloquent();
/*
|--------------------------------------------------------------------------
......@@ -63,6 +63,7 @@ $app->singleton(
$app->routeMiddleware([
'auth' => App\Http\Middleware\Authenticate::class,
'client.auth' => App\Http\Middleware\ClientMiddleware::class
]);
/*
......@@ -79,6 +80,7 @@ $app->routeMiddleware([
// $app->register(App\Providers\AppServiceProvider::class);
// $app->register(App\Providers\AuthServiceProvider::class);
// $app->register(App\Providers\EventServiceProvider::class);
$app->register(Pearl\RequestValidate\RequestServiceProvider::class);
/*
|--------------------------------------------------------------------------
......@@ -94,7 +96,7 @@ $app->routeMiddleware([
$app->router->group([
'namespace' => 'App\Http\Controllers',
], function ($router) {
require __DIR__.'/../routes/web.php';
require __DIR__ . '/../routes/web.php';
});
return $app;
......@@ -8,6 +8,7 @@
"php": ">=7.1.3",
"guzzlehttp/guzzle": "~6.0",
"laravel/lumen-framework": "5.8.*",
"pearl/lumen-request-validate": "^1.2",
"vlucas/phpdotenv": "^3.3"
},
"require-dev": {
......
This diff is collapsed.
......@@ -27,3 +27,9 @@ $router->group(['prefix' => 'mashery'], function () use ($router) {
//mashery/createUser?token={}&username={}&email={}&dname={}&pass={}
$router->get('createUser', 'MasheryController@createUser');
});
$router->group(['middleware' => 'client.auth'], function () use ($router) {
$router->group(['prefix' => 'user'], function () use ($router) {
$router->get('by-username', 'UserController@getByUsername');
});
});
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