@mateog98 do you cache your configs?
Jun 23, 2023
5
Level 1
Why am i anot being able to generate a token with my Service?
I created a TokenGenerator class
<?php
class TokenGenerator
{
private $authServiceUrl = '';
private $username = '';
private $password = '';
# constructor
function __construct($username, $pasword, $authServiceUrl) {
$this->username = $username;
$this->password = $pasword;
$this->authServiceUrl = $authServiceUrl;
}
public function loadToken()
{
$computedHash = base64_encode(hash_hmac ( 'md5' , $this->authServiceUrl , $this->password, true ));
$authorization = 'Authorization: Bearer '.$this->username.':'.$computedHash;
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, '');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
curl_setopt($curl, CURLOPT_URL, $this->authServiceUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
$obj = json_decode($result);
$info = curl_getinfo($curl);
curl_close($curl);
if ($info['http_code'] != '200') {
throw new Exception('Failed to load token');
}
return $obj;
}
}
?>
And this is my ApiMedicService.php
<?php
namespace App\Services;
use GuzzleHttp\Client;
use App\Services\TokenGenerator;
use App\Services\DiagnosisClient;
class ApiMedicService
{
protected $diagnosisClient;
public function __construct($language)
{
$tokenGenerator = new TokenGenerator(
env('APIMEDIC_USERNAME'),
env('APIMEDIC_PASSWORD'),
env('AUTH_SERVICE_URL')
);
$token = $tokenGenerator->loadToken();
if ($token) {
$this->diagnosisClient = new DiagnosisClient($language, $token->Token, $healthServiceUrl);
} else {
throw new Exception('Failed to load API token');
}
}
public function loadSymptoms()
{
return $this->diagnosisClient->loadSymptoms();
}
public function loadDiagnosis($selectedSymptoms, $gender, $yearOfBirth)
{
return $this->diagnosisClient->loadDiagnosis($selectedSymptoms, $gender, $yearOfBirth);
}
And this is a fragment from my DiagnosisClient.php
<?php
namespace App\Services;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
class DiagnosisClient {
private $healthServiceUrl;
private $language;
private $token;
private $validThrough;
# constructor
function __construct($language, $token, $healthServiceUrl) {
$this->token = $token->{'Token'};
$this->validThrough = $this->getTokenValidThrough();
$this->healthServiceUrl = $healthServiceUrl;
$this->language = $language;
}
# <summary>
# Generic api get call to load data from webservice
# </summary>
# <param name="$action">common url parameters for each call</param>
# <returns>Returns deserialized result from webservice</returns>
private function _loadFromWebserice($action)
{
$extraArgs = 'token='.$this->token.'&format=json&language='.$this->language;
$extraChar = strpos($action, '?') ? '&' : '?';
$url = $this->healthServiceUrl.'/'.$action.$extraChar.$extraArgs;
var_dump($url);
$response = Http::get($url);
$result = $response->body();
$obj = json_decode($result, true);
if ($response->status() != 200) {
// Handle error from the server
// You can throw an exception or return an appropriate response
return null;
}
return $obj;
}
For some reasons my token is not being generated or being pass correctly becuase i get the following error message in my DiagnosisClient.php
"message": "Unresolvable dependency resolving [Parameter #1 [ <required> $token ]] in class App\\Services\\DiagnosisClient", "exception": "Illuminate\\Contracts\\Container\\BindingResolutionException",
Please or to participate in this conversation.