necbaas package

Submodules

necbaas.apigw module

API Gateway module

class necbaas.apigw.Apigw(service, apiname, method, subpath=None)[source]

Bases: object

API Gateway instance. Create this instance for each API (apiname, method and subpath)

Example

api = necbaas.Apigw(service, "api1", "GET", "a/b/c")
Parameters:
  • service (Service) – Service
  • apiname (str) – API name
  • method (str) – Method (GET/POST/PUT/DELETE)
  • subpath (str) – Sub-path (optional)
service

Service – Service

apiname

str – API name

method

str – Method (GET/POST/PUT/DELETE)

subpath

str – Sub-path (optional)

execute(data=None, json=None, query=None, headers=None)[source]

Execute API Gateway. Return value is ‘Response’ object of ‘requests’ library.

Example

res = api.execute(json={"temperature": 26.3})
status = res.status()  # get status code
json = res.json()  # response body of JSON as dict
Parameters:
  • data (any) – Request body, in bytes or file-like object. This overrides ‘json’ argument. (optional)
  • json (dict) – Request body of JSON. (optional)
  • query (dict) – Query parameters. (optional)
  • headers (dict) – Headers. (optional)
Returns:

Response of ‘requests’ library.

Return type:

Response

necbaas.buckets module

Bucket manager module

class necbaas.buckets.Buckets(service, bucket_type)[source]

Bases: object

Bucket Manager

Parameters:
  • service (Service) – Service
  • bucket_type (str) – Bucket type, “object” or “file”.
service

Service – Service

bucket_type

str – Bucket type, “object” or “file”.

get(name)[source]

Query bucket.

Parameters:name (str) – Bucket name.
Returns:Bucket info
Return type:dict
query()[source]

Query buckets.

Returns:List of bucket info
Return type:list
remove(name)[source]

Remove bucket.

Parameters:name (str) – Bucket name.
Returns:Bucket info
Return type:dict
upsert(name, desc='', acl=None, content_acl=None)[source]

Create/Update bucket.

Parameters:
  • name (str) – Bucket name
  • desc (str) – Description (mandatory for update)
  • acl (dict) – Bucket ACL (mandatory for update)
  • content_acl (dict) – Content ACL (mandatory for update)
Returns:

Response JSON

Return type:

dict

necbaas.file_bucket module

File bucket module

class necbaas.file_bucket.FileBucket(service, bucket_name)[source]

Bases: object

File Bucket

Parameters:
  • service (Service) – Service
  • bucket_name (str) – Bucket name
service

Service – Service

bucket_name

str – Bucket name

create(filename, data, content_type='application/octet-stream', acl=None)[source]

Upload and create new file. To replace existing file, use ‘update()’.

Example

with open("/data/data1.dat", "rb") as f:
    bucket.upload("data1.dat", f, acl={"r": ["g:anonymous"]})
Parameters:
  • filename (str) – Filename
  • data (any) – File data in bytes or file-like object.
  • content_type (str) – Content-Type (default=application/octet-stream)
  • acl (dict) – ACL (optional)
Returns:

Response JSON

Return type:

dict

download(filename, stream=False)[source]

Download file.

Example

Example 1 without streaming:

r = bucket.download("file1.json")
binary = r.content   # get binary content
#text = r.text       # get text content
#json = r.json()     # get json content

Example2 with streaming:

with bucket.download("file2.zip", stream=True) as r:
    # Do things with the response here

For details of streaming support, see http://docs.python-requests.org/en/master/user/advanced/#streaming-requests.

Parameters:
  • filename (str) – Filename
  • stream (bool) – Stream flag (optional, default=False)
Returns:

Response (requests library)

Return type:

Response

get_metadata(filename)[source]

Get file metadata.

Parameters:filename (str) – File name
Returns:File Metadata
Return type:dict
query()[source]

Query file list.

Returns:List of file metadata JSON
Return type:list
remove(filename)[source]

Delete file

Parameters:filename (str) – Filename
Returns:Response JSON (empty JSON)
Return type:dict
update(filename, data, content_type='application/octet-stream', meta_etag=None, file_etag=None)[source]

Update and replace file body.

Parameters:
  • filename (str) – File name
  • data (any) – File data in bytes or file-like object.
  • content_type (str) – Content-Type
  • meta_etag (str) – File metadata ETag (optional)
  • file_etag (str) – File body ETag (optional)
Returns:

Response JSON

Return type:

dict

update_metadata(filename, meta, etag=None)[source]

Update file metadata

Parameters:
  • filename (str) – File name
  • meta (dict) – File metadata (JSON)
  • etag (str) – ETag of file metadata (optional)
Returns:

Updated file metadata

Return type:

dict

necbaas.group module

Group module

class necbaas.group.Group(service, group_name)[source]

Bases: object

Parameters:
  • service (Service) – Service
  • group_name (str) – Group name
service

Service – Service

group_name

str – Group name

add_members(users=None, groups=None)[source]

Add members in group.

Parameters:
  • users (list) – List of user ID to add (optional)
  • groups (list) – List of group name to add (optional)
Returns:

Group info

Return type:

dict

get()[source]

Query group.

Returns:Group info
Return type:dict
static query(service)[source]

Query groups.

Returns:List of group info
Return type:list
remove()[source]

Remove group.

Returns:Response json
Return type:dict
remove_members(users=None, groups=None)[source]

Remove members in group.

Parameters:
  • users (list) – List of user ID to remove (optional)
  • groups (list) – List of group name to remove (optional)
Returns:

Group info

Return type:

dict

upsert(users=None, groups=None, acl=None, etag=None)[source]

Create/Update group.

Parameters:
  • users (list) – List of user ID belonging to group (optional)
  • groups (list) – List of group name belonging to group (optional)
  • acl (dict) – ACL (optional)
  • etag (str) – ETag (optional)
Returns:

Response JSON

Return type:

dict

necbaas.object_bucket module

JSON Object bucket module

class necbaas.object_bucket.ObjectBucket(service, bucket_name)[source]

Bases: object

JSON Object Storage Bucket.

Parameters:
  • service (Service) – Service
  • bucket_name (str) – Bucket name
service

Service – Service

bucket_name

str – Bucket name

aggregate(pipeline, options=None)[source]

Aggregation operation

Examples

pipeline = [
    {"$lookup": { ... }},
    ...
]
options = {"allowDiskUse": True}
results = bucket.aggregate(pipeline, options)
Parameters:
  • pipeline (list) – List of aggregation pipeline stage
  • options (dist) – Aggregation option parameter (optional)
Returns:

List of aggregation results

Return type:

list

batch(requests, soft_delete=False)[source]

Batch operation

Examples

requests = [
    {"op": "insert", "data": {"name": "foo", "score": 70}},
    {"op": "insert", "data": {"name": "bar", "score": 80}}
]
results = bucket.batch(requests)
Parameters:
  • requests (list) – List of batch requests
  • soft_delete (bool) – Soft delete (optional, default=False)
Returns:

List of batch results

Return type:

list

insert(data)[source]

Insert JSON Object

Examples

acl = {"r": ["g:authenticated"], "w": ["g:authenticated"]}
result = bucket.insert({"name": "foo", "score": 70, "ACL": acl})
Parameters:data (dict) – Data (JSON)
Returns:Response JSON
Return type:dict
query(where=None, order=None, skip=0, limit=None, projection=None, delete_mark=False)[source]

Query objects in this bucket.

Examples

results = bucket.query(where={"product_name": "orange"}, order="-updatedAt", limit=100)
Parameters:
  • where (dict) – Query conditions (JSON) (optional)
  • order (str) – Sort conditions (optional)
  • skip (int) – Skip count (optional, default=0)
  • limit (int) – Limit count (optional)
  • projection (dict) – Projection (JSON) (optional)
  • delete_mark (bool) – Include soft deleted data (optional, default=False)
Returns:

List of JSON objects

Return type:

list

query_with_count(where=None, order=None, skip=0, limit=None, projection=None, delete_mark=False)[source]

Query objects in this bucket (with count query).

Examples

(results, count) = bucket.query_with_count(where={"product_name": "orange"}, order="-updatedAt", limit=100)
Parameters:
  • where (dict) – Query conditions (JSON) (optional)
  • order (str) – Sort conditions (optional)
  • skip (int) – Skip count (optional, default=0)
  • limit (int) – Limit count (optional)
  • projection (dict) – Projection (JSON) (optional)
  • delete_mark (bool) – Include soft deleted data (optional, default=False)
Returns:

Tuple of list of JSON objects and total count of query.

Return type:

(list, count)

remove(oid, soft_delete=False)[source]

Remove one JSON Object

Parameters:
  • oid (str) – Object ID
  • soft_delete (bool) – Soft delete (optional, default=False)
Returns:

Response JSON

Return type:

dict

remove_with_query(where=None, soft_delete=False)[source]

Remove multiple JSON Objects

Parameters:
  • where (dict) – Query condition (optional)
  • soft_delete (bool) – Soft delete (optional, default=False)
Returns:

Response JSON

Return type:

dict

update(oid, data, etag=None)[source]

Update JSON Object

Parameters:
  • oid (str) – ID of Object
  • data (dict) – Data (JSON)
  • etag (str) – ETag (optional)
Returns:

Response JSON

Return type:

dict

necbaas.service module

BaaS access service module

class necbaas.service.Service(param=None)[source]

Bases: object

BaaS access service class.

Examples

service = necbaas.Service({
    "baseUrl": "https://api.example.com/api",
    "tenantId": "tenant1",
    "appId": "0123456789abcdef",
    "appKey": "0123456789abcdef",
    "proxy": {
        "http": "proxy.example.com:8080",
        "https": "proxy.example.com:8080"
    }
})
Parameters:param (dict) –

Parameters, must have following dict format:

baseUrl: Base URL of BaaS API Server (ex: https://api.example.com/api) (mandatory)
tenantId: Tenant ID or Tenant Name (mandatory)
appId: App ID (mandatory)
appKey: App Key (or Master Key) (mandatory)
proxy: (optional)
    http: Http Proxy (host:port)
    https: Https Proxy (host:port)
param

dict – Service parameters, passed by constructor argument.

session_token

str – Session Token

session_token_expire

int – Session Token expire time (unix epoch seconds)

verify_server_cert

bool – Verify server cert (default: True)

logger

logging.Logger – Logger. You can change log level with setLevel()

static delete_session_token_file()[source]

Delete session token file.

execute_rest(method, path, query=None, data=None, json=None, headers=None, stream=False)[source]

Call REST API.

Note

This is low level and internal method, so you should not use this.

Parameters:
  • method (str) – HTTP method name
  • path (str) – Path. The part after ‘/1/{tenantId}’ of full path.
  • query (dict) – Query parameters in dictionary.
  • data (data) – Request body, in dict (form-encoded), bytes or file-like object. This overrides ‘json’ argument.
  • json (dict) – Request JSON in dictionary.
  • headers (dict) – headers
  • stream (bool) – Stream flag
Returns:

Response

Return type:

Response

load_session_token()[source]

Load session token from file. If session token file does not exist, session token is cleared.

save_config(path)[source]

Save configuration to file.

Parameters:path (str) – file path
save_session_token()[source]

Save session token to file.

verify_session_token()[source]

Verify session token in instance.

necbaas.user module

User module

class necbaas.user.User(service)[source]

Bases: object

Parameters:service (Service) – Service
username

str – Username

email

str – E-mail address

password

str – Password

options

dict – Options

static get(service, user_id)[source]

Get user info.

Parameters:
  • service (Service) – Service
  • user_id (str) – User ID
Returns:

User info

Return type:

dict

static login(service, username=None, email=None, password=None, params=None)[source]

Login

Example

# login with username
result = necbaas.User.login(service, username="user1", password="Passw0rD")

# login with email
result = necbaas.User.login(service, email="user1@example.com", password="Passw0rD")

# login with dict
result = necbaas.User.login(service, params={
    "username": "foo",
    "password": "Passw0rD"
})
Parameters:
  • service (Service) – Service
  • username (str) – User name (mandatory, if both email/params is not specified)
  • email (str) – E-mail (mandatory, if both username/params is not specified)
  • password (str) – Password (mandatory, if params is not specified)
  • params (dict) – dictionary. The parameter is encoded in JSON and sent to server. Usually contains username or email, and password. (optional)
Returns:

Response JSON

Return type:

dict

static logout(service)[source]

Logout

Parameters:service (Service) – Service
Returns:Response JSON in dictionary
Return type:dict
static query(service, username=None, email=None)[source]

Query user.

Parameters:
  • service (Service) – Service
  • username (str) – Username (optional)
  • email (str) – E-mail (optional)
Returns:

List of user info

Return type:

list

register()[source]

Register user. Specify username, email, password and options properties.

Example

user = necbaas.User(service)
user.username = "foo"
user.email = "foo@example.com"
user.password = "Passw0rD"
response = user.register()
Returns:Registration info (JSON)
Return type:dist
static remove(service, user_id)[source]

Remove user.

Parameters:
  • service (Service) – Service
  • user_id (str) – User ID
Returns:

Response JSON

Return type:

dict

static reset_password(service, username=None, email=None)[source]

Reset password.

Parameters:
  • service (Service) – Service
  • username (str) – Username (optional)
  • email (str) – E-mail (optional)
Returns:

Response JSON

Return type:

dict

update(user_id, etag=None)[source]

Update user.

Parameters:
  • user_id (str) – User ID
  • etag (str) – ETag (optional)
Returns:

User info (JSON)

Return type:

dist

Module contents