import time import random import jwt from space_api import API api = API('demo', 'localhost:4124') SECRET = 'my_secret' api.set_token( jwt.encode({ "password": "******" }, SECRET, algorithm='HS256').decode('utf-8')) db = api.my_sql() for i in range(10): response = db.insert('demo').doc({ "id": i + 100, "device": 2, "value": random.randint(11, 20) }).apply() if response.status == 200: print("Sent") else: print(response.error) time.sleep(2)
from space_api import API api = API('books-app', 'localhost:4124') file_store = api.file_store() print(file_store.list_files("\\")) print(file_store.create_folder("\\", "folder2")) print(file_store.list_files("\\")) print(file_store.delete_file("\\folder2")) print(file_store.list_files("\\")) print(file_store.upload_file("\\Folder\\Folder", "b.txt", "a.txt")) print(file_store.download_file("\\Folder\\Folder\\b.txt", "b.txt"))
import jwt from space_api import API api = API('books-app', 'localhost:4124') db = api.my_sql() api.set_token( jwt.encode({ "password": "******" }, 'my_secret', algorithm='HS256').decode('utf-8')) print(db.get('books').apply())
import jwt from space_api import API, COND api = API('demo', 'localhost:8081') SECRET = 'my_secret' api.set_token( jwt.encode({ "password": "******" }, SECRET, algorithm='HS256').decode('utf-8')) db = api.my_sql() service = api.service('login_service') def login(params, auth, cb): response = db.get_one('demo_users').where( COND("username", "==", params["username"])).apply() if response.status == 200: res = response.result if res["username"] == params["username"] and res["password"] == params[ "password"]: cb( 'response', { 'ack': True, 'token': jwt.encode( { "username": res["username"],
from space_api import API, COND api = API('books-app', 'localhost:4124') db = api.my_sql() b = db.begin_batch() b.add(db.insert('books').doc({"name": "MyBook", "author": "John Doe"})) b.add(db.insert('books').doc({"name": "Book_name", "author": "John Doe"})) b.add(db.insert('books').docs([{"name": "BookName"}, {"name": "BookName"}])) b.add(db.delete('books').where(COND('name', '==', 'Book_name'))) response = b.apply() print(response)
import time import random from space_api import API, COND import variables api = API(variables.app, variables.url) db = variables.db(api) db.delete('books').apply() def func(op_sel, i): if op_sel == 0: return db.insert('books').doc({ variables.id: i, "name": "MyBook" + str(i), "author": "Author" + str(i) }).apply() elif op_sel == 1: return db.update('books').set({ "author": "myself" + str(i) }).where(COND(variables.id, "==", i)).apply() elif op_sel == 2: return db.delete_one('books').where(COND(variables.id, "==", i)).apply() else: return db.get('books').apply() while True: sel = random.randint(0, 3) id = random.randint(0, 9)
from space_api import API api = API('books-app', 'localhost:4124') service = api.service('service') def echo_func(params, auth, cb): cb('response', params) def func2(params, auth, cb): cb('response', 'my_response') service.register_func('echo_func', echo_func) service.register_func('func2', func2) service.start() api.close()
# LIST FILES from space_api import API # Initialize api with the project name and url of the space cloud api = API("books-app", "localhost:4124") # Initialize file storage module file_store = api.file_store() # List all the files in a particular location ("\\" [remote]) response = file_store.list_files("\\") if response.status == 200: print(response.result) else: print(response.error) # ---------------------------------------------------------------------------------------------------- # CREATE FOLDER from space_api import API # Initialize api with the project name and url of the space cloud api = API("books-app", "localhost:4124") # Initialize file storage module file_store = api.file_store() # Create a folder ("folder" [remote]) in the location ("\\" [remote]) response = file_store.create_folder("\\", "folder") if response.status == 200:
# UPDATE from space_api import API, AND, OR, COND api = API("books-app", "localhost:4124") db = api.my_sql() # The condition to be matched condition = COND("author", "==", "author1") # Update the books response = db.update("books").where(condition).set({"name": "A book"}).apply() if response.status == 200: print("Success") else: print(response.error) # ---------------------------------------------------------------------------------------------------- # UPDATE ONE from space_api import API, AND, OR, COND api = API("books-app", "localhost:4124") db = api.my_sql() # The condition to be matched condition = COND("author", "==", "author1") # Update the books response = db.update_one("books").where(condition).set({"name": "A book"}).apply() if response.status == 200: print("Success")
import time import threading from space_api import API api = API('books-app', 'localhost:4124') pubsub = api.pubsub() def on_receive(subject, msg): print("received", subject, msg) subscription = pubsub.subscribe("/subject/", on_receive) print(subscription) def publish(): for i in range(30): msg = [1, "adf", 5.2, True, {"k": "v"}, [1, "b"]] print("publishing", msg, pubsub.publish("/subject/a/", msg)) time.sleep(2) thread = threading.Thread(target=publish) thread.start() thread.join() subscription.unsubscribe() api.close()
from space_api import API api = API("books-app", "localhost:4124") # Call a function, 'my-func' of 'my-engine' running on backend response = api.call('my-engine', 'my-func', {"msg": 'Space Cloud is awesome!'}, 1000) if response.status == 200: print(response.result) else: print(response.error)
from space_api import API import random import time import variables api = API(variables.app, variables.url) while True: response = api.call('service', 'echo_func', 'param' + str(random.randint(1,100)), timeout=5) print(response) time.sleep(0.01) api.close()
from space_api import API api = API('books-app', 'localhost:4124') for i in range(100): response = api.call('service', 'echo_func', 'param' + str(i), timeout=5000) print(response) api.close()
from space_api import API import variables api = API(variables.app, variables.url) service = api.service(variables.service) def echo_func(params, auth, cb): cb('response', params) # def func2(params, auth, cb): # cb('response', 'my_response') service.register_func('echo_func', echo_func) # service.register_func('func2', func2) service.start() api.close()
from space_api import API import time import random import variables api = API(variables.app, variables.url) db = variables.db(api) def on_snapshot(docs, kind, changed): print(docs) def on_error(error): print("ERROR:", error) while True: subscription = db.live_query('books').subscribe(on_snapshot, on_error) time.sleep(random.randint(1, 5)) subscription.unsubscribe() print("Closed") api.close()
from space_api import API api = API('books-app', 'localhost:4124') def my_func(params, auth, cb): # Function to be registered print("Params", params, "Auth", auth) # Do Something cb('response', {"ack": True}) service = api.service('service') # Create an instance of service service.register_func('my_func', my_func) # Register function service.start() # Start service # Call function of some other service response = api.call('some_service', 'some_func', {"msg": "space-service-go is awesome!"}, timeout=5) print(response) api.close()