API: Sorting

From Yombo
Jump to: navigation, search

Overview

See also

Sorting is primarily used for low powered devices that wish to consume the data in a specific order. When possible, sorting should be avoided as response times can be slower in order to sort the records.

Syntax

Sorting is controlled through the sort query string parameter with the value being a list of field or fields and their order.

By default, the sort is in ascending order (a-z, 0-9). To sort in descending order, place a minus in front of the field name to be sorted. See examples section for usage.

Examples

Sort the response by the label.

  • GET /api/v1/commands?page[size]=3&sort=label
    • This will sort by the field 'label', in ascending order
  • GET /api/v1/commands?page[size]=3&sort=-label
    • This will sort by the field 'label', in descending order. Note the minus in front of 'label'.
  <div id="curl<random_number></random_number>" class="tab-pane fade in active" style="border: 1px solid #ccc; border-radius: 5px; padding: 0.7em;">
curl \
-H "x-api-key: abc123xyz789" \
-H "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...." \
-H "Accept: application/json" \
https://api.yombo.net/api/v1/commands?page[size]=3&sort=-label
  <div id="python<random_number></random_number>" class="tab-pane fade" style="border: 1px solid #ccc; border-radius: 5px; padding: 0.7em;">
import requests

url = 'https://api.yombo.net/api/v1/commands?page[size]=3&sort=-label'
headers = {
    'x-api-key': 'abc123xyz789',
    'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJ...',
    'Accept': 'application/json',  # demo, this is the default
    }
response = requests.get(url, headers=headers)
the_data = response.json()  # create a dictionary from results
  <div id="output<random_number></random_number>" class="tab-pane fade" style="border: 1px solid #ccc; border-radius: 5px; padding: 0.7em;">
 1 {
 2     "data": [
 3         {
 4             "type": "commands",
 5             "id": "VLxj6q912gipq4Neb",
 6             "attributes": {
 7                 "id": "VLxj6q912gipq4Neb",
 8                 "voice_cmd": "volume_up",
 9                 "machine_label": "volume_up",
10                 "label": "Volume Up",
11                 "description": "Send a volume up command.",
12                 "public": 2,
13                 "status": 1,
14                 "created_at": 1482065387,
15                 "updated_at": 1482065387
16             },
17             "links": {
18                 "self": "https://api.yombo.net/api/v1/commands/VLxj6q912gipq4Neb"
19             }
20         },
21         {
22             "type": "commands",
23             "id": "PVdmOoZGlPCEBKAn6",
24             "attributes": {
25                 "id": "PVdmOoZGlPCEBKAn6",
26                 "voice_cmd": "volume_down",
27                 "machine_label": "volume_down",
28                 "label": "Volume Down",
29                 "description": "Send a volume down command.",
30                 "public": 2,
31                 "status": 1,
32                 "created_at": 1482065387,
33                 "updated_at": 1482065387
34             },
35             "links": {
36                 "self": "https://api.yombo.net/api/v1/commands/PVdmOoZGlPCEBKAn6"
37             }
38         },
39         {
40             "type": "commands",
41             "id": "VwvXBa1WLhrnBY182",
42             "attributes": {
43                 "id": "VwvXBa1WLhrnBY182",
44                 "voice_cmd": "vent",
45                 "machine_label": "vent",
46                 "label": "Vent",
47                 "description": "To vent something.",
48                 "public": 2,
49                 "status": 1,
50                 "created_at": 1504771881,
51                 "updated_at": 1504771881
52             },
53             "links": {
54                 "self": "https://api.yombo.net/api/v1/commands/VwvXBa1WLhrnBY182"
55             }
56         }
57     ],
58     "meta": {
59         "count": 3,
60         "current_page": 1,
61         "from": 1,
62         "last_page": 12,
63         "path": "https://api.yombo.net/api/v1/commands",
64         "per_page": 3,
65         "to": 3,
66         "total": 35,
67         "includable": [
68             "users"
69         ]
70     },
71     "links": {
72         "first": "https://api.yombo.net/api/v1/commands?page%5Bsize%5D=3&sort=-label&page%5Bnumber%5D=1",
73         "last": "https://api.yombo.net/api/v1/commands?page%5Bsize%5D=3&sort=-label&page%5Bnumber%5D=12",
74         "prev": null,
75         "next": "https://api.yombo.net/api/v1/commands?page%5Bsize%5D=3&sort=-label&page%5Bnumber%5D=2",
76         "self": "https://api.yombo.net/api/v1/commands"
77     }
78 }
This page was last edited on 11 April 2019.