|
| 1 | +Foxx |
| 2 | +---- |
| 3 | + |
| 4 | +**Foxx** is a microservice framework which lets you define custom HTTP endpoints |
| 5 | +that extend ArangoDB's REST API. For more information, refer to `ArangoDB Manual`_. |
| 6 | + |
| 7 | +.. _ArangoDB Manual: https://docs.arangodb.com |
| 8 | + |
| 9 | +**Example:** |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from arangoasync import ArangoClient |
| 14 | + from arangoasync.auth import Auth |
| 15 | +
|
| 16 | + # Initialize the client for ArangoDB. |
| 17 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 18 | + auth = Auth(username="root", password="passwd") |
| 19 | +
|
| 20 | + # Connect to "test" database as root user. |
| 21 | + db = await client.db("test", auth=auth) |
| 22 | +
|
| 23 | + # Get the Foxx API wrapper. |
| 24 | + foxx = db.foxx |
| 25 | +
|
| 26 | + # Define the test mount point. |
| 27 | + service_mount = "/test_mount" |
| 28 | +
|
| 29 | + # List services. |
| 30 | + await foxx.services() |
| 31 | +
|
| 32 | + # Create a service using a source file. |
| 33 | + # In this case, the server must have access to the URL. |
| 34 | + service = { |
| 35 | + "source": "/tests/static/service.zip", |
| 36 | + "configuration": {}, |
| 37 | + "dependencies": {}, |
| 38 | + } |
| 39 | + await foxx.create_service( |
| 40 | + mount=service_mount, |
| 41 | + service=service, |
| 42 | + development=True, |
| 43 | + setup=True, |
| 44 | + legacy=True |
| 45 | + ) |
| 46 | +
|
| 47 | + # Update (upgrade) a service. |
| 48 | + await db.foxx.update_service( |
| 49 | + mount=service_mount, |
| 50 | + service=service, |
| 51 | + teardown=True, |
| 52 | + setup=True, |
| 53 | + legacy=False |
| 54 | + ) |
| 55 | +
|
| 56 | + # Replace (overwrite) a service. |
| 57 | + await db.foxx.replace_service( |
| 58 | + mount=service_mount, |
| 59 | + service=service, |
| 60 | + teardown=True, |
| 61 | + setup=True, |
| 62 | + legacy=True, |
| 63 | + force=False |
| 64 | + ) |
| 65 | +
|
| 66 | + # Get service details. |
| 67 | + await foxx.service(service_mount) |
| 68 | +
|
| 69 | + # Manage service configuration. |
| 70 | + await foxx.config(service_mount) |
| 71 | + await foxx.update_config(service_mount, options={}) |
| 72 | + await foxx.replace_config(service_mount, options={}) |
| 73 | +
|
| 74 | + # Manage service dependencies. |
| 75 | + await foxx.dependencies(service_mount) |
| 76 | + await foxx.update_dependencies(service_mount, options={}) |
| 77 | + await foxx.replace_dependencies(service_mount, options={}) |
| 78 | +
|
| 79 | + # Toggle development mode for a service. |
| 80 | + await foxx.enable_development(service_mount) |
| 81 | + await foxx.disable_development(service_mount) |
| 82 | +
|
| 83 | + # Other miscellaneous functions. |
| 84 | + await foxx.readme(service_mount) |
| 85 | + await foxx.swagger(service_mount) |
| 86 | + await foxx.download(service_mount) |
| 87 | + await foxx.commit() |
| 88 | + await foxx.scripts(service_mount) |
| 89 | + await foxx.run_script(service_mount, "setup", {}) |
| 90 | + await foxx.run_tests(service_mount, reporter="xunit", output_format="xml") |
| 91 | +
|
| 92 | + # Delete a service. |
| 93 | + await foxx.delete_service(service_mount) |
| 94 | +
|
| 95 | +There are other ways to create, update, and replace services, such as |
| 96 | +providing a file directly instead of a source URL. This is useful when you |
| 97 | +want to deploy a service from a local file system without needing the |
| 98 | +server to access the file directly. When using this method, you must provide |
| 99 | +the appropriate content type in the headers, such as `application/zip` for ZIP files or |
| 100 | +`multipart/form-data` for multipart uploads. The following example demonstrates how to do this: |
| 101 | + |
| 102 | +.. code-block:: python |
| 103 | +
|
| 104 | + import aiofiles |
| 105 | + import aiohttp |
| 106 | + import json |
| 107 | + from arangoasync import ArangoClient |
| 108 | + from arangoasync.auth import Auth |
| 109 | +
|
| 110 | + # Initialize the client for ArangoDB. |
| 111 | + async with ArangoClient(hosts="http://localhost:8529") as client: |
| 112 | + auth = Auth(username="root", password="passwd") |
| 113 | +
|
| 114 | + # Connect to "test" database as root user. |
| 115 | + db = await client.db("test", auth=auth) |
| 116 | +
|
| 117 | + # Get the Foxx API wrapper. |
| 118 | + foxx = db.foxx |
| 119 | +
|
| 120 | + # Define the test mount points. |
| 121 | + mount_point = "/test_mount" |
| 122 | +
|
| 123 | + # Create the service using multipart/form-data. |
| 124 | + service = aiohttp.FormData() |
| 125 | + service.add_field( |
| 126 | + "source", |
| 127 | + open("./tests/static/service.zip", "rb"), |
| 128 | + filename="service.zip", |
| 129 | + content_type="application/zip", |
| 130 | + ) |
| 131 | + service.add_field("configuration", json.dumps({})) |
| 132 | + service.add_field("dependencies", json.dumps({})) |
| 133 | + service_info = await db.foxx.create_service( |
| 134 | + mount=mount_point, service=service, headers={"content-type": "multipart/form-data"} |
| 135 | + ) |
| 136 | +
|
| 137 | + # Replace the service using raw data. |
| 138 | + async with aiofiles.open("./tests/static/service.zip", mode="rb") as f: |
| 139 | + service = await f.read() |
| 140 | + service_info = await db.foxx.replace_service( |
| 141 | + mount=mount_point, service=service, headers={"content-type": "application/zip"} |
| 142 | + ) |
| 143 | +
|
| 144 | + # Delete the service. |
| 145 | + await db.foxx.delete_service(mount_point) |
| 146 | +
|
| 147 | +See :class:`arangoasync.foxx.Foxx` for API specification. |
0 commit comments