Im working with an API that has quite a few endpoints. The code below works but is really repititious. Is there a way to make it smarter without so much code? LIke a function or something that i just pass the part of it im trying to reach?
'routes' => [
[
'pattern' => 'snipcart/orders',
'action' => function () {
$apisecretkey = option('hashandsalt.kirby-snipcart.snipcartlive') === true ? option('hashandsalt.kirby-snipcart.apisecretlive') : option('hashandsalt.kirby-snipcart.apisecrettest');
$snipcart = [];
$request = Remote::get('https://app.snipcart.com/api/orders', [
'headers' => [
'Accept:' . 'application/json',
'Authorization: Basic ' . base64_encode($apisecretkey . ':')
]
]);
if ($request->code() === 200) {
$snipcart = $request->json();
}
return $snipcart;
}
],
[
'pattern' => 'snipcart/abandoned',
'action' => function () {
$apisecretkey = option('hashandsalt.kirby-snipcart.snipcartlive') === true ? option('hashandsalt.kirby-snipcart.apisecretlive') : option('hashandsalt.kirby-snipcart.apisecrettest');
$snipcart = [];
$request = Remote::get('https://app.snipcart.com/api/carts/abandoned', [
'headers' => [
'Accept:' . 'application/json',
'Authorization: Basic ' . base64_encode($apisecretkey . ':')
]
]);
if ($request->code() === 200) {
$snipcart = $request->json();
}
return $snipcart;
}
]
]
]
There could be up to 12 of these, so dont really want to carry on down this path.