Not long ago I’ve mentioned Apixu in a post about handling errors. I’ve find out about this service on DevForum, a development discussions platform I visit daily. What I like the most about Apixu is that they have various languages libraries for consuming their API. Not great libraries and not all of them are complete, but they try to offer as much variations as they can for their service.
I noticed they were missing a Go library and I was missing an idea to learn new things on. And I just started writing the code until it got to a full package which covers all API methods with error handling, both JSON and XML formats, unit tested, versioned.
It has a simple interface which clearly defines the API methods with their input parameters and responses. And it can be extended for custom needs.
Some important things I learned from the process are simplicity, segregation and isolation, specific errors, memory management, and creating custom marshalers.
Check it out on Github. See documentation for the package and for the API.
In the end, they adopted my package among their official ones.
The past days I’ve practiced Go by writing a package for Apixu weather service. They have a a straightforward REST service to offer their weather information.
Their service also returns an error response if it can’t offer data based on your needs, if you use an invalid API key, or for other cases. Of course, the Go package should also return errors if the case.
No problem with returning data, but I had some issues handling errors. There can be general errors that have nothing to do with Apixu (but with the package internals) and errors returned by them. My first approach was to return three values for each API method:
Search(q string) (Search, ApixuError, error)
But it smelled right away. There had to be another way. Continue reading Handling API errors