Examples#
Pagination#
You’ll often need to work with collections of elements API provides.
Cybsi Cloud SDK provides two ways to traverse collections.
The first way is pages traversing. This approach fits for cases when you need to get page’s properties i.e. cursor. For walking by page elements just iterate through the page.
#!/usr/bin/env python3
from typing import Optional
from cybsi.cloud import Client, Config
from cybsi.cloud.iocean import CollectionCommonView
from cybsi.cloud.pagination import Page
if __name__ == "__main__":
config = Config(api_key="the cryptic string")
with Client(config) as client:
page: Optional[Page[CollectionCommonView]] = client.iocean.collections.filter()
while page:
# Page is iterable
for item in page:
# Do something with an item
pass
# Fetch next page
page = page.next_page()
The second way is elements traversing. This approach allows you to iterate through collections without working with pages. To work with collections as with iterator use chain_pages.
#!/usr/bin/env python3
from cybsi.cloud import Client, Config
from cybsi.cloud.pagination import chain_pages
if __name__ == "__main__":
config = Config(api_key="the cryptic string")
with Client(config) as client:
collection_id = "phishing"
# Retrieve collection schema, it describes all attributes
# of objects you can encounter in the collection.
schema_view = client.iocean.collections.view_schema(collection_id=collection_id)
print(schema_view.schema)
# Retrieve first page of collection objects.
start_page, _ = client.iocean.objects.filter(
collection_id=collection_id,
)
for obj in chain_pages(start_page):
# Do something with the object.
print(obj)
Limit#
You can define page limit. Backend returns the specified maximum number of elements per page. Backend overrides this value if limit is not set or value is out of bounds.
#!/usr/bin/env python3
from typing import Optional
from cybsi.cloud import Client, Config
from cybsi.cloud.auth import ResourceView
from cybsi.cloud.pagination import Page
if __name__ == "__main__":
config = Config(api_key="the cryptic string")
with Client(config) as client:
page: Optional[Page[ResourceView]] = client.auth.resources.filter(limit=3)
while page:
# Got page with maximum of 3 elements
# Page is iterable
for item in page:
# Do something with an item
pass
# Fetch next page
page = page.next_page()