Partial Response
This page will help you understand how Olympics API handles partial response
When building any networked application we understand how important it is to keep the data transferred over the network to an absolute minimum. This is especially important when building mobile and web applications where increased network overhead can seriously impact user experience (and also bandwidth usage bills!).
Recognising this, the Olympics API fully supports partial response when using the JSON media type, meaning that you can determine the exact fields you'd like returned in your API responses.
In order to return a partial representation of a response use the fields query parameter. Let's take a look at an example of how partial response could be used using this parameter.
To demonstrate how this can be used let's make the following request to return the 2 latest items:
curl \
-H "Accept: application/json" \
-H "apikey: <API KEY>" \
https://olympics.api.pamedia.io/v3/games
Upon execution, the Sport API returns a "full-fat" response:
{
"total": 4,
"item": [
{
"code": "OWG2018",
"description": "PyeongChang 2018 Olympics Winter Games"
},
{
"code": "PWG2018",
"description": "PyeongChang 2018 Paralympic Winter Games"
},
{
"code": "CG2018",
"description": "Gold Coast 2018 Commonwealth Games"
},
{
"code": "OG2020",
"description": "Tokyo 2020 Olympic Games"
}
]
}
Whilst this response is great if you require all of the metadata contained within the document, it's not great if your application only requires a certain selection of fields (description) or if your users are constrained by their network.
With this in mind, you can utilise the Olympics API's partial response feature to trim the response payload down to your exact requirements.
Let's try the same request but this time only selecting the JSON fields we're interested in:
curl \
-H "Accept: application/json" \
-H "apikey: <API KEY>" \
https://olympics.api.pamedia.io/v3/games?fields=items(description)
When executing this request, we are now returned only the fields we're interested in, meaning a considerably smaller payload for a client application to process. The subsequent response:
{
"item": [
{
"description": "PyeongChang 2018 Olympics Winter Games"
},
{
"description": "PyeongChang 2018 Paralympic Winter Games"
},
{
"description": "Gold Coast 2018 Commonwealth Games"
},
{
"description": "Tokyo 2020 Olympic Games"
}
]
}
The format of the fields request parameter value is loosely based on XPath syntax. Additional examples are provided in the following section:
- Use a comma-separated list to select multiple fields (e.g. total,limit,offset).
- Use a/b to select a field b that is nested within field a; use a/b/c to select a field c nested within b. For example:?fields=prize/value.
- Specify field sub-selectors to request only specific sub-fields by placing expressions in parentheses "( )" after any selected field. For example: ?fields=items(id) returns only the item id.
- Use wildcards in field selections, if needed. For example: ?fields=meta/* selects all items in the meta object.
Updated 6 months ago