[Tutor] Python advice about API, JSON
Patrick Thunstrom
pathunstrom at gmail.com
Mon Sep 22 22:10:13 CEST 2014
On Mon, Sep 22, 2014 at 4:01 PM, Juan Christian
<juan0christian at gmail.com> wrote:
> I'm trying to make my script the more pythonic possible. How would a good
> approach regarding external API and json be?
I don't think dealing with a specific API is actually appropriate for
this list. I'll try to answer the general elements.
> I'm calling this API (http://docs.themoviedb.apiary.io/) using 'requests'.
> The thing is that I need to do a bunch of different calls to the API:
>
> - http://api.themoviedb.org/3/person << Receive ID and return person info
> - http://api.themoviedb.org/3/tv << Receive ID and return serie info
> - http://api.themoviedb.org/3/movie << Receive ID and return movie info
> - http://api.themoviedb.org/3/search << Receive "Name of person or serie or
> movie" and return ID
>
> I have the following structures:
>
> - {query}/{type}?api_key={key}&query={person or tv or movie}
> - {query}/{id}?api_key={key}
> - {query}/{id}/credits?api_key={key}
> - {query}/{id}/season/{season_numer}?api_key={key}
> - {query}/{id}/season/{season_numer}/episode/{episode_number}?api_key={key}
> -
> {query}/{id}/season/{season_numer}/episode/{episode_number}/credits?api_key={key}
>
>
> I'm thinking about creating a class 'API' and have all these URLs and
> structures there. The thing is that I need to pass my API_KEY and this key
> will be used for everything, so I just need to pass it once, how can I do
> that? Yes, I know about class attributes, but the thing is that I will call
> this class 'API' from different classes and I'll need to instantiate a API
> obj in each of them, is it a good approach? Inside this 'API' class I would
> have a __init__ that would receive an API_KEY and different methods, each
> for a type of query (person, tv, movie, search, credits).
First question: Why do you need multiple versions of this theoretical
class floating around? Perhaps if you wrote a single module api that
handles interactions with the API, you can then import that module
into any other module that will use the API, and you only have to
write all of this once.
> Maybe my explanation is a bit confusing but I hope you guys understood,
> anyway, you can ask for more information if needed!
>
> And another thing, What's better, a single module with multiple classes
> (total lines: ~110) or multiple modules with 1-3 classes (correlated, like
> class serie, season and episode in the same module, and class person and
> character in the same module) each?
I personally prefer multiple modules, though it absolutely depends on
the demands of the project.
More information about the Tutor
mailing list