Skip to content

Commit 3c70522

Browse files
authored
implement addSession (#159)
1 parent ca4853f commit 3c70522

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

src/navability/entities/session.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class SessionSchema(Schema):
4343
id = fields.UUID(allow_none=True)
4444
label = fields.String(required=True)
4545

46-
metadata = fields.Method("get_metadata", "set_metadata")
46+
metadata = fields.Method("get_metadata", "set_metadata", allow_none=True)
4747
originLatitude = fields.String(allow_none=True)
4848
originLongitude = fields.String(allow_none=True)
4949
createdTimestamp = fields.DateTime(required=True)
@@ -63,7 +63,7 @@ class SessionSchema(Schema):
6363
@post_load
6464
def make_session(self, data, **kwargs):
6565
return Session(**data)
66-
66+
6767
def get_metadata(self, obj):
6868
return base64.b64encode(json.dumps(obj).encode())
6969

src/navability/services/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from .blobentry import *
1111
from .loader import *
1212
from .graph import *
13+
from .session import *
1314

1415
# Maps
1516
from .map import *

src/navability/services/blobentry.py

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ async def listBlobEntriesAsync(
119119
entry['label'] for entry in entries
120120
]
121121

122+
122123
def listBlobEntries(
123124
fgclient: DFGClient,
124125
variableLabel: str,

src/navability/services/loader.py

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from gql import gql # used to parse GraphQL queries
88
from graphql import GraphQLSyntaxError # used to handle GraphQL errors
99

10+
1011
# Define a class to represent a GraphQL fragment with a name and data
1112
class Fragment:
1213
def __init__(self, name: str, data: str):

src/navability/services/session.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import logging
2+
import asyncio
3+
from navability.entities.navabilityclient import (
4+
NavAbilityClient,
5+
# QueryOptions,
6+
MutationOptions,
7+
)
8+
from navability.entities.session import SessionSchema
9+
from navability.services.loader import GQL_OPERATIONS
10+
from navability.common.versions import payload_version
11+
12+
import nest_asyncio
13+
nest_asyncio.apply()
14+
15+
logger = logging.getLogger(__name__)
16+
17+
18+
async def addSessionAsync(
19+
navAbilityClient: NavAbilityClient,
20+
userLabel: str,
21+
robotLabel: str,
22+
sessionLabel: str,
23+
):
24+
"""Add a new session to the given user and robot.
25+
26+
Args:
27+
navAbilityClient (NavAbilityClient): The NavAbility client.
28+
"""
29+
params = {
30+
"userLabel": userLabel,
31+
"robotLabel": robotLabel,
32+
"sessionLabel": sessionLabel,
33+
"version": payload_version,
34+
}
35+
result = await navAbilityClient.mutate(
36+
MutationOptions(
37+
GQL_OPERATIONS["MUTATION_ADD_SESSION"].data,
38+
params,
39+
)
40+
)
41+
42+
return SessionSchema().load(result["addSessions"]['sessions'][0])
43+
44+
45+
def addSession(
46+
navAbilityClient: NavAbilityClient,
47+
userLabel: str,
48+
robotLabel: str,
49+
sessionLabel: str,
50+
):
51+
tsk = addSessionAsync(navAbilityClient, userLabel, robotLabel, sessionLabel)
52+
return asyncio.run(tsk)

src/sdkCommonGQL

tests/test_session.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import random
2+
import string
3+
from navability.entities.navabilityclient import NavAbilityHttpsClient
4+
from navability.services import addSession
5+
6+
7+
def test_addSession():
8+
client = NavAbilityHttpsClient()
9+
userLabel = "[email protected]"
10+
robotLabel = "TestRobot"
11+
sessionLabel = "Test_addSession_" + "".join(
12+
random.choices(string.ascii_letters + string.digits, k=4)
13+
)
14+
session = addSession(client, userLabel, robotLabel, sessionLabel)
15+
assert session.label == sessionLabel

0 commit comments

Comments
 (0)