question

kristian avatar image
kristian asked

Playfab Cloudscript to Azure Functions context and arguments in Python,Playfab Cloduscript to Azure Function context and arguments

Hello,

We are migrating to Azure Functions, however we have couple of questions and we couldn't find an answer in the documentation so far.

1. How are the Playfab Cloudscript Arguments passed to Azure Functions? We have looked into the body and headers but we don't receive anything. We use Python.

2.How to retrieve also the PlayerID from the context when entering or leaving an entity segment? Again using Python.

Thank you in advance!

Kris

,
5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Seth Du avatar image Seth Du ♦ commented ·
  1. May I have your code snippet? You need to define FunctionParameter property in the request of ExecuteFunction API call.
  2. Do you mean you are configuring the actions for "Entered segment" and "Left segment"? All the context data model can be found at Azure Functions Context Models and usually you are able to get context via following code:
FunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<FunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

I suggest simply printing it out as a string for testing so that you can know the structure of the context in a triggered execution.

0 Likes 0 ·
kristian avatar image kristian Seth Du ♦ commented ·

We have used the hello world example and we want to make adjustment to it so we can pass the name as an argument in Playfab here:

import logging

import azure.functions as func


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    name = req.params.get('name')
    if not name:
        try:
            req_body = req.get_json()
        except ValueError:
            pass
        else:
            name = req_body.get('name')

    if name:
        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.{req.headers}, {req.params}, {req.route_params}")
    else:
        return func.HttpResponse(
             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
             status_code=200
        )


0 Likes 0 ·
kristian avatar image kristian kristian commented ·

The screenshot didn't get attached

0 Likes 0 ·
argument.png (7.8 KiB)
samuelhammer avatar image samuelhammer commented ·

Hi there, i'm still struggling with this issue. I get

errorMessage': 'Invocation of cloud script function GrpCreate failed'

with following lines

request = {<br>    "Entity": {<br>        "id": usr.entityid,"Type": "title_player_account"},"FunctionName": "GrpCreate","FunctionParameter": {<br>        "p1": player1id,<span>"p2": player2id<br>    },<span>"GeneratePlayStreamEvent": False}<br><br>PlayFabCloudScriptAPI.ExecuteFunction(request<span>, self.creategroupcb)</span></span></span>

CloudScript: function is up and running in Azure and as well is the PlayFab Functions endpoint (GrpCreate) set:

import azure.functions as func<br><br>def main(req: func.HttpRequest) -> func.HttpResponse:<br>    req_body = req.get_json()<br>    p1 = req_body["FunctionArgument"]["p1"]<br>    return func.HttpResponse(p1)
I just wanna know if i have some major syntax errors. Esp. can i pass more than one FunctionParameter?Cheers
0 Likes 0 ·
samuelhammer avatar image samuelhammer samuelhammer commented ·

Okay, sorry... I tried it with only one Input Parameter and it works, so i will stringify the dictionary and convert it again in the script

0 Likes 0 ·

1 Answer

·
Seth Du avatar image
Seth Du answered

Because this hello world sample is designed for general use, while PlayFab request has specific format, you may simply return the req_body to review it. The format will be like the below.

{
    'CallerEntityProfile': {...}
    'TitleAuthenticationContext': {...}, 
    'GeneratePlayStreamEvent': True, 
    'FunctionArgument': {...}
} 

Here is my sample code, which will return the value of argument “name”.

import azure.functions as func

def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    req_body = req.get_json()
    name = req_body["FunctionArgument"]["name"] 
    return func.HttpResponse(name)
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.