More Effeciant Routes

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.

1 Like

I’d move these request to a function or create a class whose methods you can then query in your routes. And/or you use a single route with different options

'pattern' => 'snipcart/(:any)',
'action'  => function ($param) {

So $param would be whatever :any is?

Yep.

Ok this mostly works…

[
    'pattern' => 'snipcart/(:any)',
    'action'  => function ($param) {

      $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/'. $param, [
        'headers' => [
            'Accept:' . 'application/json',
            'Authorization: Basic ' . base64_encode($apisecretkey . ':')
        ]
      ]);

      if ($request->code() === 200) {
          $snipcart = $request->json();
      }

      return $snipcart;
    }
]

But one of the end points is https://app.snipcart.com/api/carts/abandoned which needs a second (:any). How can i get it tell the difference between snipcart/(:any) and snipcart/(:any)/(:any) so i can build up the end of the URL for the api in a single route? is that possible?

You can use (:all) instead of (:any) for a catch-all route, or an array of alternatives.