Answer the question
In order to leave comments, you need to log in
Is this the right way to organize the code?
Hello. I needed to develop an API to get data from another API.
The task itself is elementary, but I am tormented by the question of how to do it more correctly.
Here's how I did it:
1) Created a Services directory in app and added it to autoload in composer. Created a Partner class that can send and receive songs.
//App\Services
class Partner
{
/**
* Returns an object of song of certain singer.
*
* @param string $singer
* @param string $song_name
* @return array
*/
public function getSong(string $singer, string $song_name) : array
{
try {
$json = file_get_contents('partner-domain.com/api?singer=' . $singer);
} catch (\Exception $e) {
//throw new Exception or return object with status?
}
$songs = json_decode($json, true);
//Filter songs by given name
$result = array_filter($songs, function($song) use ($song_name) {
return mb_strtolower($song['name'] === mb_strtolower($song_name));
});
//We need to return only 1 song
return $result[0] ?? [];
}
/**
* Send a song to API.
*
* @param string $singer
* @param object $song
* @return void
*/
public function sendSong(string $singer, object $song) : void
{
try {
$curl = curl_init('partner-domain.com/api/send');
/*curl body, postfields etc...*/
curl_exec($curl);
curl_close($curl);
} catch (\Exception $e) {
//throw new Exception or return object with status?
}
}
}
//App\Http\Controllers
class ApiController()
{
private $partnerService;
public function __constructor()
{
$this->partnerService = new App\Services\Partner();
}
/**
* Returns an object of song of certain singer.
*
* @param string $singer
* @param string $song_name
* @return string
*/
public function getSong(string $singer, string $song_name) : string
{
$song = $this->partnerService->getSong($singer, $song_name);
return response()->json($song);
}
/**
* Send a song to API.
*
* @param string $singer
* @param object $song
* @return string
*/
public function sendSong(string $singer, object $song) : void
{
$this->partnerService->sendSong($singer, $song);
}
}
//App\Services\Partner
public function getSong(string $singer, string $song_name) : array
{
try {
$json = file_get_contents('partner-domain.com/api?singer=' . $singer);
} catch (\Exception $e) {
return [
'status' => 'error',
'message' => $e->getMessage();
];
}
$songs = json_decode($json, true);
//Filter songs by given name
$result = array_filter($songs, function($song) use ($song_name) {
return mb_strtolower($song['name'] === mb_strtolower($song_name));
});
//We need to return only 1 song
if (isset($result[0])) {
return [
'status' => 'ok',
'data' => $result[0];
];
} else {
return [
'status' => 'error',
'message' => 'not found';
];
}
}
Answer the question
In order to leave comments, you need to log in
throw new Exeption('Ошибка!');
1) at least one PartnerException must come from the Partner class,
all exceptions in the Partner class will be "digested into one" PartnerException
in your case, you can add another heir and PartnerNotFoundException
logic becomes more concise:
//App\Services\Partner
public function getSong(string $singer, string $song_name) : array
{
try {
$json = file_get_contents('partner-domain.com/api?singer=' . $singer);
} catch (\Exception $e) {
throw new PartnerException($e->getMessage());
}
$songs = json_decode($json, true);
//Filter songs by given name
$result = array_filter($songs, function($song) use ($song_name) {
return mb_strtolower($song['name'] === mb_strtolower($song_name));
});
//We need to return only 1 song
if (!isset($result[0])) {
throw new PartnerNotFoundException(sprintf(
'parnter not found by %s and %s', $singer, $song_name
));
}
return [
'status' => 'ok',
'data' => $result[0];
];
}
public function getSong(string $singer, string $song_name) : string
{
try{
$song = $this->partnerService->getSong($singer, $song_name);
}
catch(PartnerNotFoundException $ex){
//п.с не помню как в лаевел но смысл поняли
return response()->status(404)->send();
}
return response()->json($song);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question