When I advise clients on how to work with APIs, I recommend never calling API's directly. Instead, I suggest creating functions whose sole purpose is to get and set API data. It allows you to change the API endpoint and even how your API returns data without having to rewrite significant portions of your code base. Let's dig in! An exampleLet's imagine you have a todo app. Users can view a list of their todo items, add new items, and mark items as complete. Each of these actions makes a call to an API that gets, sets, or modifies data. Let's say the API endpoint is A
Direct-use endpointsI sometimes see API endpoints called directly in the code that uses their data.
This works just fine… until it doesn't. APIs changeThere's a good chance your app calls the API in various places for various tasks. When the API changes, that means a lot of rewrites. Maybe the endpoint changes from Maybe the way authentication is handled changes. Now you need to update your Sometimes, the data that's returned changes.
Now, anywhere your API is called, you need to not just update the API call, but refactor how you work with it. But there's an easier way. Getter and setter methodsI recommend my clients use dedicated methods for calling APIs and processing the data that's returned.
If the API changes, you now have a single file where you can make your changes. And for more severe changes, like a big change in how the data is returned, you can transform it before returning it and avoid ever having to update the functions that use it at all.
With this approach, The specifics of the API become an implementation detail you abstract away. If you or your team need help structuring code for long term maintenance and scalability, get in touch. I'd love to help! Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] Data portability: API getter and setter methods"