Btrieve 2
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
brestful.py

This is an example of restful use written in Python.

1import sys
2import struct
3import math
4import os
5import platform
6import json
7
8if (platform.system() == "Windows"):
9 sys.path.append("C:\\Program Files\\Actian\\Zen\\bin")
10else:
11 sys.path.append("/usr/local/actianzen/lib64")
12
13import btrievePython
14
15min_x = 0
16max_x = 255
17collectionName = "squaresAndSquareRoots.btr"
18
19# If the incorrect number of arguments were given.
20if (len(sys.argv) != 2):
21 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
22
23integerValue = int(sys.argv[1])
24
25# If integerValue is out of range.
26if (integerValue < min_x) or (integerValue > max_x):
27 sys.exit("Usage: " + os.path.basename(sys.argv[0]) + " uint8_value")
28
29key = sys.argv[1]
30
31
32response = json.loads(btrievePython.BRestful.POST(collectionName))
33assert(response["_statusCode"] == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), response["_statusString"]
34
35
36# For all the values of x.
37for i in range(min_x, max_x + 1):
38 iSquared = i * i
39 iSquareRoot = math.sqrt(i)
40 document = {}
41 document["x"] = i
42 document["xSquared"] = iSquared
43 document["xSquareRoot"] = iSquareRoot
44 document["text"] = "The square of %d is %d and its square root is %f." % (i, iSquared, iSquareRoot)
45 documentJson = json.dumps(document)
46
47
48 response = json.loads(btrievePython.BRestful.POST(collectionName, documentJson))
49 assert(response["_statusCode"] == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), response["_statusString"]
50
51
52query = '{ "x": ' + key + '}'
53
54
55response = json.loads(btrievePython.BRestful.GET(collectionName, query))
56assert(response["_statusCode"] == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), response["_statusString"]
57
58
59documentIdList = response["_ids"]
60
61length = len(documentIdList)
62assert(length == 1), "Unexpected length. Length should be one."
63
64documentId = documentIdList[0]
65
66
67response = json.loads(btrievePython.BRestful.GET(collectionName, documentId))
68assert(response["_statusCode"] == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), response["_statusString"]
69
70
71print ("json:", json.dumps(response))
72
73
74response = json.loads(btrievePython.BRestful.DELETE(collectionName))
75assert(response["_statusCode"] == btrievePython.Btrieve.STATUS_CODE_NO_ERROR), response["_statusString"]
76