Package: pyquestionit
pyquestionitSimpleQuestionIt.spaceAPI client for Python 3.6+.Seedocsfor more details.This documencation is a WIP. It should be completed later.Installing the packageInstall the package using pip.pipinstallpyquestionitYou're ready to import it in your project.importpyquestionitGetting startedMake unlogged requestsYou can easily make requests to the QuestionIt.space API with the generic HTTP methods.get,.post,.put,.patchand.delete.
Find documentation about every endpoint in thedocumentation.# Instanciate the clientclient=pyquestionit.QuestionIt()# Make unlogged requests to allowed endpointsusers=client.get('users/find',params={'q':'questionit'})# users == [{'id': '1', 'slug': 'questionitspace', ...}]For every method, you can find the following parameters:endpoint(str): Specify the endpoint (mandatory)params(dict): Specify query or body parameters of your request. It will be automatically formatted.headers(dict): If you want to set custom headers.auth(boolorstr): IfTrue(default), request will use registred token. Ifstr, use the string as token. IfFalse, disable auth.with_rq(boolean): IfTrue, return the response object instead of the direct result. You can useresponse.json()to get result.Endpointparameter is the remaining part afterhttps://api.questionit.space/URL.
For example, for endpointhttps://api.questionit.space/users/find, parameter should beusers/find.Make an authentificated requestThis kind of request requires anaccess token. If you don't have it yet, jump toAuthentificationpart.You can specify your token insideQuestionItconstructorclient=pyquestionit.QuestionIt('some-token-here');or just useset_access_token()method.client.set_access_token('some-token-here');Token will be automatically added to request headers.relationship=client.get('relationships/with/2');print(f"You{'follow'ifrelationship.followingelse'do not follow'}user #2")ErrorsThis library usesrequestspackage and raise exceptions when HTTP status code is not a success.
You can catch exceptions withrequests.exceptions.HTTPErrortype:importrequeststry:users=client.get('users/find',params={'q':'questionit'})exceptrequests.exceptions.HTTPErrorase:request=e.requestresponse=e.responseresult=response.json()# Usually, result is an APIError result# Do something with response or result...print('Error code: ',result['code'])AuthentificationYou can generate login tokens and get access token through this library.Get a request tokenA request token is used to ask user to connect to your app.importurllib.parsetoken=client.get_request_token('app-key-here','redirect-url-after-confirm-or-deny'# or 'oob' for no redirection)token_encoded=urllib.parse.quote(token)url='https://questionit.space/appflow?token='+token_encoded# Send user to {url} !Get access tokenOnce user has approved the app, he will be redirected to your redirect URL (or will have an access PIN displayed).For redirect URLs, there's formed like:https://yoursite.com/callback?validator={validator}.You can extractvalidatorfrom query string, they're needed to generate access token.result=client.get_access_token('app-key-here',# You need to have original token, it should be stored somewhere on your side.# You can give an unique key into callback URL (like in query),# it will be keeped.'token-here','validator-or-PIN-here')print(f"Access token is{result['token']}.")You can now use this token with the instance.client.set_access_token(result['token'])Endpoint-based methodsThis Python library binds most of the endpoints of the API to specific methods, so you don't need to handle boring things by yourself.
Their usage is pretty straight-forward and don't need to be explained (the method parameters are usually whats API is taking), except for a few methods (see below).The following methods exists:.verify_token->GET auth/token/verify.revoke_token->DELETE auth/token.find_users->GET users/find.get_user->GET users/id/:idandGET users/slug/:slug.get_logged->GET users/logged.set_pinned->PATCH questions/pin.remove_pinned->DELETE questions/pin.set_muted_words->POST users/blocked_words.get_muted_words->GET users/blocked_words.ask->POST questions,POST questions/anonymousandPOST polls.waiting_questions->GET questions/waiting.reply->POST questions/answer.remove_question->DELETE questions.remove_muted_questions->DELETE questions/masked.like->POST likes.unlike->DELETE likes.likers_of->GET likes/list/:id.likers_ids_of->GET likes/ids/:id.questions_of->GET questions.asked_questions_of->GET questions/sent.home_timeline->GET questions/timeline.ancestors_of->GET questions/tree/:root.replies_of->GET questions/replies/:id.relationship_with->GET relationships/with/:id.relationship_between->GET relationships/between.follow->POST relationships/:id.unfollow->DELETE relationships/:id.followers->GET relationships/followers.followings->GET relationships/followings.block->POST blocks/:id.unblock->DELETE blocks/:id.get_notifications->GET notifications.remove_notification->DELETE notifications/:id.get_notification_count->GET notifications/count.notifications_all_mark_as_seen->POST notifications/bulk_seen.get_userThis method can fetch an user by user ID or by slug.It automatically choose between ID and slug regarding the given string ; if it's numeric, ID endpoint will be used.client.get_user('2')# calls users/id/2client.get_user('questionitspace')# calls users/slug/questionitspace.askYou can attach multiple choices "polls" directly with this method.
Just give a simple list of strings in thepollparameter.client.ask('Cat or dog?',user_id='2',in_reply_to='36',poll=['Cats!!','Dogs :(']).replyWhen you reply to a question, you can attach medias (JPEG, PNG and GIF images).You must attach the picture in thepictureparameter of.replyby following this example:importmimetypespath='path-to-file.ext'client.reply(answer='Yes, cats are the best.',question_id='32',picture=('picture',# Name, required for multipart/form-data sendopen(path,'rb'),# Open as 'rb' !mimetypes.guess_type(path)[0]# MIME type),)