Swagger API iOS helper
Swagger is a great tool that helps developers community save hours while working on web services. API specifications are of great help for both developers and QA team.
But there is more. Client side developers can easily skip routine part of development process and not implement client-server communications but use swagger-codegen.
It is available for a lot of platforms, but as far as I am mostly an iOS developer I will share a small hack that I use in my daily routine.
Let’s say we have a messenger app and we need to implement a screen with a list of conversations. I will skip the part where I salute to VIPER pattern and go strictly to the place where we agreed that we need some kind of ChatsService.
Let’s say we define some protocol
Now we want to implement remote chats provider. It should call API and map API response models into app-layer entities.
You might have noticed a class called SwaggerApiResponseHandler. It’s the simple class with helper methods that I want to share with you. It’ll save you some time to avoid code duplicates. (Even though that part with DispatchGroup stuff must also be reworked to prettier implementation. But so far so good.)
What we are doing here is we simply unwrap swagger api responses in a way we feel suits us best. Swagger generated API returns a tuple of optional successful response object and optional error. It’s not so convenient to handle it every time, so I’ve come up with the handler above.
You can either stick to closure style or to more convenient (imho) way where async methods look like sync ones.
So to use this you’ll need to do something like this:
The drawback of this approach is that you’ll need to handle dispatch queues yourself. But it’s also an advantage. You’ll be able to chain methods and avoid closures hell.
Thanks for your time!
