Ugly API

Introduction

In this article, I want to talk about the problems I encountered in the process of integrating with the API over the HTTP protocol, and share my experience in solving them.





When developing frontend applications (Mobile / Web), you often come across the fact that the API on the backend has not yet been implemented. You have to wait for the developer on the backend when he provides the necessary requests, constantly reminding him of himself. Another situation is not uncommon when the necessary http requests already exist, but they are implemented very poorly and crookedly.





Perhaps I would not have written this article. But it struck me as amazing that all the examples of bad API implementation below came across to me in one single project, at the same time!





In this project I am developing a mobile application in Flutter using the Retrofit package , which helps me reduce the time and code I have to write myself, generating significant code automatically. I also use Insomnia for the initial verification of requests before implementing them in the code.





The video version is available here https://www.youtube.com/watch?v=oj-i1IBejcI&t=8s





Before I get into problems, I would like to point out that before I became a mobile developer, I worked as a Full-Stack developer for over 5 years. And I understand how important it is to implement a beautiful API for the frontend, with which it is easy and pleasant to integrate and safe in case of future changes to the API.





So, let's begin.





Non-RESTful architecture

The first disappointment was the fact that the API architecture is not implemented in the RESTful style. Honestly, I don't remember when I had to deal with the integration of such non-RESTful APIs.





REST REpresentational State Transfer. RESTful β€” API, HTTP. REST . , (, tasks, customers, etc.). , HTTP, CRUD , .. GET, POST, PUT, DELETE.





 





RESTful API:





API β€” REST β€” :





, , POST . type, . , , , - endpoint’ - if-else switch type .





, , RESTful, , - Retrofit, API





Header Accept: application/json

Insomnia, , . 





Insomnia, , Preview json , json . , Retrofit Dart , .





- , . : β€œ ”.





  Insomnia . Preview json, . Header , Content-Type , text/html, charset-utf-8, Preview json. 





, , application/json text/html, - Retrofit Dart .





Accept Header , , β€œ - json”. , .. Header Accept.





, :





  1. Retrofit, β€œβ€





  1. , , . , Retrofit . , , , :





, , .





JSON keys case types

json:





, . :





Dart camelCase . json camelCase , Retrofit , , json. JsonKey, snake_case, β€”  UPPER_CASE_SNAKE_CASE:





β€” , case type, , .. json b Dart . API, API , .





, . β€œ ”. , , , . , , , json . Insomnia, . , , β€” .  Insomnia , , β€” :





, json, ? json? ? ?





I know the server is written in PHP. Gentlemen who program in PHP, could you write in the comments how this is possible?





Conclusion

Now I am experiencing the opposite feelings. On the one hand, I am upset that I have to integrate such an ugly API, on the other, I am already looking forward to the next case, which will show how else you can disfigure the API.





I would like to know about your experience in integrating with the API, and what problems you encountered. 





Thank you. Happy coding!








All Articles