Skip to content

Commit d09c36b

Browse files
committed
Refinement
1 parent 778429f commit d09c36b

File tree

11 files changed

+122
-106
lines changed

11 files changed

+122
-106
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
2-
"label": "Building Endpoints",
3-
"position": 2
4-
}
2+
"label": "Endpoint examples",
3+
"position": 2
4+
}

docs/docs/fastapi/building_endpoints/tables_insert.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ sidebar_position: 2
77
This recipe demonstrates how to insert data into a Databricks [Unity Catalog table](https://docs.databricks.com/aws/en/tables/) from a FastAPI application using the [Databricks SQL Connector](https://docs.databricks.com/en/dev-tools/python-sql-connector.html).
88

99
:::info
10-
In this example, we set up our API to be called using the `POST` HTTP method which is the standard choice for creating new resources in REST APIs as defined in RFC 7231.
11-
Unlike `GET`, POST is not idempotent - making the same request multiple times may create multiple resources.
10+
In this example, we set up our API to be called using the `POST` HTTP method which is the standard choice for creating new resources in REST APIs as defined in RFC 7231.
11+
Unlike `GET`, POST is not idempotent - making the same request multiple times may create multiple resources.
1212

1313
POST requests are typically used when submitting data to be processed or when creating new resources, with the request body containing the data to be added.
1414

1515
For detailed specifications, refer to [RFC 7231 Section 4.3.3](https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.3) which defines the POST method's semantics and requirements.
1616

1717
:::
18+
1819
## Code snippet
1920

2021
```python title="app.py"
@@ -102,14 +103,16 @@ async def insert_table_data(request: Request):
102103

103104
:::warning
104105

105-
The above example is shortened for brevity and skips best practice implementation.
106-
We encourage the reader to review and run the optimized FastAPI application code provided in the cookbook repository.
106+
The above example is shortened for brevity and not suitable for production use.
107+
You can find a more advanced sample in the databricks-apps-cookbook GitHub repository.
107108

108109
:::
109110

110111
### Example Usage
112+
111113
In this example, we will create the following Unity Catalog table called `my_catalog.my_schema.trips` via Databricks SQL Editor.
112-
It is assumed that the user/service principal identity has the appropriate Unity Catalog grants to make changes as required.
114+
It is assumed that the user/service principal identity has the appropriate Unity Catalog grants to make changes as required.
115+
113116
```sql
114117
CREATE OR REPLACE TABLE my_catalog.my_schema.trips (
115118
trip_id INT,
@@ -178,7 +181,9 @@ response = requests.post(
178181
)
179182
print(response.json())
180183
```
184+
181185
If the request was successful, you will get the following output in your terminal:
186+
182187
```shell
183188
{'data': [{'trip_id': 1, 'passenger_count': 1, 'trip_distance': 10.0, 'pickup_datetime': '2024-01-01 12:00:00', 'dropoff_datetime': '2024-01-01 12:10:00', 'payment_type': 'credit_card', 'fare_amount': 15.0, 'tip_amount': 2.0}, {'trip_id': 2, 'passenger_count': 1, 'trip_distance': 86.0, 'pickup_datetime': '2024-01-01 14:00:00', 'dropoff_datetime': '2024-01-01 15:13:00', 'payment_type': 'cash', 'fare_amount': 15.0, 'tip_amount': 3.0}, {'trip_id': 3, 'passenger_count': 1, 'trip_distance': 6.0, 'pickup_datetime': '2024-01-01 15:31:00', 'dropoff_datetime': '2024-01-01 15:45:00', 'payment_type': 'cash', 'fare_amount': 15.0, 'tip_amount': 3.0}], 'count': 3, 'total': 3}
184189
```
@@ -209,4 +214,4 @@ databricks-sdk
209214
databricks-sql-connector
210215
fastapi
211216
uvicorn
212-
```
217+
```

docs/docs/fastapi/building_endpoints/tables_read.mdx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ sidebar_position: 1
77
This recipe demonstrates how to query Databricks [Unity Catalog tables](https://docs.databricks.com/aws/en/tables/) from a FastAPI application using the [Databricks SQL Connector](https://docs.databricks.com/en/dev-tools/python-sql-connector.html).
88

99
:::info
10-
In this example, we set up our API to be called using the `GET` HTTP method which is the standard choice for reading data in REST APIs as defined in RFC 7231. It's designed to be safe (non-modifying) and idempotent, making it ideal for data retrieval operations.
10+
In this example, we set up our API to be called using the `GET` HTTP method which is the standard choice for reading data in REST APIs as defined in RFC 7231. It's designed to be safe (non-modifying) and idempotent, making it ideal for data retrieval operations.
1111

12-
GET requests are cacheable by default, improving performance, and their parameters are URL-encoded, making them bookmarkable and shareable.
12+
GET requests are cacheable by default, improving performance, and their parameters are URL-encoded, making them bookmarkable and shareable.
1313

1414
For detailed specifications, refer to [RFC 7231 Section 4.3.1](https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.1) which defines the GET method's semantics and requirements.
1515

1616
:::
17+
1718
## Code snippet
1819

1920
```python title="app.py"
@@ -69,20 +70,21 @@ def table(
6970

7071
:::warning
7172

72-
The above example is shortened for brevity and skips best practice implementation.
73-
We encourage the reader to review and run the optimized FastAPI application code provided in the cookbook repository.
73+
The above example is shortened for brevity and not suitable for production use.
74+
You can find a more advanced sample in the databricks-apps-cookbook GitHub repository.
7475

7576
:::
7677

7778
### Example Usage
78-
The query below retrieves data from the `system.billing.usage` [table](https://docs.databricks.com/aws/en/admin/system-tables/billing) via the above code snippet.
79-
Kindly note that we need to encode the `sql_query` contents when making the API request.
8079

80+
The query below retrieves data from the [`system.billing.usage` table](https://docs.databricks.com/aws/en/admin/system-tables/billing) via the above code snippet.
81+
Note that we need to encode the `sql_query` contents when making the API request.
8182

8283
```shell
8384
curl -X GET "https://your-app-url/api/v1/table?sql_query=SELECT%20%2A%20FROM%20system.billing.usage%20LIMIT%201" \
8485
-H "Authorization: Bearer YOUR_DATABRICKS_TOKEN" | jq
8586
```
87+
8688
```json
8789
{
8890
"results": [
@@ -176,4 +178,4 @@ databricks-sdk
176178
databricks-sql-connector
177179
fastapi
178180
uvicorn
179-
```
181+
```

docs/docs/fastapi/getting_started/connections/_category_.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/docs/fastapi/getting_started/connections/connect_from_app.mdx

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,24 @@ sidebar_position: 4
44

55
# Connect Databricks App to Databricks App
66

7-
This recipe demonstrates how to connect one Databricks App to another Databricks App.
7+
This recipe demonstrates how to connect from a Databricks App to an API hosted as another Databricks App.
88

99
:::info
1010

11-
This is another type of Machine-to-Machine (M2M) connectivity; Databricks Apps provisions a dedicated Service Principal per application slot which should be used for the purposes of security isolation, authentication, and authorization.
11+
This is another type of Machine-to-Machine (M2M) connectivity. As each Databricks App is assigned a dedicated [service principal](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m) upon creation, you should use this service principal's credentials when interacting with any Databricks service, including other Databricks Apps.
1212

1313
:::
1414

15-
1615
## Code snippet
1716

18-
Under the Databricks Apps runtime, environment variables for `DATABRICKS_HOST`, `DATABRICKS_CLIENT_ID`, and `DATABRICKS_CLIENT_SECRET` are present and associated with the App service principal.
17+
In the Databricks Apps environment, environment variables for `DATABRICKS_HOST`, `DATABRICKS_CLIENT_ID`, and `DATABRICKS_CLIENT_SECRET` are are automatically set based on the app's workspace and assigned service principal. Therefore, you do not need to explicitly specify these values in the following code:
1918

2019
```python
2120
from databricks.sdk import WorkspaceClient
2221
import requests
2322

24-
# DATABRICKS_HOST, DATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRET implicitly present under env
25-
wc = WorkspaceClient()
23+
# DATABRICKS_HOST, DATABRICKS_CLIENT_ID, DATABRICKS_CLIENT_SECRET available in environemnt
24+
wc = WorkspaceClient()
2625
headers = wc.config.authenticate() # Contains Bearer Token
2726

2827
response = requests.get(
@@ -42,8 +41,7 @@ For Databricks App connectivity and authentication, the connecting App [service
4241
- [Databricks SDK for Python](https://pypi.org/project/databricks-sdk/) - `databricks-sdk`
4342
- [Requests](https://pypi.org/project/requests/) - `requests`
4443

45-
4644
```python title="requirements.txt"
4745
databricks-sdk
4846
requests
49-
```
47+
```

docs/docs/fastapi/getting_started/connections/connect_from_external.mdx

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
sidebar_position: 3
33
---
44

5-
# Connect External App to Databricks App
5+
# Connect external app to Databricks App
66

7-
This recipe demonstrates how to connect your external application to a deployed Databricks App.
7+
This recipe demonstrates how to connect from your externally hosted application to an API deployed as a Databricks App.
88

99
:::info
1010

11-
This connection methodology is regarded as Machine-to-Machine (M2M) and customers are generally advised to use a Databricks [Service Principal](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m) as per security best practices.
11+
This connectivity option is called Machine-to-Machine (M2M) and requires you to set up a Databricks [service principal](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m) as per security best practices.
1212

1313
:::
1414

15-
1615
## Code snippet
1716

18-
The example below requires a valid databricks workspace host, client id, and client secret.
17+
The example below requires you to input a valid databricks workspace host URl as well as [service principal](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m) client id and client secret when instantiating the `WorkspaceClient`.
1918

20-
Alternatively, they can be provided as environment variables `DATABRICKS_HOST`, `DATABRICKS_CLIENT_ID`, and `DATABRICKS_CLIENT_SECRET` respectively without needing to be used within `WorkspaceClient()`.
19+
Alternatively, you can set up these values as environment variables `DATABRICKS_HOST`, `DATABRICKS_CLIENT_ID`, and `DATABRICKS_CLIENT_SECRET`. The `WorkspaceClient` will auomatically pick up these environemnt variables.
2120

2221
```python
2322
from databricks.sdk import WorkspaceClient
@@ -27,7 +26,7 @@ wc = WorkspaceClient(
2726
host="https://<DATABRICKS_HOST>",
2827
client_id="<DATABRICKS_CLIENT_ID>",
2928
client_secret="<DATABRICKS_CLIENT_SECRET>"
30-
)
29+
)
3130
headers = wc.config.authenticate() # Contains Bearer Token
3231

3332
response = requests.get(
@@ -47,8 +46,7 @@ For external app connectivity and authentication, the connecting [service princi
4746
- [Databricks SDK for Python](https://pypi.org/project/databricks-sdk/) - `databricks-sdk`
4847
- [Requests](https://pypi.org/project/requests/) - `requests`
4948

50-
5149
```python title="requirements.txt"
5250
databricks-sdk
5351
requests
54-
```
52+
```

docs/docs/fastapi/getting_started/connections/connect_from_local.mdx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@
22
sidebar_position: 2
33
---
44

5-
# Connect Local Machine to Databricks App
5+
# Connect local machine to Databricks App
66

7-
This recipe demonstrates how to connect from your local machine to a deployed Databricks App.
7+
This recipe demonstrates how to connect from your local machine to an API deployed as a Databricks App.
88

99
:::warning
1010

11-
The FastAPI application running on http://127.0.0.1:8000 does not require a [valid bearer token](https://docs.databricks.com/aws/en/dev-tools/auth/#databricks-authorization-methods) but only when deployed on Databricks Apps.
12-
This token is required for accessing Databricks Apps via the secured HTTPS URL with `/api` endpoints.
11+
While the FastAPI application running locally on http://127.0.0.1:8000 does not require a [valid bearer token](https://docs.databricks.com/aws/en/dev-tools/auth/#databricks-authorization-methods), this token is required for accessing Databricks Apps via the secured HTTPS URL with `/api` endpoints.
1312

1413
:::
1514

1615
## Generate OAuth2 Bearer token
16+
1717
The easiest method to generate a valid token is to use the [Databricks CLI](https://docs.databricks.com/aws/en/dev-tools/cli/tutorial) which allows you to authenticate with the workspace where the Databricks App is hosted.
1818

19+
You can log in to a workspace via the following CLI command:
1920

20-
Users can log into a workspace via the CLI command:
2121
```bash
2222
databricks auth login --host https://<databricks-workspace-url> --profile my-env
2323
```
2424

2525
Upon logging in successfully via the browser, you will see that your profile has been saved. This is helpful if you need to test your app across multiple environments.
26+
2627
```bash
2728
Profile my-env was successfully saved
2829
```
2930

30-
A bearer token can then be generated with a limited TTL; temporarily store the `access_token` details for use later on.
31+
A bearer token can then be generated with a limited time-to-live. Make sure to temporarily store the `access_token` details for use later on.
3132

3233
```bash
3334
databricks auth token --profile my-env
@@ -61,7 +62,7 @@ response = requests.get(
6162
print(response.json())
6263
```
6364

64-
If you would like to avoid storing the token in your code, you can leverage the profile you have created using the Databricks CLI through the Databricks Python SDK directly.
65+
If you would like to avoid storing the token in your code, you can leverage the profile you have created using the Databricks CLI through the [Databricks SDK for Python](https://databricks-sdk-py.readthedocs.io/en/latest/) directly.
6566

6667
```python
6768
from databricks.sdk.core import Config
@@ -88,8 +89,7 @@ For local connectivity and authentication, the connecting user(s)/group(s) needs
8889
- [Databricks SDK for Python](https://pypi.org/project/databricks-sdk/) - `databricks-sdk`
8990
- [Requests](https://pypi.org/project/requests/) - `requests`
9091

91-
9292
```python title="requirements.txt"
9393
databricks-sdk
9494
requests
95-
```
95+
```

docs/docs/fastapi/getting_started/connections/connect_summary.mdx

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
sidebar_position: 3
3+
---
4+
5+
# Connect to a FastAPI app
6+
7+
There are several options to consider when connecting to a deployed Databricks App.
8+
9+
| Option | Use-Case |
10+
| ------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
11+
| [Local Machine to Databricks App](/docs/fastapi/getting_started/connections/connect_from_local) | Development & testing, exploratory work |
12+
| [External App to Databricks App](/docs/fastapi/getting_started/connections/connect_from_external) | Consume Databricks services from an external application via an API hosted in Databricks Apps |
13+
| [Databricks App to Databricks App](/docs/fastapi/getting_started/connections/connect_from_app) | Internal consumption of Databricks services across workspaces (e.g. app federation), microservices architecture |

0 commit comments

Comments
 (0)