Commit ff68cd98 by Arpit Jain

Implemented condition if key file does not exist then create file and update key

parent b8ea506e
...@@ -6,9 +6,42 @@ use Illuminate\Support\Facades\Storage; ...@@ -6,9 +6,42 @@ use Illuminate\Support\Facades\Storage;
Trait Apitraits Trait Apitraits
{ {
private function createFileAndPutApiKey(){
Storage::disk('public')->put('key/apikey.dba', '');
$api_key_url = config('app.api_key_url');
$client = new Client();
$response = $client->request('POST', $api_key_url,[
'form_params' => [
'client_id' => 1,
'client_secret' => 'DJXCYjyCRDt5IHMbiKYbiKqjVWdeNKo1VixHYQaL'
]
]);
$body = $response->getBody()->getContents();
$keyDetailAPIdata = json_decode($body, true);
$updateKey = Storage::disk('public')->put('key/apikey.dba', $keyDetailAPIdata['access_token']);
if($updateKey == true){
$api_key = Storage::get('key/apikey.dba');
}else{
return false;
}
return $api_key;
}
public function getAPiKey(){ public function getAPiKey(){
$file = Storage::disk('public')->exists('key/apikey.dba');
if($file == false){
$api_key = $this->createFileAndPutApiKey();
}else{
$modifiedtime = Storage::lastModified('key/apikey.dba'); $modifiedtime = Storage::lastModified('key/apikey.dba');
$time = date('d-m-Y', $modifiedtime); $time = date('d-m-Y', $modifiedtime);
$expieryDate = date('d-m-Y', strtotime($time. ' + 15 days')); $expieryDate = date('d-m-Y', strtotime($time. ' + 15 days'));
...@@ -40,6 +73,7 @@ Trait Apitraits ...@@ -40,6 +73,7 @@ Trait Apitraits
return false; return false;
} }
}else{ }else{
$file = Storage::disk('public')->exists('key/apikey.dba'); $file = Storage::disk('public')->exists('key/apikey.dba');
if($file == true){ if($file == true){
$api_key = Storage::get('key/apikey.dba'); $api_key = Storage::get('key/apikey.dba');
...@@ -47,6 +81,8 @@ Trait Apitraits ...@@ -47,6 +81,8 @@ Trait Apitraits
return false; return false;
} }
} }
}
return $api_key; return $api_key;
} }
......
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