Skip to content

Commit

Permalink
Merge pull request #36 from chuva-io/python-docs
Browse files Browse the repository at this point in the history
Add Python documentation
  • Loading branch information
nnascim authored Apr 1, 2024
2 parents 1f6e291 + 965cf78 commit 0d611d3
Show file tree
Hide file tree
Showing 9 changed files with 444 additions and 34 deletions.
57 changes: 52 additions & 5 deletions docs/cloud-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ mkdir less/functions/sum
}
```
</TabItem>

<TabItem value="py" label="Python">
```bash
touch less/functions/sum/__init__.py
```

```py title="less/functions/sum/__init__.py" showLineNumbers
def process(data):
return data['a'] + data['b']
```
</TabItem>

</Tabs>

Expand All @@ -40,7 +51,7 @@ Less offers 2 ways to call cloud functions:
1. Using the `functions` module in the Less SDK.
2. Using the Less Functions REST API.

### Send Messages Using the SDK
### Using the SDK
<Tabs>
<TabItem value="nodejs" label="Node.js">
Import `functions` from `@chuva.io/less` to call the cloud function. The function payloads are JSON objects.
Expand All @@ -52,12 +63,29 @@ Less offers 2 ways to call cloud functions:
// Result: 7
```
</TabItem>

<TabItem value="py" label="Python">
Import `functions` from `less` to call the function and process your payload in order to retrieve the response.

```py showLineNumbers
from less import functions

sum_result = functions.sum({ 'a': 3, 'b': 4 })
print('Result:', sum_result)
# Result: 7
```
</TabItem>

</Tabs>

---

Let's create a `GET /sum` route that will return the sum of 2 numbers using our `sum` cloud function.

```bash
mkdir less/apis/demo/sum
```

<Tabs groupId="programming-language" queryString="programming-language">
<TabItem value="nodejs" label="Node.js">
```bash
Expand All @@ -79,6 +107,25 @@ Let's create a `GET /sum` route that will return the sum of 2 numbers using our
```
</TabItem>
<TabItem value="py" label="Python">
```bash
touch less/apis/demo/sum/get.py
```
```py title="less/apis/demo/sum/get.py" showLineNumbers
from less import functions
def process(request, response):
a = request['query']['a']
b = request['query']['b']
response['body'] = functions.sum({
'a': int(a),
'b': int(b)
})
return response
```
</TabItem>
</Tabs>
:::info Less REST API Documentation
Expand Down Expand Up @@ -112,11 +159,11 @@ You can now deploy and call your `GET /sum` route to test your `sum` cloud funct
2. Execute your `GET /sum` request.
```bash
curl [FUNCTIONS_URL]/sum?a=1&b=2
# The sum is: 3
curl "[FUNCTIONS_URL]/sum?a=1&b=2"
# 3
```
### Send Messages Using the REST API
### Using the REST API
When using the Cloud Functions feature, Less automatically creates a Functions REST API for you. You can use the API to call cloud functions from anywhere, making it easier to integrate Less with your existing systems.
1. Deploy your function.
Expand Down Expand Up @@ -153,7 +200,7 @@ When using the Cloud Functions feature, Less automatically creates a Functions R
3. Send a message to your `sum` function using the `POST /functions/{function_name}` route.
```bash
curl -X POST -d '{"a": 1, "b": 2}' [FUNCTIONS_BASE_URL]/functions/user_created
curl -X POST -d '{"a": 1, "b": 2}' [FUNCTIONS_BASE_URL]/functions/sum
# 3
```
Expand Down
6 changes: 4 additions & 2 deletions docs/environment-variables.md → docs/configuration.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
---
sidebar_position: 12
sidebar_position: 13
---

# Environment Variables
# Configuration

## Environment Variables

In case you need access to environment variables in your code you will need to declare them in your `less.config` file in the root of your project.

Expand Down
29 changes: 28 additions & 1 deletion docs/cron-jobs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ mkdir -p less/crons/generate_daily_report
};
```
</TabItem>

<TabItem value="py" label="Python">
```bash
touch less/crons/generate_daily_report/__init__.py
```

```py title="less/crons/generate_daily_report/__init__.py" showLineNumbers
def process():
# Your code here
```
</TabItem>

</Tabs>

Expand All @@ -42,4 +53,20 @@ In the example of the `generate_daily_report` CRON the envoronment variable woul
export CRON_GENERATE_DAILY_REPORT="0 0 * * ? *"
```

Visit the [AWS Cron expressions reference](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cron-expressions.html) for CRON expression syntax documentation.
:::note Remember to add the env var to your less.config.
```bash
touch less.config
```

```yaml title="less.config"
env_vars:
- CRON_GENERATE_DAILY_REPORT
```
:::info Less Configuration Documentation
Read the [Less configuration documentation](/configuration#environment-variables) to learn more about setting up environment variables.
:::
:::tip
Visit the [AWS Cron expressions reference](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-cron-expressions.html) for CRON expression syntax documentation.
:::
110 changes: 86 additions & 24 deletions docs/key-value-store.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ All Less deployments come with a built in key-value store.
const { kvs } = require('@chuva.io/less');
```
</TabItem>

<TabItem value="py" label="Python">
Import `kvs` from `less`.
```python showLineNumbers
from less import kvs
```
</TabItem>

</Tabs>

Expand All @@ -36,6 +43,14 @@ All Less deployments come with a built in key-value store.
```
</TabItem>

<TabItem value="py" label="Python">
```python {3} showLineNumbers
from less import kvs

kvs.set('MY_KEY', 'This is my value.')
```
</TabItem>

</Tabs>

## Get Value for Key
Expand All @@ -52,6 +67,14 @@ Get a **value** for a **key**.
```
</TabItem>

<TabItem value="py" label="Python">
```python {3} showLineNumbers
from less import kvs

my_value = kvs.get('MY_KEY')
```
</TabItem>

</Tabs>


Expand All @@ -68,6 +91,14 @@ Delete a **key** and its **value**.
await kvs.delete('MY_KEY');
```
</TabItem>

<TabItem value="py" label="Python">
```python {3} showLineNumbers
from less import kvs

kvs.delete('MY_KEY')
```
</TabItem>

</Tabs>

Expand All @@ -78,25 +109,39 @@ The Key-Value Store (KVS) allows you to subscribe to a stream of real-time updat
Read the [Less Topics/Subscribers (Pub/Sub) documentation](/topics_subscribers) to learn more.
:::

### Subscribe to the `kvs_created` Topic
### Subscribe to `create` events
Create the `kvs_created` Topic.

```bash
mkdir -p less/topics/kvs_created
mkdir -p less/topics/kvs_created/log
```

<Tabs groupId="programming-language" queryString="programming-language">
<TabItem value="nodejs" label="Node.js">
```bash
touch less/topics/kvs_created/index.js
touch less/topics/kvs_created/log/index.js
```

```js title="less/topics/kvs_created/index.js"
```js title="less/topics/kvs_created/log/index.js"
exports.process = async ({ key, new_value }) => {
console.log(`New item created with key: '${key}' and value: '${new_value}'.`);
}
```
</TabItem>

<TabItem value="py" label="Python">
```bash
touch less/topics/kvs_created/log/__init__.py
```

```python title="less/topics/kvs_created/log/__init__.py"
def process(data):
key = data['key']
new_value = data['new_value']

print(f"A new item is being created with the key '{key}' and value {new_value}")
```
</TabItem>
</Tabs>

Here is an example `kvs_created` event payload:
Expand All @@ -113,19 +158,20 @@ Here is an example `kvs_created` event payload:
}
```

### Subscribe to the `kvs_updated` Topic
### Subscribe to `update` events
Create the `kvs_updated` Topic.

```bash
mkdir -p less/topics/kvs_updated
mkdir -p less/topics/kvs_updated/log
```

<Tabs groupId="programming-language" queryString="programming-language">
<TabItem value="nodejs" label="Node.js">
```bash
touch less/topics/kvs_updated/index.js
touch less/topics/kvs_updated/log/index.js
```

```js title="less/topics/kvs_updated/index.js"
```js title="less/topics/kvs_updated/log/index.js"
exports.process = async ({ key, old_value, new_value }) => {
console.log(`Item with with key: '${key}' updated.`);
console.log(`Old value: ${old_value}.`);
Expand All @@ -134,6 +180,19 @@ mkdir -p less/topics/kvs_updated
```
</TabItem>

<TabItem value="py" label="Python">
```bash
touch less/topics/kvs_updated/log/__init__.py
```

```python title="less/topics/kvs_updated/log/__init__.py"
def process(data):
key = data['key']
new_value = data['new_value']

print(f"The value for the key '{key}' was updated.\nNew value: {new_value}\nOld value: {old_value}")
```
</TabItem>
</Tabs>

Here is an example `kvs_updated` event payload:
Expand Down Expand Up @@ -162,43 +221,46 @@ Here is an example `kvs_updated` event payload:
}
```

### Subscribe to the `kvs_deleted` Topic
### Subscribe `delete` events
Create the `kvs_deleted` Topic.

```bash
mkdir -p less/topics/kvs_deleted
mkdir -p less/topics/kvs_deleted/log
```

<Tabs groupId="programming-language" queryString="programming-language">
<TabItem value="nodejs" label="Node.js">
```bash
touch less/topics/kvs_deleted/index.js
touch less/topics/kvs_deleted/log/index.js
```

```js title="less/topics/kvs_deleted/index.js"
```js title="less/topics/kvs_deleted/log/index.js"
exports.process = async ({ key, old_value }) => {
console.log(`Item with with key: '${key}' deleted.`);
console.log(`Old value: ${old_value}.`);
}
```
</TabItem>

<TabItem value="py" label="Python">
```bash
touch less/topics/kvs_deleted/log/__init__.py
```

```python title="less/topics/kvs_deleted/log/__init__.py"
def process(data):
key = data['key']
new_value = data['new_value']

print(f"The key '{key}' was deleted.")
```
</TabItem>
</Tabs>

Here is an example `kvs_deleted` event payload:
```js
{
"key": "artists",
"new_value": [
{
"first_name": "Cesária",
"last_name": "Évora",
"dob": "08/27/1941"
},
{
"first_name": "Mayra",
"last_name": "Andrade",
"dob": "02/13/1985"
}
],
"old_value": [
{
"first_name": "Cesária",
Expand Down
12 changes: 12 additions & 0 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ mkdir less/apis/demo/hello
};
```
</TabItem>

<TabItem value="py" label="Python">
```bash
touch less/apis/demo/hello/get.py
```

```py title="less/apis/demo/hello/get.py" showLineNumbers
def process(request, response):
response['body'] = 'Hello, world.'
return response
```
</TabItem>

</Tabs>

Expand Down
Loading

0 comments on commit 0d611d3

Please sign in to comment.