The requester
sub-module contains one class, designed to build, handle and execute requests to the Stack Exchange API.
It is intended as the primary interface into APiPy, and to be used with the response
sub-module to handle the
returned responses.
The APIRequester
class, as summarised, is the primary interface your code has to the library. It's intended to be a
complete package that enables you to fire off a basic request to the API without having to touch any other classes.
request_tokens
Type: Dictionary
Holds request tokens for this requester instance. Request tokens are defined as one of a request key, a client ID, and an access token. You can use this object directly for getting, setting, or checking request tokens; there are also methods provided to perform those tasks if you prefer using methods.
-
Param
request_key
: An optional request key for the API, to grant a higher request quota. Type: string.
Paramclient_id
: An optional client ID, as provided by Stack Apps registration, identifying your application. Type: string.
Paramaccess_token
: An optional access token, used to give authorisation for write operations with the API. Type: string.This is simply the class initializer method, and doesn't do anything special. If you pass any parameters to it, it will add them to the
request_tokens
dictionary so that they can be accessed both internally and by your code at a later point. You can add, change or delete request tokens after initialization, though - it's not imperative that you pass them in here. -
Param
token_name
: The name of the request token you want to check existence of.This is one of the methods provided to work with the
request_tokens
dictionary, instead of directly interacting with the dictionary itself. This method checks for existence of a particular token: that is, that there is a field in the dictionary with the token's name, and that its value isn'tNone
. You should pass in one ofrequest_key
,client_id
, oraccess_token
- those are the names under which the respective tokens are stored. -
Param
token_name
: The name of the request token to fetch a value for.This method complements
has_request_token
andset_request_token
. It does pretty much what it says on the tin (and the docstring) - returns the value of a given request token. ReturnsNone
if the request token doesn't exist, orNone
if that's the token's actual value. For most purposes, non-existent is functionally no different from aNone
value. -
Param
token_name
: The name of the request token whose value you want to set.
Paramtoken_value
:: The new value of the token.The third of the trilogy. Again, does what it says on the signature - sets the value of the request token called
token_name
to your new value intoken_value
.This method doesn't actually check whether you're using a valid request token name (i.e.
request_key
,client_id
oraccess_token
), so if you have a typo it's pretty likely to cause a bug. However, this is almost a non-issue, so I don't envision fixing this. -
Param
route
: The API route to request from.
Paramdata
: Optional data to send with the request.This is the main method in the class. It takes an API route and optionally extra data to add to the request, and makes the request. It returns an
APIResponse
object, which is documented in the documentation for theresponse
sub-module.A note on API routes. A normal URL to the SE API will look something like this:
https://api.stackexchange.com/2.2/questions
The route, as I've defined it, is the part after the version indicator - so in this case, it's
/questions
.In your route, you can include named parameters. Some API methods require a set of IDs (or similar) in the middle of the path - you can deal with this by using a named parameter. If you pass a
route
like this:/answers/{ids}/action
and additionally include an
ids
field in thedata
dictionary, then the{ids}
in the route will be rewritten to the data in the dictionary before sending. So, take this code:requester = APIRequester(some_request_key) request_data = { 'ids': [27743, 29924, 34623] } requester.make_request("/answers/{ids}/action", data=request_data)
This would, after processing, send a request to
https://api.stackexchange.com/2.2/answers/27743;29924;34623/action
. The name of the parameter doesn't have to be 'ids', and it can be either a string to substitute, or a list (which will be joined with semicolons).