/**
  * Authenticate the given request token is valid or not.
  *
  * @param  string $token
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 public function authenticate($token)
 {
     $token = Token::where('token', '=', $token)->first();
     if (is_null($token) || $token->disable) {
         $msg = is_null($token) ? 'Invalid token' : 'Application is disable';
         return response_unauthorized($msg);
     }
     return response_ok($token);
 }
 /**
  * Handle an incoming request.
  *
  * @param  \Illuminate\Http\Request  $request
  * @param  \Closure  $next
  * @return mixed
  */
 public function handle($request, Closure $next)
 {
     if (!$this->shouldExcept($request)) {
         $appKey = $request->header(static::X_KEY);
         $appSecret = $request->header(static::X_SECRET);
         if (!$this->validAppKey($appKey)) {
             return response_unauthorized();
         }
         if (!$this->validAppSecret($appSecret)) {
             return response_unauthorized();
         }
     }
     return $next($request);
 }
 /**
  * Generate the token for given api key.
  *
  * @param  string $key
  * @return \Symfony\Component\HttpFoundation\JsonResponse
  */
 public function generate($key)
 {
     $app = Application::where('key', '=', $key)->first();
     if (is_null($app) || $app->disable) {
         $msg = is_null($app) ? 'Invalid app key' : 'Application is disable';
         return response_unauthorized($msg);
     }
     $tokenValue = $this->getUUID5Token($app);
     if ($tokenValue) {
         $token = new Token();
         $token->app_id = $app->id;
         // Application ID
         $token->app_key = $app->key;
         // Application Key
         $token->user_id = $app->user_id;
         // Application owner id
         $token->token = $tokenValue;
         // Token for unique user.
         if ($token->save()) {
             return response_ok($token);
         }
     }
     return response_error('Error occured to generate token. Please try again');
 }
 private function getAnalyticData($url, $query = [])
 {
     $client = new Client(['base_uri' => $this->base_url]);
     try {
         $response = $client->get($url, ['headers' => ['X-API-KEY' => $this->api_key, 'X-API-SECRET' => $this->api_secret], 'query' => $query]);
     } catch (\Exception $e) {
         return response_error("Internal Server Error.");
     }
     switch ($response->getStatusCode()) {
         case 500:
             return response_error("Internal Server Error.");
             break;
         case 401:
             response_unauthorized();
             break;
         case 404:
             return response_missing();
             break;
         case 200:
             return response_ok(json_decode($response->getBody()->getContents()));
             break;
     }
     return response_error("Internal Server Error.");
 }