Created
November 13, 2021 16:13
-
-
Save cptiwari20/31ffed7aca6c3d5a7368f79bf835ae33 to your computer and use it in GitHub Desktop.
Fetch and Search the fontawesome pro icons using graphql api in Laravel application.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Tenant\Integrations; | |
use App\Http\Controllers\Controller; | |
use Carbon\Carbon; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Http; | |
use function GuzzleHttp\json_decode; | |
class FontawesomeController extends Controller | |
{ | |
protected $base_url = 'https://api.fontawesome.com/'; | |
protected function fetchToken() | |
{ | |
try { | |
$response = Http::withHeaders([ | |
'Authorization' => 'Bearer '.env('FONTAWESOME_API'), | |
'Content-Type' => 'application/json' | |
])->post($this->base_url.'token'); | |
if($response->successful()){ | |
$resposeBody = json_decode($response->body()); | |
$resposeBody->create_at = time(); // set the time to current time | |
// set the data in the server | |
session()->put('fontawesomeToken', $resposeBody); | |
//retrun the actual data as in session | |
return $resposeBody; | |
}else{ | |
session()->put('fontawesomeToken', false); | |
return false; | |
} | |
} catch (\Exception $e) { | |
return false; | |
} | |
} | |
protected function getAccessToken() | |
{ | |
$fetchedToken = $this->fetchToken(); | |
if($fetchedToken){ // check if token exists | |
return $fetchedToken->access_token; | |
}else{ | |
return false; | |
} | |
} | |
public function fetchIcon(Request $request) | |
{ | |
if(!$request->query('name')){ | |
return response('No icon name found!', 404); | |
} | |
// Check session | |
$sessionData = session('fontawesomeToken'); | |
$accessToken = ''; | |
if($sessionData){ // if session token exists | |
if(isset($sessionData->create_at)){ // check if created at exists | |
// return dd(Carbon::now()->diffInSeconds(Carbon::parse($sessionData->create_at)), $sessionData->expires_in); | |
if(Carbon::now()->diffInSeconds(Carbon::parse($sessionData->create_at)) < $sessionData->expires_in){ // check if token expiration still valid | |
$accessToken = session('fontawesomeToken')->access_token; | |
}else{ //when token expired | |
$accessToken = $this->getAccessToken(); | |
} | |
}else{ // if create at not exist | |
session()->put('fontawesomeToken', false); | |
$accessToken = $this->getAccessToken(); | |
} | |
}else{ // when session is not there | |
$accessToken = $this->getAccessToken(); | |
} | |
if(!$accessToken){ // still when no token found | |
return response('Unable to find the access token', 403)->json(['status' => 'false', 'unable to fetch the icons from server.']); | |
} | |
// graphql query | |
$query = "query { | |
search(version: ".json_encode(env('FONTAWESOME_VERSION')).", query: ".json_encode($request->query('name')).", first: 10) { | |
id | |
label | |
unicode | |
styles | |
} | |
}"; | |
// convert query into array | |
$data = array ('query' => $query); | |
// hit post request with token | |
$response = Http::withHeaders([ | |
'Authorization' => sprintf("Authorization: Bearer %s",$accessToken), | |
'Content-Type' => 'application/json' | |
])->post($this->base_url, $data); | |
// get the response | |
if($response->successful()){ | |
return response()->json(['status'=> true, 'message' => 'Data found!', 'data' => json_decode($response->body())]); | |
}else{ | |
return response()->json(['status'=>false, 'message' => 'Unable to fetch data!'.' Unexpected HTTP status: ' . | |
$response->getReasonPhrase() ]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment