REST Client in Python
Introduction
A guide to using the GemFire REST API from a Python client.
GemFire REST Client in Python
At the end of this article is an example Python script that accesses a GemFire client using the REST API.
To run this example, GemFire must first be installed and a cluster must be started with the REST API enabled.
The step-by-step instructions:
To run this example, GemFire must first be installed and a cluster must be started with the REST API enabled.
The step-by-step instructions:
Install GemFire according the instructions found at Getting Started with VMware GemFire.
With the gemfire bin
directory in your command PATH
, start the GemFire shell:
> gfsh
Start a locator:
gfsh> start locator --name=locator1
Start a server with the REST API enabled. The example code expects the http service to be running on port 8080:
gfsh> start server --name=server1 --start-rest-api=true --http-service-port=8080 --http-service-bind-address=localhost --server-port=40405
Connect to the cluster in
gfsh:
gfsh> connect
Create a region with name “demoRegion” (as expected by the example code):
gfsh> create region --name=demoRegion --type=REPLICATE
Exit gfsh
gfsh> exit
Using python3, run the python example. The code is available Here is the code:
#!/usr/bin/env python3
# This assumes you have created a region called "demoRegion".
import json
import uuid
import requests
REGION = "demoRegion"
BASE_URI = "http://localhost:8080/gemfire-api/v1"
headers = {'content-type': 'application/json'}
person = {'type': 'Person',
'firstName': 'John',
'middleName': 'Q',
'lastName': 'Public',
'birthDate': '1 Jan 1900'}
def resource_uri(res=None, region=REGION):
if res:
return "%s/%s/%s" % (BASE_URI, region, res)
return "%s/%s" % (BASE_URI, region)
print("[*] First, we'll empty out our demo region - DELETE %s" %
requests.delete(resource_uri()))
r = requests.delete(resource_uri())
r.raise_for_status()
print("[*] Now, we'll create 5 demo objects")
keys = []
for i in range(1, 6):
key = uuid.uuid1()
keys.append(key)
person['uuid'] = str(key)
print("\t Creating object with key: POST %s" % key)
r = requests.post(resource_uri(), json=person,
params={'key': key},
headers=headers)
r.raise_for_status()
print("[*] List our keys - GET %s" % resource_uri("keys"))
r = requests.get(resource_uri("keys"))
print(r.text)
print("[*] Here's all our data - GET %s" % resource_uri())
r = requests.get(resource_uri())
print(r.text)
print("[*] Now each key one by one")
for key in keys:
print("Fetching key - GET %s" % resource_uri(res=key))
r = requests.get(resource_uri(res=key))
print(r.text)
print("[*] Now grab one, change the first name to 'Jane' and save it")
print(" GET - %s" % resource_uri(res=keys[0]))
r = requests.get(resource_uri(res=keys[0]))
p = json.loads(r.text)
p['firstName'] = 'Jane'
print(" PUT - %s" % resource_uri(res=keys[0]))
r = requests.put(resource_uri(res=keys[0]), json=p,
headers=headers)
print(" GET - %s" % resource_uri(res=keys[0]))
r = requests.get(resource_uri(res=keys[0]))
print(r.text)