Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial Python examples #1

Merged
merged 1 commit into from
Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.pyc
__pycache__
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# rest-client-examples

Example Usage of Aerospike REST Client

This repository Contains sample code utilizing the [Aerospike Rest Client](https://www.aerospike.com/docs/client/rest/index.html) .
16 changes: 16 additions & 0 deletions python-examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Python Examples

These examples utilize the Requests library to

## Prerequisites

* Install the Aerospike Rest Client
* Install requests: `pip install requests`

## The Examples

The files contained in the `basic/` folder are standalone single file demo applications demonstrating basic usage of some of the REST Client features.

The code in `demo-app/` presents a simplified example of building a more complex application around the functionality of the REST Client.

The examples assume that the REST Client is listening on `localhost:8080` if it is running elsewhere, the code snippets will need to be modified.
41 changes: 41 additions & 0 deletions python-examples/basic/messagepack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import requests
import msgpack


def pack(obj):
'''
msgpack an object using the binary type
'''
return msgpack.packb(obj, use_bin_type=True)


def unpack(obj):
'''
Unpack object turning message pack strings into unicode
'''
return msgpack.unpackb(obj, encoding='UTF-8')

request_uri = 'http://localhost:8080/v1/kvs/test/demo/mp'

pack_request_header = {'Content-Type': "application/msgpack"}
pack_response_header = {'Accept': "application/msgpack"}

# Python 3
# For Python 27 specify 'ba' as bytearray('1234', 'UTF-8')
bins = {'ba': b'1234', 'a': {1: 2, 3: 4}}

# Store the packed content
requests.post(request_uri, pack(bins), headers=pack_request_header)

# The request without specifying msgpack return format.
# The map keys are converted to strings, and the bytte array is urlsafe base64 encoded.
print("content with Accept: application/json")
print(requests.get(request_uri).json())
# {'ttl': 2591545, 'bins': {'ba': 'MTIzNA==', 'a': {'3': 4, '1': 2}}, 'generation': 1}


response = requests.get(request_uri, headers=pack_response_header)
print("Content with Accept: application/msgpack")
print(unpack(response.content))

# {'ttl': 2591958, 'bins': {'ba': b'1234', 'a': {1: 2, 3: 4}}, 'generation': 1}
117 changes: 117 additions & 0 deletions python-examples/basic/simple-rest-usage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import requests

REST_BASE = 'http://localhost:8080/v1'
KVS_ENDPOINT = REST_BASE + '/kvs'
OPERATE_ENDPOINT = REST_BASE + '/operate'

# Components of the Key
namespace = 'test'
setname = 'users'
userkey = 'bob'

record_uri = '{base}/{ns}/{setname}/{userkey}'.format(
base=KVS_ENDPOINT, ns=namespace, setname=setname, userkey=userkey)

# The content to be stored into Aerospike.
bins = {
'name': 'Bob',
'id': 123,
'color': 'Purple',
'languages': ['Python', 'Java', 'C'],
}


# Store the record
res = requests.post(record_uri, json=bins)

# Get the record
# It is a map: {
# 'bins': {},
# 'generation': #,
# 'ttl': #
# }
response = requests.get(record_uri)
print("*** The Original Record ***")
print(response.json())

# Change the value of the 'color' bin
update_bins = {'color': 'Orange'}
requests.patch(record_uri, json=update_bins)

# Get the updated Record. Only the 'color' bin has changed
response = requests.get(record_uri)
print("*** The updated Record ***")
print(response.json())

# Replace the record with a new version
replacement_bins = {'single': 'bin'}
requests.put(record_uri, json=replacement_bins)

# Get the new Record.
response = requests.get(record_uri)
print("*** The Replaced Record ***")
print(response.json())

# Delete the record.
response = requests.delete(record_uri)

# Try to get the deleted . We will receive a 404.
response = requests.get(record_uri)

print('*** The response code for a GET on a non existent record is {} ***'.format(response.status_code))

# The response also includes a JSON error object
print("*** The Error object is: ***")
print(response.json())

# The rest client also supports more complicated operations.
# For example, suppose that we are storing information about users.
# Initially we store their name, and the length of their name.
# Our first user is 'Bob'
base_record = {'name': 'Bob', 'name_length': 3}
requests.post(record_uri, json=base_record)

# Suppose we want to append some characters to the name
# Now we want to add to the name of our user, keep the name length field accurate,
# and add a new bin containing the company for which the user works.

# We can specify the operations. As an array of JSON objects.
# These operations change the user's name to 'Bob Roberts', update the length
# Of the name accordingly, and indicate that he works for Aerospike.
operations = [
{
'operation': 'APPEND',
'opValues': {
'bin': 'name',
'value': ' Roberts'
}
},
{
'operation': 'ADD',
'opValues': {
'bin': 'name_length',
'incr': len(' Roberts')
}
},
{
'operation': 'PUT',
'opValues': {
'bin': 'company',
'value': 'Aerospike'
}
}
]

operate_record_uri = '{base}/{ns}/{setname}/{userkey}'.format(
base=OPERATE_ENDPOINT, ns=namespace, setname=setname, userkey=userkey)

# Perform the operations on the record.
requests.post(operate_record_uri, json=operations)

# Fetch the updated record.
response = requests.get(record_uri)
print("*** The Record after applying operations ***")

print(response.json())

# For a list of operations, and example usage see <Operate.md> link
5 changes: 5 additions & 0 deletions python-examples/demo-app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Demo application

## Usage

Running `python rc_users_demo.py` will demonstrate the usage of the demo app.
Empty file.
16 changes: 16 additions & 0 deletions python-examples/demo-app/asrestclient/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
'''
Simple file containing Aerospike Rest Client constants
'''

INTEGER_KEYTYPE = 'INTEGER'
BYTES_KEYTYPE = 'BYTES'
DIGEST_KEYTYPE = 'DIGEST'

CREATE_ONLY = 'CREATE_ONLY'
UPDATE_ONLY = 'UPDATE_ONLY'

OPERATION_NAME = 'operation'
OPERATION_VALUES = 'opValues'

LIST_APPEND_OP = 'LIST_APPEND'
READ_OP = 'READ'
Loading