Python

Features ›› Game ›› getGameQuestion ›› Sample Code ››
Parent Previous Next

Get Game Question


We will first import "request" and "json" module, as we need those module for calling the API

Then, we will import our own url function from a file called functions.py, which can be referred to in the "Common Stuff" section linked at the bottom.



Next, we create a function getGameQuestion

Inside the function, we will create the input variables.



Then we build both the header and content object.



Then, we will create the final url, where we will use the url() function to get the API url, and append the headerObj and contentObj to the API url.

We will then post the final_url and store the server response in the variable "response".

The GlobalErrorID is then retrieved and stored in variable "errorCode".



If the errorCode is '010000', it means that there is no error, and we will display the result.

Else, we will display the ErrorText.


Entire Code:


import requests, json

from functions import url


def getGameQuestion():

   #Header

   serviceName = 'getGameQuestion'

   #Content

   questionID = '1'

   

   headerObj = {

                       'Header': {

                       'serviceName': serviceName,

                       'userID': '',

                       'PIN': '',

                       'OTP': ''

                       }

                       }

   contentObj = {

                       'Content': {

                       'questionID': questionID

                       }

                       }

   final_url="{0}?Header={1}&Content={2}".format(url(),json.dumps(headerObj),json.dumps(contentObj))

   response = requests.post(final_url)

   serviceRespHeader = response.json()['Content']['ServiceResponse']['ServiceRespHeader']

   errorCode = serviceRespHeader['GlobalErrorID']


   if errorCode == '010000':

       QuestionDetails = response.json()['Content']['ServiceResponse']['QuestionDetails']

       choice = QuestionDetails['choices'].split("&&")

       print("Category: {}".format(QuestionDetails['category']))

       print("Question ID: {}".format(QuestionDetails['question_Id']))

       print("Question: {}".format(QuestionDetails['question']))

       print("Choices: ")

       for i in range(0,len(choice),1):

           print("{}. {}".format(i+1,choice[i]))

       

   else:

       print(serviceRespHeader['ErrorText'])


getGameQuestion()


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Easily create HTML Help documents