Hacker News new | ask | show | jobs
by CptKriechstrom 1828 days ago
Seems like a Solution in search for a Problem. Instead of hardcoding the URL to the Request it can be set in the config like it was done with jsonplaceholder.api.token.

And creating a Request for every API-Endpoint instead of a single API-Client Class seems overkill.

1 comments

I think it makes sense if you're doing the same 5 requests over and over throughout your application. As it stands, I generally build a service class for these scenarios:

  class GoogleMapsApiService {
      public function geocodeAddress(string $address);
      public function getDirections(string $address);
  }

That sort of thing. It's all reusable and I can just call `app(GoogleMapsApiService::class)->getDirections('123 Main St')` to request the data. This just goes a step further and makes it more flexible (and probably more testable) by turning it into a class. Creating the class is a simple command line run (if you're familiar with Laravel, much of your application is scaffolded this way).

I think it loses its effectiveness/value if you're using it for 20 different requests that are only used once each, but for a scenario where you're making 1 request in 10 different places like you might be with geocoding, it's not a bad implementation in my opinion.