Python

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

Get Game Leaders


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 getGameLeaders

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 proceed to check whether there are any records. If there are no records, we will display 'No record Found'.

If there are records, we will use the getRecord() function to check whether there is only 1 record, or more than 1 records, and display the results accordingly.

**NOTE: JSON output for 1 record and more than 1 record is different due to JSON structure. Hence, we use getRecord() function to retrieve the number of records.

Else, we will display the ErrorText.


Entire Code:


import requests, json

from functions import url,getRecord


def getGameLeaders():

   #Header

   serviceName = 'getGameLeaders'

   #Content

   gameID = '1'

   start = '2010-10-10 00:00:00'

   end = '2018-10-10 00:00:00'

   mode = 'Pretest'

   byGroup = 'true'

   

   headerObj = {

                       'Header' : {

                       'serviceName': serviceName,

                       'userID': '',

                       'PIN': '',

                       'OTP': ''

                       }

                       }

   contentObj = {

                        'Content': {

                        'gameID': gameID,

                        'start': start,

                        'end':end,

                        'mode': mode,

                        'byGroup': byGroup

                        }

                        }

   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':

       leader = response.json()['Content']['ServiceResponse']['LeaderDetails']

       if leader =={}:

           print("No record found!")

       else:

           leader_list = leader['Leaders']['Leader']

           recordCount = getRecord(leader_list)

           print("\nNo.\t         UserID\t\tScore")

           if recordCount >1:

               for i in range(recordCount):

                   users = leader_list[i]

                   print("{}.\t{:>15}\t\t{}".format(i+1,users['user_Id'],users['score']))

           else:

               print("{}.\t{:>15}\t\t{}".format(1,leader_list['user_Id'],leader_list['score']))

   else:

       print(serviceRespHeader['ErrorText'])


getGameLeaders()


Download


Go to Common Stuff Section

Created with the Personal Edition of HelpNDoc: Full-featured EBook editor