In the first article of my series on using ServiceStack I mentioned the feature of the framework that the individual components can be used independently of each other.
This short article is intended as an example of how the C# client can be used separately from a custom service to communicate with third-party APIs. If you would like to know more about the client specifically, you can read my article on this feature.
I would like to use coinmarketcap.com‘s API to access statistics on different crypto currencies. The API documentation is available on the projects website. Within the scope of this article, there are two things I want to achieve: Querying all available currency data, which contains the 100 strongest crypto currencies in terms of turnover, as well as the targeted query of a single currency.
We receive all the necessary information from the docs. These are the paths of the service as well as the structure of the objects we’d like to receive.
I have decided to use Bitcoin as an example, which leads to the following two paths for use in the project:
… And Execution
Since we have no influence on the objects we receive from api, we have to adapt a class accordingly. In order to be able to design the properties according to my own ideas, I use the attributes [DataContract] and [DataMember], so that the properties do not have to be named identically to those of the API.
And that was actually the most complicated part. To obtain the data through the API, I directly specify the corresponding paths from the documentation, since we do not use request DTOs.
Since the API always returns an array, I simply return the first element from the Bitcoin queries response. As promised, the implementation of an external service with the ServiceStack Client is quick and easy as usual. The concluding image shows the result of the query.
Subscribe via RSS