Skip to content

Commit

Permalink
Olivia/update nav python content (#1770)
Browse files Browse the repository at this point in the history
* data model examples python & ts

* data models latest

* streaming functions examples

* latest

* fix nav

* latest consumption docs

* python function template param rename (#1761)

* chore: change execution model for ts node processes (#1758)

* Python data model gen (#1753)

* Python data model decorator (#1765)

* Change node type in docs (#1766)

* Surface errors from python runners (#1764)

* Python config error handling (#1768)

* use PrimitiveMap in function init (#1769)

* data models latest

* update inline to span

* blocks

* blocks

* fix creating endpoints

* remove python inline

* quickstart moose_data_model

* resolve comments

---------

Co-authored-by: George Leung <[email protected]>
Co-authored-by: Nicolas Joseph <[email protected]>
Co-authored-by: Jonathan Widjaja <[email protected]>
  • Loading branch information
4 people authored Sep 24, 2024
1 parent d76cbb4 commit c81da8d
Show file tree
Hide file tree
Showing 28 changed files with 1,072 additions and 428 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion apps/framework-docs/src/components/language-wrappers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const Python: React.FC<LanguageProps> = ({
{children}
</NodeType>
);
//return language === "python" ? <>{children}</> : null;
};

export const LanguageSwitch: React.FC<{
Expand Down
55 changes: 36 additions & 19 deletions apps/framework-docs/src/pages/consumption-apis.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
import { Callout } from "../components";
import { Tabs } from "nextra/components";
import { Callout, TypeScript, Python, LanguageSwitcher } from "../components";
import { Tabs, FileTree } from "nextra/components";

# Introduction to Consumption APIs
<LanguageSwitcher />

Moose **Consumption APIs** offer a powerful and flexible way to create custom API endpoints, allowing your applications to access and retrieve data from your OLAP database. These APIs act as the final layer in your Moose application, dynamically generating and executing SQL queries based on parameters received from incoming requests from your data consumers.
Consumption APIs offer a powerful and flexible way to create custom API endpoints, allowing your applications to access and retrieve data from your OLAP database. These APIs act as the final layer in your Moose application, dynamically generating and executing SQL queries based on parameters received from incoming requests from your data consumers.

## Core Concepts

### File and Folder Conventions

Consumption APIs are defined as individual files within the `/apis` folder of your Moose application. These files, written in TypeScript (`.ts`) or Python (`.py`), are automatically mapped to API endpoints based on their filenames.
Consumption APIs are defined as individual <TypeScript NodeType="span">`.ts`</TypeScript><Python NodeType="span">`.py`</Python> files within the `/apis` folder of your Moose application. These files are automatically mapped to API endpoints based on their filenames.
<TypeScript>
<FileTree>
<FileTree.Folder name="app" open>
<FileTree.Folder name="datamodels"/>
<FileTree.Folder name="functions" />
<FileTree.Folder name="blocks" />
<FileTree.Folder name="apis" open>
<FileTree.File name="myMooseApi.ts" />
</FileTree.Folder>
</FileTree.Folder>
</FileTree>
</TypeScript>
<Python>

<FileTree>
<FileTree.Folder name="app" open>
<FileTree.Folder name="apis" />
<FileTree.File name="myMooseApi.py" />
</FileTree.Folder>
</FileTree>
</Python>


<Callout type="info" title="Example">
A file named `myMooseApi.ts` would correspond to the `/consumption/myMooseApi`
A file named <TypeScript NodeType="span">`myMooseApi.ts`</TypeScript><Python NodeType="span">`myMooseApi.py`</Python> would correspond to the `/consumption/myMooseApi`
endpoint.
</Callout>

Expand All @@ -25,13 +48,9 @@ Each API file contains a route handler function that processes requests to its c
3. Executing the queries against your database
4. Processing and formatting the results before sending the response

#### Language-Specific Conventions

The definition of route handler functions follows language-specific conventions:

<Tabs items={["Typescript", "Python"]}>
<Tabs.Tab>
For Typescript, you must export the function as the default export of the file:
<TypeScript>
This route handler function must be the default export of the file:
```ts filename="/apis/dailyActiveUsers.ts" copy {8}
import { ConsumptionUtil } from "@514labs/moose-lib";

Expand All @@ -54,11 +73,11 @@ sql`SELECT
);
}

````
</Tabs.Tab>
```
</TypeScript>

<Tabs.Tab>
For Python, you must define a single function named `run()` in the file.
<Python>
Consumption APIs are defined as a function named `run()` in the file:
```python filename="/apis/dailyActiveUsers.py" copy {1}
def run(client, params):
minDailyActiveUsers = int(params.get('minDailyActiveUsers', [0])[0])
Expand All @@ -79,10 +98,8 @@ def run(client, params):
}
)

````

</Tabs.Tab>
</Tabs>
```
</Python>

#### Query Parameters and Dynamic Data Retrieval

Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,55 @@
import { Callout } from "../../components";
import { Callout, LanguageSwitcher, TypeScript, Python } from "../../components";
import { FileTree } from "nextra/components";

# Initialize Your API Endpoint

<LanguageSwitcher />

## via the CLI

To create a new Consumption API endpoint, leverage the `moose-cli consumption init` command. This will generate a `.ts` or `.py` file in the `/apis` directory.
To create a new Consumption API endpoint, leverage the `moose-cli consumption init` command.

<TypeScript>
```bash filename="Terminal" copy
npx moose-cli consumption init <YOUR_API_ROUTE>
```
</TypeScript>

<Python>
```bash filename="Terminal" copy
moose-cli consumption init <YOUR_API_ROUTE>
```
</Python>

- `<YOUR_API_ROUTE>`: The name of the route you want to create.

This command will generate a <TypeScript NodeType="span">`.ts`</TypeScript><Python NodeType="span">`.py`</Python> file in the `/apis` directory.

<TypeScript>
<FileTree>
<FileTree.Folder name="app" open>
<FileTree.Folder name="apis" open>
<FileTree.File name="YOUR_API_ROUTE.ts" />
</FileTree.Folder>
</FileTree.Folder>
</FileTree>
</TypeScript>
<Python>
<FileTree>
<FileTree.Folder name="app" open>
<FileTree.Folder name="apis" open>
<FileTree.File name="YOUR_API_ROUTE.py" />
</FileTree.Folder>
</FileTree.Folder>
</FileTree>
</Python>

For detailed information about the moose block init command, you can run the following in your terminal:

```bash copy
```bash filename="Terminal" copy
npx moose-cli consumption init --help
```

## Manually

You can alternatively manually create a new `.ts` or `.py` file inside the `/apis` folder of your project.
You can alternatively manually create a new <TypeScript NodeType="span">`.ts`</TypeScript><Python NodeType="span">`.py`</Python> file inside the `/apis` folder of your project.
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,4 @@ Once your route handler is defined, you can test it by making a request to your
https://localhost:4000/consumption/dailyActiveUsers?limit=10&minDailyActiveUsers=5
```

This example passes limit and minDailyActiveUsers as query parameters. Be sure that the endpoint URL matches the filename of your API handler, as Moose maps endpoints based on the filenames in the /apis folder.
This example passes `limit` and `minDailyActiveUsers` as query parameters. Be sure that the endpoint URL matches the filename of your API handler, as Moose maps endpoints based on the filenames in the `/apis` folder.
Loading

0 comments on commit c81da8d

Please sign in to comment.