Skip to content

api

APIClient #

Source code in origami/clients/api.py
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
class APIClient:
    def __init__(
        self,
        authorization_token: Optional[str] = None,
        api_base_url: str = "https://app.noteable.io/gate/api",
        headers: Optional[dict] = None,
        transport: Optional[httpx.AsyncHTTPTransport] = None,
        timeout: httpx.Timeout = httpx.Timeout(5.0),
        creator_client_type: str = "origami",
    ):
        # jwt and api_base_url saved as attributes because they're re-used when creating rtu client
        self.jwt = authorization_token or os.environ.get("NOTEABLE_TOKEN")
        if not self.jwt:
            raise ValueError(
                "Must provide authorization_token or set NOTEABLE_TOKEN environment variable"
            )
        self.api_base_url = os.environ.get("NOTEABLE_API_URL", api_base_url)
        self.headers = {"Authorization": f"Bearer {self.jwt}"}
        if headers:
            self.headers.update(headers)

        self.client = httpx.AsyncClient(
            base_url=self.api_base_url,
            headers=self.headers,
            transport=transport,
            timeout=timeout,
        )
        # creator_client_type helps log what kind of client created Resources like Files/Projects
        # or is interacting with Notebooks through RTU / Deltas. If you're not sure what to use
        # yourself, go with the default 'origami'
        if creator_client_type not in ["origami", "origamist", "planar_ally", "geas"]:
            # this list of valid creator client types is sourced from Gate's FrontendType enum
            creator_client_type = "unknown"
        self.creator_client_type = creator_client_type  # Only used when generating an RTUClient

    def add_tags_and_contextvars(self, **tags):
        """Hook for Apps to override so they can set structlog contextvars or ddtrace tags etc"""
        pass

    async def user_info(self) -> User:
        """Get email and other info for User account of this Client's JWT."""
        endpoint = "/users/me"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        user = User.model_validate(resp.json())
        self.add_tags_and_contextvars(user_id=str(user.id))
        return user

    async def share_resource(
        self, resource: Resource, resource_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
    ) -> int:
        """
        Add another User as a collaborator to a Resource.
        """
        user_lookup_endpoint = f"/{resource.value}/{resource_id}/shareable-users"
        user_lookup_params = {"q": email}
        user_lookup_resp = await self.client.get(user_lookup_endpoint, params=user_lookup_params)
        user_lookup_resp.raise_for_status()
        users = user_lookup_resp.json()["data"]

        if isinstance(level, str):
            level = AccessLevel.from_str(level)
        share_endpoint = f"/{resource.value}/{resource_id}/users"
        for item in users:
            user_id = item["id"]
            share_body = {"access_level": level.value, "user_id": user_id}
            share_resp = await self.client.put(share_endpoint, json=share_body)
            share_resp.raise_for_status()
        return len(users)

    async def unshare_resource(self, resource: Resource, resource_id: uuid.UUID, email: str) -> int:
        """
        Remove access to a Resource for a User
        """
        # Need to look this up still to go from email to user-id
        user_lookup_endpoint = f"/{resource.value}/{resource_id}/shareable-users"
        user_lookup_params = {"q": email}
        user_lookup_resp = await self.client.get(user_lookup_endpoint, params=user_lookup_params)
        user_lookup_resp.raise_for_status()
        users = user_lookup_resp.json()["data"]

        for item in users:
            user_id = item["id"]
            unshare_endpoint = f"/{resource.value}/{resource_id}/users/{user_id}"
            unshare_resp = await self.client.delete(unshare_endpoint)
            unshare_resp.raise_for_status()
        return len(users)

    async def change_resource_visibility(
        self,
        resource: Resource,
        resource_id: uuid.UUID,
        visibility: Visibility,
        visibility_default_access_level: Optional[AccessLevel] = None,
    ):
        """
        Change overall visibility of a Resource.

        visibility_default_access_level is only required when visibility is not private.
        """
        if isinstance(visibility, str):
            visibility = Visibility.from_str(visibility)

        if visibility is not Visibility.private and visibility_default_access_level is None:
            raise ValueError(
                "visibility_default_access_level must be set when visibility is not private"
            )

        patch_body = {"visibility": visibility.value}
        if isinstance(visibility_default_access_level, str):
            visibility_default_access_level = AccessLevel.from_str(
                visibility_default_access_level
            ).value

        # always set this as either None or a valid (string) value
        patch_body["visibility_default_access_level"] = visibility_default_access_level

        endpoint = f"/{resource.value}/{resource_id}"
        resp = await self.client.patch(
            endpoint,
            json=patch_body,
        )
        resp.raise_for_status()
        return resp.json()

    # Spaces are collections of Projects. Some "scoped" resources such as Secrets and Datasources
    # can also be attached to a Space and made available to all users of that Space.
    async def create_space(self, name: str, description: Optional[str] = None) -> Space:
        endpoint = "/spaces"
        resp = await self.client.post(endpoint, json={"name": name, "description": description})
        resp.raise_for_status()
        space = Space.model_validate(resp.json())
        self.add_tags_and_contextvars(space_id=str(space.id))
        return space

    async def get_space(self, space_id: uuid.UUID) -> Space:
        self.add_tags_and_contextvars(space_id=str(space_id))
        endpoint = f"/spaces/{space_id}"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        space = Space.model_validate(resp.json())
        return space

    async def delete_space(self, space_id: uuid.UUID) -> None:
        self.add_tags_and_contextvars(space_id=str(space_id))
        endpoint = f"/spaces/{space_id}"
        resp = await self.client.delete(endpoint)
        resp.raise_for_status()
        return None

    async def list_space_projects(self, space_id: uuid.UUID) -> List[Project]:
        """List all Projects in a Space."""
        self.add_tags_and_contextvars(space_id=str(space_id))
        endpoint = f"/spaces/{space_id}/projects"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        projects = [Project.model_validate(project) for project in resp.json()]
        return projects

    async def share_space(
        self, space_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
    ) -> int:
        """
        Add another user as a collaborator to a Space.
        """
        return await self.share_resource(Resource.spaces, space_id, email, level)

    async def unshare_space(self, space_id: uuid.UUID, email: str) -> int:
        """
        Remove access to a Space for a User
        """
        return await self.unshare_resource(Resource.spaces, space_id, email)

    async def change_space_visibility(
        self,
        space_id: uuid.UUID,
        visibility: Visibility,
        visibility_default_access_level: Optional[AccessLevel] = None,
    ) -> Visibility:
        """
        Change overall visibility of a Space
        """
        return await self.change_resource_visibility(
            Resource.spaces,
            space_id,
            visibility,
            visibility_default_access_level,
        )

    # Projects are collections of Files, including Notebooks. When a Kernel is launched for a
    # Notebook, all Files in the Project are volume mounted into the Kernel container at startup.
    async def create_project(
        self, space_id: uuid.UUID, name: str, description: Optional[str] = None
    ) -> Project:
        self.add_tags_and_contextvars(space_id=str(space_id))
        endpoint = "/projects"
        resp = await self.client.post(
            endpoint,
            json={
                "space_id": str(space_id),
                "name": name,
                "description": description,
                "with_empty_notebook": False,
                "creator_client_type": self.creator_client_type,
            },
        )
        resp.raise_for_status()
        project = Project.model_validate(resp.json())
        self.add_tags_and_contextvars(project_id=str(project.id))
        return project

    async def get_project(self, project_id: uuid.UUID) -> Project:
        self.add_tags_and_contextvars(project_id=str(project_id))
        endpoint = f"/projects/{project_id}"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        project = Project.model_validate(resp.json())
        return project

    async def delete_project(self, project_id: uuid.UUID) -> Project:
        self.add_tags_and_contextvars(project_id=str(project_id))
        endpoint = f"/projects/{project_id}"
        resp = await self.client.delete(endpoint)
        resp.raise_for_status()
        project = Project.model_validate(resp.json())
        return project

    async def share_project(
        self, project_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
    ) -> int:
        """
        Add another User as a collaborator to a Project.
        """
        return await self.share_resource(Resource.projects, project_id, email, level)

    async def unshare_project(self, project_id: uuid.UUID, email: str) -> int:
        """
        Remove access to a Project for a User
        """
        return await self.unshare_resource(Resource.projects, project_id, email)

    async def change_project_visibility(
        self,
        project_id: uuid.UUID,
        visibility: Visibility,
        visibility_default_access_level: Optional[AccessLevel] = None,
    ) -> Visibility:
        """
        Change overall visibility of a Project
        """
        return await self.change_resource_visibility(
            Resource.projects,
            project_id,
            visibility,
            visibility_default_access_level,
        )

    async def list_project_files(self, project_id: uuid.UUID) -> List[File]:
        """List all Files in a Project. Files do not have presigned download urls included here."""
        self.add_tags_and_contextvars(project_id=str(project_id))
        endpoint = f"/projects/{project_id}/files"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        files = [File.model_validate(file) for file in resp.json()]
        return files

    # Files are flat files (like text, csv, etc) or Notebooks.
    async def _multi_step_file_create(
        self,
        project_id: uuid.UUID,
        path: str,
        file_type: Literal["file", "notebook"],
        content: bytes,
    ) -> File:
        # Uploading files using the /v1/files endpoint is a multi-step process.
        # 1. POST /v1/files to get a presigned upload url and file id
        # 2. PUT the file content to the presigned upload url, save the etag
        # 3. POST /v1/files/{file-id}/complete-upload with upload id / key / etag
        # file_type is 'file' for all non-Notebook files, and 'notebook' for Notebooks
        # (1) Reserve File in db
        body = {
            "project_id": str(project_id),
            "path": path,
            "type": file_type,
            "file_size_bytes": len(content),
            "creator_client_type": self.creator_client_type,
        }
        resp = await self.client.post("/v1/files", json=body)
        resp.raise_for_status()

        # (1.5) parse response
        js = resp.json()
        upload_url = js["presigned_upload_url_info"]["parts"][0]["upload_url"]
        upload_id = js["presigned_upload_url_info"]["upload_id"]
        upload_key = js["presigned_upload_url_info"]["key"]
        file = File.model_validate(js)

        # (2) Upload to pre-signed url
        # TODO: remove this hack if/when we get containers in Skaffold to be able to translate
        # localhost urls to the minio pod/container
        if "LOCAL_K8S" in os.environ and bool(os.environ["LOCAL_K8S"]):
            upload_url = upload_url.replace("localhost", "minio")
        async with httpx.AsyncClient() as plain_client:
            r = await plain_client.put(upload_url, content=content)
            r.raise_for_status()

        # (3) Tell API we finished uploading (returns 204)
        etag = r.headers["etag"].strip('"')
        body = {
            "upload_id": upload_id,
            "key": upload_key,
            "parts": [{"etag": etag, "part_number": 1}],
        }
        endpoint = f"/v1/files/{file.id}/complete-upload"
        r2 = await self.client.post(endpoint, json=body)
        r2.raise_for_status()
        return file

    async def create_file(self, project_id: uuid.UUID, path: str, content: bytes) -> File:
        """Create a non-Notebook File in a Project"""
        self.add_tags_and_contextvars(project_id=str(project_id))
        file = await self._multi_step_file_create(project_id, path, "file", content)
        self.add_tags_and_contextvars(file_id=str(file.id))
        logger.info("Created new file", extra={"file_id": str(file.id)})
        return file

    async def create_notebook(
        self, project_id: uuid.UUID, path: str, notebook: Optional[Notebook] = None
    ) -> File:
        """Create a Notebook in a Project"""
        self.add_tags_and_contextvars(project_id=str(project_id))
        if notebook is None:
            notebook = Notebook()
        content = notebook.model_dump_json().encode()
        file = await self._multi_step_file_create(project_id, path, "notebook", content)
        self.add_tags_and_contextvars(file_id=str(file.id))
        logger.info("Created new notebook", extra={"file_id": str(file.id)})
        return file

    async def get_file(self, file_id: uuid.UUID) -> File:
        """Get metadata about a File, not including its content. Includes presigned download url."""
        self.add_tags_and_contextvars(file_id=str(file_id))
        endpoint = f"/v1/files/{file_id}"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        file = File.model_validate(resp.json())
        return file

    async def get_file_content(self, file_id: uuid.UUID) -> bytes:
        """Get the content of a File, including Notebooks."""
        self.add_tags_and_contextvars(file_id=str(file_id))
        file = await self.get_file(file_id)
        presigned_download_url = file.presigned_download_url
        if not presigned_download_url:
            raise ValueError(f"File {file.id} does not have a presigned download url")
        # TODO: remove this hack if/when we get containers in Skaffold to be able to translate
        # localhost urls to the minio pod/container
        if "LOCAL_K8S" in os.environ and bool(os.environ["LOCAL_K8S"]):
            presigned_download_url = presigned_download_url.replace("localhost", "minio")
        async with httpx.AsyncClient() as plain_http_client:
            resp = await plain_http_client.get(presigned_download_url)
            resp.raise_for_status()
        return resp.content

    async def get_file_versions(self, file_id: uuid.UUID) -> List[FileVersion]:
        """
        List all versions of a File. The response includes presigned urls to download the content
        of any previous version. Note when working with older versions, you do not want to establish
        an RTUClient to "catch up" past that version.
        """
        endpoint = f"/files/{file_id}/versions"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        versions = [FileVersion.model_validate(version) for version in resp.json()]
        return versions

    async def delete_file(self, file_id: uuid.UUID) -> File:
        self.add_tags_and_contextvars(file_id=str(file_id))
        endpoint = f"/v1/files/{file_id}"
        resp = await self.client.delete(endpoint)
        resp.raise_for_status()
        file = File.model_validate(resp.json())
        return file

    async def share_file(
        self, file_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
    ) -> int:
        """
        Add another User as a collaborator to a Notebook or File.
        """
        return await self.share_resource(Resource.files, file_id, email, level)

    async def unshare_file(self, file_id: uuid.UUID, email: str) -> int:
        """
        Remove access to a Notebook or File for a User
        """
        return await self.unshare_resource(Resource.files, file_id, email)

    async def change_file_visibility(
        self,
        file_id: uuid.UUID,
        visibility: Visibility,
        visibility_default_access_level: Optional[AccessLevel] = None,
    ) -> Visibility:
        """
        Change overall visibility of a Notebook or File
        """
        return await self.change_resource_visibility(
            Resource.files,
            file_id,
            visibility,
            visibility_default_access_level,
        )

    async def get_datasources_for_notebook(self, file_id: uuid.UUID) -> List[DataSource]:
        """Return a list of Datasources that can be used in SQL cells within a Notebook"""
        self.add_tags_and_contextvars(file_id=str(file_id))
        endpoint = f"/v1/datasources/by_notebook/{file_id}"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        datasources = pydantic.parse_obj_as(List[DataSource], resp.json())

        return datasources

    async def launch_kernel(
        self, file_id: uuid.UUID, kernel_name: str = "python3", hardware_size: str = "small"
    ) -> KernelSession:
        endpoint = "/v1/sessions"
        data = {
            "file_id": str(file_id),
            "kernel_config": {
                "kernel_name": kernel_name,
                "hardware_size_identifier": hardware_size,
            },
        }
        resp = await self.client.post(endpoint, json=data)
        resp.raise_for_status()
        kernel_session = KernelSession.model_validate(resp.json())
        self.add_tags_and_contextvars(kernel_session_id=str(kernel_session.id))
        logger.info(
            "Launched new kernel",
            extra={"kernel_session_id": str(kernel_session.id), "file_id": str(file_id)},
        )
        return kernel_session

    async def shutdown_kernel(self, kernel_session_id: uuid.UUID) -> None:
        endpoint = f"/sessions/{kernel_session_id}"
        resp = await self.client.delete(endpoint, timeout=60)
        resp.raise_for_status()
        logger.info("Shut down kernel", extra={"kernel_session_id": str(kernel_session_id)})

    async def get_output_collection(
        self, output_collection_id: uuid.UUID
    ) -> KernelOutputCollection:
        endpoint = f"/outputs/collection/{output_collection_id}"
        resp = await self.client.get(endpoint)
        resp.raise_for_status()
        return KernelOutputCollection.model_validate(resp.json())

    async def connect_realtime(self, file: Union[File, uuid.UUID, str]) -> "RTUClient":  # noqa
        """
        Create an RTUClient for a Notebook by file id. This will perform the following steps:
         - Check /v1/files to get the current version information and presigned download url
         - Download seed notebook and create a NotebookBuilder from it
         - Create an RTUClient, initialize the websocket connection, authenticate, and subscribe
         - Apply delts to in-memory NotebookBuilder
        """
        # Import here to avoid circular imports
        from origami.clients.rtu import RTUClient

        file_id = None

        if isinstance(file, str):
            file_id = uuid.UUID(file)
        elif isinstance(file, uuid.UUID):
            file_id = file
        elif isinstance(file, File):
            file_id = file.id
        else:
            raise ValueError(f"Must provide a `file_id` or a File, not {file}")

        self.add_tags_and_contextvars(file_id=str(file_id))

        logger.info(f"Creating RTUClient for file {file_id}")
        rtu_client = RTUClient(api_client=self, file_id=file_id)
        # .initialize() downloads the seed notebook, establishes websocket, subscribes to various
        # channels, and begins squashing deltas.
        await rtu_client.initialize()
        # This event is resolved once all deltas from the file_subscribe reply deltas_to_apply
        # payload have been applied to the RTUClient NotebookBuilder
        await rtu_client.deltas_to_apply_event.wait()
        return rtu_client

add_tags_and_contextvars(**tags) #

Hook for Apps to override so they can set structlog contextvars or ddtrace tags etc

Source code in origami/clients/api.py
def add_tags_and_contextvars(self, **tags):
    """Hook for Apps to override so they can set structlog contextvars or ddtrace tags etc"""
    pass

change_file_visibility(file_id, visibility, visibility_default_access_level=None) async #

Change overall visibility of a Notebook or File

Source code in origami/clients/api.py
async def change_file_visibility(
    self,
    file_id: uuid.UUID,
    visibility: Visibility,
    visibility_default_access_level: Optional[AccessLevel] = None,
) -> Visibility:
    """
    Change overall visibility of a Notebook or File
    """
    return await self.change_resource_visibility(
        Resource.files,
        file_id,
        visibility,
        visibility_default_access_level,
    )

change_project_visibility(project_id, visibility, visibility_default_access_level=None) async #

Change overall visibility of a Project

Source code in origami/clients/api.py
async def change_project_visibility(
    self,
    project_id: uuid.UUID,
    visibility: Visibility,
    visibility_default_access_level: Optional[AccessLevel] = None,
) -> Visibility:
    """
    Change overall visibility of a Project
    """
    return await self.change_resource_visibility(
        Resource.projects,
        project_id,
        visibility,
        visibility_default_access_level,
    )

change_resource_visibility(resource, resource_id, visibility, visibility_default_access_level=None) async #

Change overall visibility of a Resource.

visibility_default_access_level is only required when visibility is not private.

Source code in origami/clients/api.py
async def change_resource_visibility(
    self,
    resource: Resource,
    resource_id: uuid.UUID,
    visibility: Visibility,
    visibility_default_access_level: Optional[AccessLevel] = None,
):
    """
    Change overall visibility of a Resource.

    visibility_default_access_level is only required when visibility is not private.
    """
    if isinstance(visibility, str):
        visibility = Visibility.from_str(visibility)

    if visibility is not Visibility.private and visibility_default_access_level is None:
        raise ValueError(
            "visibility_default_access_level must be set when visibility is not private"
        )

    patch_body = {"visibility": visibility.value}
    if isinstance(visibility_default_access_level, str):
        visibility_default_access_level = AccessLevel.from_str(
            visibility_default_access_level
        ).value

    # always set this as either None or a valid (string) value
    patch_body["visibility_default_access_level"] = visibility_default_access_level

    endpoint = f"/{resource.value}/{resource_id}"
    resp = await self.client.patch(
        endpoint,
        json=patch_body,
    )
    resp.raise_for_status()
    return resp.json()

change_space_visibility(space_id, visibility, visibility_default_access_level=None) async #

Change overall visibility of a Space

Source code in origami/clients/api.py
async def change_space_visibility(
    self,
    space_id: uuid.UUID,
    visibility: Visibility,
    visibility_default_access_level: Optional[AccessLevel] = None,
) -> Visibility:
    """
    Change overall visibility of a Space
    """
    return await self.change_resource_visibility(
        Resource.spaces,
        space_id,
        visibility,
        visibility_default_access_level,
    )

connect_realtime(file) async #

Create an RTUClient for a Notebook by file id. This will perform the following steps: - Check /v1/files to get the current version information and presigned download url - Download seed notebook and create a NotebookBuilder from it - Create an RTUClient, initialize the websocket connection, authenticate, and subscribe - Apply delts to in-memory NotebookBuilder

Source code in origami/clients/api.py
async def connect_realtime(self, file: Union[File, uuid.UUID, str]) -> "RTUClient":  # noqa
    """
    Create an RTUClient for a Notebook by file id. This will perform the following steps:
     - Check /v1/files to get the current version information and presigned download url
     - Download seed notebook and create a NotebookBuilder from it
     - Create an RTUClient, initialize the websocket connection, authenticate, and subscribe
     - Apply delts to in-memory NotebookBuilder
    """
    # Import here to avoid circular imports
    from origami.clients.rtu import RTUClient

    file_id = None

    if isinstance(file, str):
        file_id = uuid.UUID(file)
    elif isinstance(file, uuid.UUID):
        file_id = file
    elif isinstance(file, File):
        file_id = file.id
    else:
        raise ValueError(f"Must provide a `file_id` or a File, not {file}")

    self.add_tags_and_contextvars(file_id=str(file_id))

    logger.info(f"Creating RTUClient for file {file_id}")
    rtu_client = RTUClient(api_client=self, file_id=file_id)
    # .initialize() downloads the seed notebook, establishes websocket, subscribes to various
    # channels, and begins squashing deltas.
    await rtu_client.initialize()
    # This event is resolved once all deltas from the file_subscribe reply deltas_to_apply
    # payload have been applied to the RTUClient NotebookBuilder
    await rtu_client.deltas_to_apply_event.wait()
    return rtu_client

create_file(project_id, path, content) async #

Create a non-Notebook File in a Project

Source code in origami/clients/api.py
async def create_file(self, project_id: uuid.UUID, path: str, content: bytes) -> File:
    """Create a non-Notebook File in a Project"""
    self.add_tags_and_contextvars(project_id=str(project_id))
    file = await self._multi_step_file_create(project_id, path, "file", content)
    self.add_tags_and_contextvars(file_id=str(file.id))
    logger.info("Created new file", extra={"file_id": str(file.id)})
    return file

create_notebook(project_id, path, notebook=None) async #

Create a Notebook in a Project

Source code in origami/clients/api.py
async def create_notebook(
    self, project_id: uuid.UUID, path: str, notebook: Optional[Notebook] = None
) -> File:
    """Create a Notebook in a Project"""
    self.add_tags_and_contextvars(project_id=str(project_id))
    if notebook is None:
        notebook = Notebook()
    content = notebook.model_dump_json().encode()
    file = await self._multi_step_file_create(project_id, path, "notebook", content)
    self.add_tags_and_contextvars(file_id=str(file.id))
    logger.info("Created new notebook", extra={"file_id": str(file.id)})
    return file

get_datasources_for_notebook(file_id) async #

Return a list of Datasources that can be used in SQL cells within a Notebook

Source code in origami/clients/api.py
async def get_datasources_for_notebook(self, file_id: uuid.UUID) -> List[DataSource]:
    """Return a list of Datasources that can be used in SQL cells within a Notebook"""
    self.add_tags_and_contextvars(file_id=str(file_id))
    endpoint = f"/v1/datasources/by_notebook/{file_id}"
    resp = await self.client.get(endpoint)
    resp.raise_for_status()
    datasources = pydantic.parse_obj_as(List[DataSource], resp.json())

    return datasources

get_file(file_id) async #

Get metadata about a File, not including its content. Includes presigned download url.

Source code in origami/clients/api.py
async def get_file(self, file_id: uuid.UUID) -> File:
    """Get metadata about a File, not including its content. Includes presigned download url."""
    self.add_tags_and_contextvars(file_id=str(file_id))
    endpoint = f"/v1/files/{file_id}"
    resp = await self.client.get(endpoint)
    resp.raise_for_status()
    file = File.model_validate(resp.json())
    return file

get_file_content(file_id) async #

Get the content of a File, including Notebooks.

Source code in origami/clients/api.py
async def get_file_content(self, file_id: uuid.UUID) -> bytes:
    """Get the content of a File, including Notebooks."""
    self.add_tags_and_contextvars(file_id=str(file_id))
    file = await self.get_file(file_id)
    presigned_download_url = file.presigned_download_url
    if not presigned_download_url:
        raise ValueError(f"File {file.id} does not have a presigned download url")
    # TODO: remove this hack if/when we get containers in Skaffold to be able to translate
    # localhost urls to the minio pod/container
    if "LOCAL_K8S" in os.environ and bool(os.environ["LOCAL_K8S"]):
        presigned_download_url = presigned_download_url.replace("localhost", "minio")
    async with httpx.AsyncClient() as plain_http_client:
        resp = await plain_http_client.get(presigned_download_url)
        resp.raise_for_status()
    return resp.content

get_file_versions(file_id) async #

List all versions of a File. The response includes presigned urls to download the content of any previous version. Note when working with older versions, you do not want to establish an RTUClient to "catch up" past that version.

Source code in origami/clients/api.py
async def get_file_versions(self, file_id: uuid.UUID) -> List[FileVersion]:
    """
    List all versions of a File. The response includes presigned urls to download the content
    of any previous version. Note when working with older versions, you do not want to establish
    an RTUClient to "catch up" past that version.
    """
    endpoint = f"/files/{file_id}/versions"
    resp = await self.client.get(endpoint)
    resp.raise_for_status()
    versions = [FileVersion.model_validate(version) for version in resp.json()]
    return versions

list_project_files(project_id) async #

List all Files in a Project. Files do not have presigned download urls included here.

Source code in origami/clients/api.py
async def list_project_files(self, project_id: uuid.UUID) -> List[File]:
    """List all Files in a Project. Files do not have presigned download urls included here."""
    self.add_tags_and_contextvars(project_id=str(project_id))
    endpoint = f"/projects/{project_id}/files"
    resp = await self.client.get(endpoint)
    resp.raise_for_status()
    files = [File.model_validate(file) for file in resp.json()]
    return files

list_space_projects(space_id) async #

List all Projects in a Space.

Source code in origami/clients/api.py
async def list_space_projects(self, space_id: uuid.UUID) -> List[Project]:
    """List all Projects in a Space."""
    self.add_tags_and_contextvars(space_id=str(space_id))
    endpoint = f"/spaces/{space_id}/projects"
    resp = await self.client.get(endpoint)
    resp.raise_for_status()
    projects = [Project.model_validate(project) for project in resp.json()]
    return projects

share_file(file_id, email, level) async #

Add another User as a collaborator to a Notebook or File.

Source code in origami/clients/api.py
async def share_file(
    self, file_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
) -> int:
    """
    Add another User as a collaborator to a Notebook or File.
    """
    return await self.share_resource(Resource.files, file_id, email, level)

share_project(project_id, email, level) async #

Add another User as a collaborator to a Project.

Source code in origami/clients/api.py
async def share_project(
    self, project_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
) -> int:
    """
    Add another User as a collaborator to a Project.
    """
    return await self.share_resource(Resource.projects, project_id, email, level)

share_resource(resource, resource_id, email, level) async #

Add another User as a collaborator to a Resource.

Source code in origami/clients/api.py
async def share_resource(
    self, resource: Resource, resource_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
) -> int:
    """
    Add another User as a collaborator to a Resource.
    """
    user_lookup_endpoint = f"/{resource.value}/{resource_id}/shareable-users"
    user_lookup_params = {"q": email}
    user_lookup_resp = await self.client.get(user_lookup_endpoint, params=user_lookup_params)
    user_lookup_resp.raise_for_status()
    users = user_lookup_resp.json()["data"]

    if isinstance(level, str):
        level = AccessLevel.from_str(level)
    share_endpoint = f"/{resource.value}/{resource_id}/users"
    for item in users:
        user_id = item["id"]
        share_body = {"access_level": level.value, "user_id": user_id}
        share_resp = await self.client.put(share_endpoint, json=share_body)
        share_resp.raise_for_status()
    return len(users)

share_space(space_id, email, level) async #

Add another user as a collaborator to a Space.

Source code in origami/clients/api.py
async def share_space(
    self, space_id: uuid.UUID, email: str, level: Union[str, AccessLevel]
) -> int:
    """
    Add another user as a collaborator to a Space.
    """
    return await self.share_resource(Resource.spaces, space_id, email, level)

unshare_file(file_id, email) async #

Remove access to a Notebook or File for a User

Source code in origami/clients/api.py
async def unshare_file(self, file_id: uuid.UUID, email: str) -> int:
    """
    Remove access to a Notebook or File for a User
    """
    return await self.unshare_resource(Resource.files, file_id, email)

unshare_project(project_id, email) async #

Remove access to a Project for a User

Source code in origami/clients/api.py
async def unshare_project(self, project_id: uuid.UUID, email: str) -> int:
    """
    Remove access to a Project for a User
    """
    return await self.unshare_resource(Resource.projects, project_id, email)

unshare_resource(resource, resource_id, email) async #

Remove access to a Resource for a User

Source code in origami/clients/api.py
async def unshare_resource(self, resource: Resource, resource_id: uuid.UUID, email: str) -> int:
    """
    Remove access to a Resource for a User
    """
    # Need to look this up still to go from email to user-id
    user_lookup_endpoint = f"/{resource.value}/{resource_id}/shareable-users"
    user_lookup_params = {"q": email}
    user_lookup_resp = await self.client.get(user_lookup_endpoint, params=user_lookup_params)
    user_lookup_resp.raise_for_status()
    users = user_lookup_resp.json()["data"]

    for item in users:
        user_id = item["id"]
        unshare_endpoint = f"/{resource.value}/{resource_id}/users/{user_id}"
        unshare_resp = await self.client.delete(unshare_endpoint)
        unshare_resp.raise_for_status()
    return len(users)

unshare_space(space_id, email) async #

Remove access to a Space for a User

Source code in origami/clients/api.py
async def unshare_space(self, space_id: uuid.UUID, email: str) -> int:
    """
    Remove access to a Space for a User
    """
    return await self.unshare_resource(Resource.spaces, space_id, email)

user_info() async #

Get email and other info for User account of this Client's JWT.

Source code in origami/clients/api.py
async def user_info(self) -> User:
    """Get email and other info for User account of this Client's JWT."""
    endpoint = "/users/me"
    resp = await self.client.get(endpoint)
    resp.raise_for_status()
    user = User.model_validate(resp.json())
    self.add_tags_and_contextvars(user_id=str(user.id))
    return user

Visibility #

Bases: Enum

Visibility levels associated with a specific Resource.

Private = only invited users can access Open = any member can access Public = anyone can access

Source code in origami/clients/api.py
class Visibility(enum.Enum):
    """Visibility levels associated with a specific Resource.

    Private = only invited users can access
    Open = any member can access
    Public = anyone can access
    """

    private = "private"
    open = "open"
    public = "public"

    @classmethod
    def from_str(cls, s: str):
        for vis in cls:
            if vis.name == s:
                return vis
        raise ValueError(f"Invalid visibility {s}")