Core: modularize client and use exceptions.

Use proper exceptions when needed and modularize the client that queries the API.