One thing I want to experiment in ClojureDart is to use generative code from Dart.
One example of generative code could be retrofit a package in Dart to configure a client http easily. We will make an example how to use this in ClojureDart during this article.
If you want a quick example of ClojureDart take a look of my previous article.
Adding Retrofit package
Take a look of Retrofit documentation where we can find how to install it, for quick install follow:
We will use previous example code to modify it and implement the retrofit part.
First the api/albums.cljd:
The important part here is the require, as you can see, to import local Dart file we need to ["api_client.dart" :as api] by default ClojureDart looking in lib/ so we don't have to add it.
Then we create a def for our base url. Then a def to configure dio, it will be use by retrofit. Finally we create the api-client with the api/RestClient we created before.
Then get-albums just call the (-> api-client(.getAlbums)) from the RestClient dart. The documentation for the -> macro can be find here .
Now we have a list of Album with a proper type. So to use it we can change some code in the main.cljd:
We change the call to :watch [response (api.albums/get-albums)] then we use it in the builder list (.-title album) to get the title from the model Album for example.
In the detail view we change some code too:
Here we can see (.-userId album) to get the userId from the Album class.
Conclusion
And it's done, we have changed the api part to be more typed, and safe. And use local generated code thanks to retrofit. We can still change the baseUrl inside ClojureDart or other BaseOptions settings. Well done !
Like always feel free to fork/clone/reuse the code I use for this article: