Skip to content

Commit 4e79df4

Browse files
authored
Merge pull request #4 from PlayerData/3-add-example-for-querying-raw-data
feat: add raw data query examples
2 parents 6bbeef8 + cc7b988 commit 4e79df4

5 files changed

Lines changed: 138 additions & 30 deletions

File tree

README.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,39 @@ To request API credentials, please contact the PlayerData team at support@player
3535

3636
### Option 1: Use generated types with PlayerDataAPI
3737

38-
Example usage of this option is provided in the `example_pydantic.py` file.
38+
Example usage of this option is provided in the `examples/pydantic/` folder.
3939
The basic flow is to create a PlayerDataAPI instance, build the query objects using the code-generated Pydantic models (generated by ariadne-codegen), and then call the run_queries method.
4040

4141
To run an example of this option, you can use the following command:
4242

4343
```bash
44-
python example_pydantic.py
44+
python examples/pydantic/sessions_query.py
4545
```
4646

47+
To run this you will need to set the following environment variables or hardcode them in the file:
48+
```bash
49+
export CLIENT_ID=your_client_id
50+
export CLIENT_SECRET=your_client_secret
51+
export CLUB_ID=your_club_id
52+
```
53+
54+
4755
### Option 2: Use the GraphqlClient class directly
4856

49-
Example usage of this option is provided in the `example_direct.py` file.
57+
Example usage of this option is provided in the `examples/direct/` folder.
5058
The basic flow is to create a GraphqlClient instance, build the query string, and then call the query method.
5159

5260
To run an example of this option, you can use the following command:
5361

5462
```bash
55-
python example_direct.py
63+
python examples/direct/sessions_query.py
64+
```
65+
66+
To run this you will need to set the following environment variables or hardcode them in the file:
67+
```bash
68+
export CLIENT_ID=your_client_id
69+
export CLIENT_SECRET=your_client_secret
70+
export CLUB_ID=your_club_id
5671
```
5772

5873
## Authentication Types
Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@
33
from playerdatapy.gqlauth import AuthenticationType
44
import asyncio
55
from playerdatapy.constants import API_BASE_URL
6+
import os
67

7-
# Build out the query string.
8-
# Our GraphiQL Playground at https://app.playerdata.co.uk/api/graphiql/ is useful for building out and testing the query.
9-
CLIENT_ID = "your_client_id"
10-
CLIENT_SECRET = "your_client_secret"
8+
CLIENT_ID = os.environ.get("CLIENT_ID")
9+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
10+
SESSION_ID = os.environ.get("SESSION_ID")
1111

12-
example_query = """
13-
query Session($session_id: ID!) {
12+
# Build out the query string to get session participations and datafiles from a session.
13+
# Our GraphiQL Playground at https://app.playerdata.co.uk/api/graphiql/ is useful for building out and testing the query.
14+
session_query = """
15+
query GetSessionParticipations($session_id: ID!) {
1416
session(id: $session_id) {
15-
id
16-
startTime
17-
endTime
17+
sessionParticipations {
18+
id
19+
athlete {
20+
name
21+
}
22+
datafiles {
23+
url(format: json)
24+
}
25+
}
1826
}
1927
}
2028
"""
@@ -31,14 +39,13 @@ async def main(session_id: str):
3139
headers={"Authorization": f"Bearer {auth._get_authentication_token()}"},
3240
)
3341
response = await client.execute(
34-
query=example_query,
42+
query=session_query,
3543
variables={"session_id": session_id},
3644
)
3745
result = client.get_data(response)
38-
return result["session"]
46+
return result
3947

4048

4149
if __name__ == "__main__":
42-
session_id = "an_example_session_id"
43-
result = asyncio.run(main(session_id))
50+
result = asyncio.run(main(SESSION_ID))
4451
print(result)

examples/direct/sessions_query.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from playerdatapy.gqlauth import GraphqlAuth
2+
from playerdatapy.gqlclient import Client
3+
from playerdatapy.gqlauth import AuthenticationType
4+
import asyncio
5+
from playerdatapy.constants import API_BASE_URL
6+
import os
7+
from datetime import datetime, timedelta
8+
9+
CLIENT_ID = os.environ.get("CLIENT_ID")
10+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
11+
CLUB_ID = os.environ.get("CLUB_ID")
12+
13+
14+
# Build out the query string to get session details.
15+
# Our GraphiQL Playground at https://app.playerdata.co.uk/api/graphiql/ is useful for building out and testing the query.
16+
example_query = """
17+
query($clubIdEq:ID!,$startTimeGteq:ISO8601DateTime,$endTimeLteq:ISO8601DateTime){
18+
sessions(filter: {clubIdEq:$clubIdEq, startTimeGteq:$startTimeGteq, endTimeLteq:$endTimeLteq}){
19+
id
20+
startTime
21+
endTime
22+
}
23+
}
24+
"""
25+
26+
27+
async def main():
28+
auth = GraphqlAuth(
29+
client_id=CLIENT_ID,
30+
client_secret=CLIENT_SECRET,
31+
type=AuthenticationType.CLIENT_CREDENTIALS_FLOW,
32+
)
33+
client = Client(
34+
url=f"{API_BASE_URL}/api/graphql",
35+
headers={"Authorization": f"Bearer {auth._get_authentication_token()}"},
36+
)
37+
startTimeGteq = (datetime.now() - timedelta(days=30)).isoformat()
38+
endTimeLteq = datetime.now().isoformat()
39+
variables = {
40+
"clubIdEq": CLUB_ID,
41+
"startTimeGteq": startTimeGteq,
42+
"endTimeLteq": endTimeLteq,
43+
}
44+
response = await client.execute(query=example_query, variables=variables)
45+
result = client.get_data(response)
46+
return result["sessions"]
47+
48+
49+
if __name__ == "__main__":
50+
result = asyncio.run(main())
51+
print(result)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import asyncio
2+
3+
from playerdatapy.playerdata_api import PlayerDataAPI
4+
from playerdatapy.custom_queries import Query
5+
from playerdatapy.custom_fields import (
6+
SessionInterface,
7+
SessionParticipationInterface,
8+
AthleteFields,
9+
EdgeDataFileFields,
10+
)
11+
from playerdatapy.enums import DatafileFormat
12+
from playerdatapy.gqlauth import AuthenticationType
13+
import os
14+
15+
CLIENT_ID = os.environ.get("CLIENT_ID")
16+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
17+
SESSION_ID = os.environ.get("SESSION_ID")
18+
19+
# Create a PlayerDataAPI instance.
20+
api = PlayerDataAPI(
21+
client_id=CLIENT_ID,
22+
client_secret=CLIENT_SECRET,
23+
authentication_type=AuthenticationType.CLIENT_CREDENTIALS_FLOW,
24+
)
25+
26+
# Build out the query to get session participations and datafiles from a session.
27+
session_query = Query.session(id=SESSION_ID).fields(
28+
SessionInterface.session_participations().fields(
29+
SessionParticipationInterface.id,
30+
SessionParticipationInterface.athlete().fields(
31+
AthleteFields.name,
32+
),
33+
SessionParticipationInterface.datafiles().fields(
34+
EdgeDataFileFields.url(format=DatafileFormat.json),
35+
),
36+
),
37+
)
38+
39+
# Run the query.
40+
response = asyncio.run(api.run_queries("GetSessionParticipations", session_query))
41+
print(response)
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33

44
from playerdatapy.playerdata_api import PlayerDataAPI
55
from playerdatapy.custom_queries import Query
6-
from playerdatapy.custom_fields import SportDefinitionFields, SessionInterface
6+
from playerdatapy.custom_fields import SessionInterface
77
from playerdatapy.input_types import SessionsSessionFilter
88
from playerdatapy.gqlauth import AuthenticationType
9+
import os
910

10-
CLIENT_ID = "your_client_id"
11-
CLIENT_SECRET = "your_client_secret"
12-
CLUB_ID = "your_club_id"
11+
CLIENT_ID = os.environ.get("CLIENT_ID")
12+
CLIENT_SECRET = os.environ.get("CLIENT_SECRET")
13+
CLUB_ID = os.environ.get("CLUB_ID")
1314

1415
# Create a PlayerDataAPI instance.
1516
api = PlayerDataAPI(
@@ -19,13 +20,6 @@
1920
)
2021

2122
# Build out the query objects.
22-
sports_query = Query.sports().fields(
23-
SportDefinitionFields.id,
24-
SportDefinitionFields.name,
25-
SportDefinitionFields.is_indoor,
26-
SportDefinitionFields.has_pitch_definition,
27-
)
28-
2923
sessions_query = Query.sessions(
3024
filter=SessionsSessionFilter(
3125
clubIdEq=CLUB_ID,
@@ -38,6 +32,6 @@
3832
SessionInterface.end_time,
3933
)
4034

41-
# Run the queries.
42-
response = asyncio.run(api.run_queries("sports", sports_query, sessions_query))
35+
# Run the query .
36+
response = asyncio.run(api.run_queries("Sessions", sessions_query))
4337
print(response)

0 commit comments

Comments
 (0)