Python HTTP REST API Client Example

 1import requests
 2import time
 3
 4#  reynard-the-robot base urle
 5base_url = "http://localhost:29201/api"
 6
 7# Read the current state
 8response = requests.get(base_url + "/state")
 9print(response.json())
10
11# Teleport the robot
12requests.post(base_url + "/teleport", json={"x": 100, "y": -200}).raise_for_status()
13
14# Drive the robot
15requests.post(base_url + "/drive_robot", json={"vel_x": 500, "vel_y": -200}).raise_for_status()
16
17time.sleep(1)
18
19# Stop the robot
20requests.post(base_url + "/drive_robot", json={"vel_x": 0, "vel_y": 0}).raise_for_status()
21
22# Set the arm position
23requests.post(base_url + "/set_arm_position", json={"q1": 100, "q2": -30, "q3": -70}).raise_for_status()
24
25# Drive the arm
26requests.post(base_url + "/drive_arm", json={"q1": 10, "q2": -30, "q3": -15}).raise_for_status()
27
28time.sleep(1)
29
30# Stop the arm
31requests.post(base_url + "/drive_arm", json={"q1": 0, "q2": 0, "q3": 0}).raise_for_status()
32
33# Read the color
34response = requests.get(base_url + "/color")
35print(response.json())
36
37# Set the color
38requests.post(base_url + "/color", json={"r": 1, "g": 0, "b": 0}).raise_for_status()
39
40time.sleep(1)
41
42# Reset the color
43requests.post(base_url + "/color", json={"r": 0.929, "g": 0.49, "b": 0.192}).raise_for_status()
44
45# Say something
46requests.post(base_url + "/say", json={"message": "Hello, World From HTTP!"}).raise_for_status()
47
48# Read message queue
49response = requests.get(base_url + "/messages")
50print(response.json())