Getting Started

Hello World - Onna In 6 Minutes


💡 2 min read

# Requirements

Please make sure that you have all requirements installed.

This example uses Python.

Access to the Onna API requires that you have an active account.

You can sign up for an Onna account by filling out the registration form.

A workspace in your Onna account.

# Intro

In 6 minutes you can have a functioning Python program that authenticates against Onna, creates a Datasource and retrieves data from a remote location.

You can see the output of this program when you login to your Onna account.

Tip

Copy and paste the code blocks into a single file and run it, then check your Onna account.

import asyncio
import aiohttp
import json

base_url = "https://enterprise.onna.com"
container = "container"
scope = account = "account"
auth_code_url = f"{base_url}/api/{container}/{account}/@oauthgetcode?client_id=canonical&scope={account}"
workspace_name = "workspace"
username = "you@example.com"
password = "super-secret-password"
1
2
3
4
5
6
7
8
9
10
11

Note

Change the following values to match your Onna credentials:

  • container
  • scope
  • account
  • workspace_name
  • username
  • password

# Authenticate

The first step is to generate the auth token. Once you have the token, you can create the Datasource and start collecting data.

async def auth():
    async with aiohttp.ClientSession() as session:
        resp = await session.get(auth_code_url)
        if resp.status == 200:
            data = await resp.json()
            auth_code = data.get("auth_code")

        payload = {
            "grant_type": "user",
            "code": auth_code,
            "username": username,
            "password": password,
            "scopes": [scope],
            "client_id": "canonical",
        }
        headers = {"Accept": "application/json"}
        resp = await session.post(
            f"{base_url}/auth/oauth/get_auth_token",
            headers=headers,
            data=json.dumps(payload),
        )
        if resp.status == 200:
            jwt_token = await resp.text()
        return jwt_token
    return None
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

# Create A Web Crawler

Next, you can create the Datasource in a workspace and start collecting data.

async def create_crawler_ds():
    token = await auth()
    if not token:
        raise Exception
    workspace_url = (
        f"{base_url}/api/{container}/{account}/workspaces/{workspace_name}"
    )
    headers = {"Authorization": f"Bearer {token}"}
    async with aiohttp.ClientSession() as session:
        data = {
            "@id": f"{workspace_url}/onnacrawler",
            "@type": "CrawlerDatasource",
            "title": f"Onna Web Crawler",
            "urls": [
                "https://confluence.cornell.edu/"
            ],
        }
        resp = await session.post(
            workspace_url, data=json.dumps(data), headers=headers
        )
        data = await resp.json()
        if resp.status == 201:
            resp = await session.get(
                f"{data['@id']}/@sendToSpyder?force=true", headers=headers
            )
asyncio.run(create_crawler_ds())
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
Last Updated: 8/3/2020, 2:23:36 PM