Implementing Python CryptoCompare API Client: Monitoring Cryptocurrency Markets
What is CryptoCompare?
CryptoCompare is an interactive platform where you can discuss the latest crypto trends and monitor all cryptocurrency markets streaming in real time. It provides a public API which allows getting cryptocurrency pricing, OHLC and volume data for multiple exchanges.
How did it Start?
While working on one of the Django framework based projects we implemented ICO monitoring. We fetched data from CryptoCompare public APIs using Python. I looked for a Python client but it is not available, so I have started thinking about making my own.
Why is it Needed?
We can implement an API integration by simply putting the code in Django views, and finish the task! But wait, would that be really helpful to test the code properly? It would be tightly coupled with other common web app code and unmaintainable! Or as a better approach extract it out as a separate utility class just handling the API stuff.
Instead, as a better approach, we planned to extract it out as a separate class just for handling the API stuff. So the code has been moved from views to a separate class having all the necessary API methods I needed. Good, now we have the code separated, easy to change, test and easy to understand! The first milestone achieved!
This is what we need, API client for Python as it does not need the developer to read and remember everything for the API. Instead, he can just focus on “which methods I need to use?”
Implementation: CryptoCompare API
After having a Python class, I have added all the methods as placeholders I wanted to implement so that I can use them in my Django view/model code. I decided to implement the framework first, so that I don’t need to remember what I need to do next, just have to focus on filling in the blanks! See the first file in the code below.
Implement the business logic
Now, I have implemented the business logic for all the methods I have added in the first step. Here, we will add the real code which calls the RESTful API and generates the required data in JSON format. See the second file in the code below.
Refactor the code
If you notice here, there are many lines repeated more than twice and it is a candidate for DRY – Extract them as methods, and reuse! So the first refactoring will extract the URLs as constants and extract the method for HTTP URL call and JSON parsing. See the third file in the code below.
Redesign the structure
After adding all the public API methods, I noticed that the class is now big and having 21 public API methods and 3 helper methods. So the next step would be to design the code having a modular structure, keeping the runtime behavior similar. For that, we have grouped all the related methods and separated them into different python module files. Now each file will have only a few methods!
So the next step would be to design the code having a modular structure, keeping the runtime behavior similar. For that, we have grouped all the related methods and separated them into different python module files. Now each file will have only a few methods!
Then, to keep the runtime behavior as it is, we have included the methods in the same class. This way, the class is modular now but having all public methods available as instance methods.
And then finally, we added API documentation! Done!
You can access the full source code at https://github.com/BoTreeConsultingTeam/crypto_compare
At BoTree Technologies, we build enterprise applications with our Python team of 15+ engineers.
Consulting is free – let us help you grow!
Choose Your Language
- Digital Marketing
- IT Consulting
- Project Management