Jun 22, 2023
3
Level 1
How to send a request to consume an api?
I am trying to consume the apimedic api.
The following is my ApiMedicService.php
<?php
namespace App\Services;
use GuzzleHttp\Client;
class ApiMedicService
{
protected $client;
protected $apiKey;
public function __construct($apiKey)
{
$this->client = new Client([
'base_uri' => 'https://sandbox-healthservice.priaid.ch/',
'timeout' => 10,
]);
$this->apiKey = $apiKey;
}
public function getSymptoms()
{
$response = $this->client->get('symptoms', [
'query' => [
'token' => $this->apiKey,
'language' => 'en-gb'
],
]);
return json_decode($response->getBody(), true);
}
public function getDiagnosis($symptoms, $gender, $birth_date, $token)
{
$query = http_build_query([
'symptoms' => $symptoms,
'token' => $this->apiKey,
'gender' => $gender,
'year_of_birth' => $birth_date,
]);
$url = 'diagnosis?' . $query;
$response = $this->client->get($url);
return json_decode($response->getBody(), true);
}
}
And this is my controller method to getDiagnosis in SymptomsController.php
public function getDiagnosis(Request $request)
{
$this->validate($request, [
'symptoms' => 'required|array',
'symptoms.*' => 'numeric',
]);
$symptoms = $request->input('symptoms');
$user = Auth::user();
$gender = $user->gender;
$birth_date = $user->birth_date;
$token = config('APIMEDIC_API_KEY');
$diagnosis = $this->apiMedicService->getDiagnosis($symptoms, $gender, $birth_date, $token);
return response()->json($diagnosis);
}
With the following route:
Route::get('/diagnosis', [SymptomsController::class, 'getDiagnosis'])->middleware('auth');
I am trying to send a request to:
https://sandbox-healthservice.priaid.ch/diagnosis?symptoms=[10]&gender=female& year_of_birth=2000&token=<APIMEDIC_API_KEY>
So i want to get the gender and year_of_birth from the propertys gender and birth_date of the logged in user. The APIMEDIC_API_KEY i get it from api key in the env file
But for some reasons when i hit a GET request to http://localhost:8000/api/diagnosis i get symptoms are missing Why is this happening?
Please or to participate in this conversation.