Python API client – Code Review Stack Exchange

Related movie: GEMINI BITCOIN EXCHANGE Ondergrens WITHDRAWAL REQUIREMENT


I’m creating a Python API client, after thinking and checking some open source Python clients I created this class, and I’m looking for any terugkoppeling (vormgeving, best practices. ), this is an example URL of the API:

And here’s a usage example:

My questions are:

  • Is this a good vormgeving for use ter a webstek, and is it safe for numerous requests?
  • Should I store the API result te a property api.result instead of returning it when calling api.request() ?
  • How can I improve the way I get the API result? Is the 2nd example better?
  • How can I improve my code?

1 Response

Python has a mechanism for treating exceptional situations, but you go to fine lengths to suppress it: you catch all the exceptions that might result from your request method and substitute them with a string ter the .error property of the APIClient object.

This has several problems.

Very first, it needlessly complicates every API call. A caller can’t just write:

permitting exceptions to pass up the call stack and so eventually show up on the console or te the loom. Instead, they have to write:

This means that every call to your API needs to consider how to treat errors. What are programmers going to do? Well, mostly likely they will raise thesis errors:

So why bother suppressing thesis errors te your API te the very first place?

2nd, you substitute all exceptions that you don’t recognize with An unexpected error occurred. So this makes it unlikely to distinguish a ProxyError from a TooManyRedirects from an SSLError .

Third, you lose information that wasgoed present ter the exception objects. For example, an SSLError comes with a description of the problem, for example if the certificate doesn’t match the domain, you’ll get an error like this:

This kleintje of information is básico te tracking down the cause of problems. But you substitute this with An unexpected error occurred which is, frankly, futile.

If the status code is not 200, you:

Related movie: The Winklevoss Twins’ Bitcoin Exchange Opens ter the U.K.


Which has three problems: (i) you should raise an example of an exception class, not the class itself, (ii) you should initialize the exception object with gegevens that describes the exception, and (iii) the class Exception is supposed to be the root of the class hierarchy for “built-in, non-system-exiting exceptions” and user exceptions. You shouldn’t raise Exception itself, but instead derive a specific exception class and raise an example of that. So here you would need something like:

But te fact the requests module already has a function for doing this, so all you need to do is:

Your interface for setting API parameters is to set them directly spil properties of the API object:

It would be better for the constructor to take thesis spil keyword arguments: (i) this permits the constructor to check that all required parameters have bot set, (ii) parameter values can be checked (if possible), and (iii) the keyword argument mechanism is more limber than object properties since you can pass a sets of keywords around te the form of a dictionary. So I would expect to write:

Related movie: BITCOIN COULD INCREASE 40X CAMERON WINKLEVOSS GEMINI EXCHANGE


3 thoughts on “Python API client – Code Review Stack Exchange”

  1. Any gepast amount of institutional money that comes into the spel when the ETF starts will shove the price higher.

Leave a Reply

Your email address will not be published. Required fields are marked *