Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Jan 22, 2023
2 parents 16971d4 + 30054ba commit de62760
Show file tree
Hide file tree
Showing 225 changed files with 11,887 additions and 162 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ Example functions to show off what you can achieve with Open Runtimes.
| Generate Giphy GIF | [](/dart/generate_giphy_gif) | [](/deno/generate-giphy-gif) | [](/node/generate_giphy_gif) | | [](/python/generate-giphy-gif) | | |
| Get COVID-19 Stats | | | | | | | [](/swift/get-covid-stats) |
| Compresss Imagse | | | | | |[](/ruby/compress-image)| |
| Send a HTTP Request | | | | | | [](/ruby/send-http-request) | |
| Wipe Appwrite Bucket | | | [](/node/wipe_appwrite_bucket) | | | | |
| Detect Language with Deepgram | | | | | [](/python/deepgram-language-detection) | | |
| Summarize Audio with Deepgram | | | | | |[](/ruby/deepgram-audio-summary)| |
| Summarize Audio with Deepgram | | | | | [](/python/deepgram-audio-summary) | | |

## Contributing

All code contributions - including those of people having commit access - must go through a pull request and be approved by a core developer before being merged. This is to ensure a proper review of all the code.

All functions should have the exactly same functionality as their other language counterparts and if possible you should avoid using any third-party libraries to keep the functions simple and secure.

We truly ❤️ pull requests! If you wish to help, you can learn more about how you can contribute to this project in the [contribution guide](CONTRIBUTING.md).
We truly ❤️ pull requests! If you wish to help, you can learn more about how you can contribute to this project in the [contribution guide](https://github.com/open-runtimes/.github/blob/main/CONTRIBUTING.md).


## Security
Expand Down
82 changes: 82 additions & 0 deletions dart/compress_image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# 🌐 Compress any Image

A Dart Cloud Function that perform a lossless compression of an image. Input image is in base64 format.

_Example input 1:_

```json
{
"provider":"tinypng",
"image":"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAf0lEQVR4nO2Wuw2AMAxEbw1gpMwDDMBcGQpooDKydGVAoXCK6J7k6qyc83MCCFGP/Yz+CkDF4KHmjgowbQF0CKFrCDUiwztqxabHCL0/xwcNhoI2UdsjC8g0mQvaSs1zwkg0uQAsAEaGm9/UPCeU7eMj6loTEpf6ZOQWMxd98gAhZnS6XEZcNQAAAABJRU5ErkJggg=="
}
```

_Example output 1:_

```json
{
"success":true,
"image":"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgBAMAAACBVGfHAAAAG1BMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACUUeIgAAAACHRSTlMA8712Sxr5g97cFtUAAAA9SURBVCjPY6Aa6AADfAIcDSA8KoBTgLGVgSFCAEmAqZmBwUIBSYClzTQ4wwE52Cs6OtpR4oFFUciBerEKAP58HnyLtZsYAAAAAElFTkSuQmCC"
}
```

_Example input 2:_

```json
{
"provider": "tinypng",
"image": "wrong-image"
}
```

_Example output 2:_

```json
{
"success": false,
"message": "Payload is invalid"
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

* **TINYPNG_API_KEY** - API Key for [tinyPNG](https://tinypng.com/developers)
<!-- * **TINYURL_API_KEY** - API Key for TinyUrl -->

## 🚀 Deployment

1. Clone this repository, and enter this function folder:

```shell
git clone https://github.com/open-runtimes/examples.git && cd examples
cd dart/compress_image
```

2. Enter this function folder and build the code:

```shell
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=lib/main.dart --rm --interactive --tty --volume $PWD:/usr/code openruntimes/dart:v2-2.16 sh /usr/local/src/build.sh
```

As a result, a `code.tar.gz` file will be generated.

3. Start the Open Runtime:

```shell
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/dart:v2-2.16 sh /usr/local/src/start.sh
```

Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Dart runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/dart-2.16).

4. Execute function:

```shell
curl http:https://localhost:3000/ -d '{"variables":{"TINYPNG_API_KEY":"YOUR_TINYPNG_API_KEY"},"payload":"{\"provider\":\"tinypng\",\"image\":\"iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAf0lEQVR4nO2Wuw2AMAxEbw1gpMwDDMBcGQpooDKydGVAoXCK6J7k6qyc83MCCFGP/Yz+CkDF4KHmjgowbQF0CKFrCDUiwztqxabHCL0/xwcNhoI2UdsjC8g0mQvaSs1zwkg0uQAsAEaGm9/UPCeU7eMj6loTEpf6ZOQWMxd98gAhZnS6XEZcNQAAAABJRU5ErkJggg==\"}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
```

## 📝 Notes

* This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions).
* This example is compatible with Dart 2.16. Other versions may work but are not guaranteed to work as they haven't been tested. Versions below Dart 2.14 will not work, because Apwrite SDK requires Dart 2.14,
79 changes: 79 additions & 0 deletions dart/compress_image/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'dart:convert';
import 'dart:typed_data';
import 'package:http/http.dart' as http;

Future<void> _tinypng(
final res, final Uint8List binaryImage, final String token) async {
final reqUrl = Uri.parse("https://api.tinify.com/shrink");
final headers = {
"Authorization": "Basic ${base64.encode(utf8.encode('api:$token'))}",
};

try {
var tinypngRes =
await http.post(reqUrl, headers: headers, body: binaryImage);

if (tinypngRes.statusCode == 200 || tinypngRes.statusCode == 201) {
final output = jsonDecode(tinypngRes.body)['output'];
final compressedImageURL = output['url'];
final compressedImageBytes =
await http.get(Uri.parse(compressedImageURL));
markSuccess(res, base64Encode(compressedImageBytes.bodyBytes));
return;
} else {
throw Exception(tinypngRes.reasonPhrase);
}
} catch (e) {
print("Failure in tinpng $e");
markFailure(res, e.toString());
return;
}
}

void markSuccess(final res, final String image) {
res.json({
'success': true,
'image': image,
});
}

void markFailure(final res, final String message) {
res.json({
'success': false,
'message': message,
});
}

Future<void> start(final request, final resonse) async {
late final payload;
late final String provider, imageBase64, apiKey;
late final Uint8List image;
try {
payload = jsonDecode(request.payload);
provider = payload['provider'].toLowerCase();
imageBase64 = payload['image'];
} catch (err) {
markFailure(resonse, "Payload is invalid");
return;
}

try {
image = base64Decode(imageBase64);
} catch (err) {
markFailure(resonse, imageBase64);
return;
}

switch (provider) {
case "tinypng":
try {
apiKey = request.variables["TINYPNG_API_KEY"];
} catch (err) {
markFailure(resonse, "TINYPNG_API_KEY is not provided");
return;
}
await _tinypng(resonse, image, apiKey);
break;
default:
}
}
9 changes: 9 additions & 0 deletions dart/compress_image/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: compress_image
description: ""
version: 1.0.0

environment:
sdk: ">=2.14.0 <3.0.0"

dependencies:
http: ^0.13.5
4 changes: 2 additions & 2 deletions dart/convert-phone-number-to-country-name/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ $ cd dart/convert_phone_number_to_country_name

2. Enter this function folder and build the code:
```
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=lib/main.dart --rm --interactive --tty --volume $PWD:/usr/code openruntimes/dart:2.16 sh /usr/local/src/build.sh
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=lib/main.dart --rm --interactive --tty --volume $PWD:/usr/code openruntimes/dart:v2-2.16 sh /usr/local/src/build.sh
```
As a result, a `code.tar.gz` file will be generated.

3. Start the Open Runtime:
```
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/dart:2.16 sh /usr/local/src/start.sh
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/dart:v2-2.16 sh /usr/local/src/start.sh
```

Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Dart runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/dart-2.16).
Expand Down
12 changes: 6 additions & 6 deletions dart/convert-phone-number-to-country-name/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Future<void> start(final req, final res) async {

// Make sure we have envirnment variables required to execute
if(
req.env['APPWRITE_FUNCTION_ENDPOINT'] == null ||
req.env['APPWRITE_FUNCTION_PROJECT_ID'] == null ||
req.env['APPWRITE_FUNCTION_API_KEY'] == null
req.variables['APPWRITE_FUNCTION_ENDPOINT'] == null ||
req.variables['APPWRITE_FUNCTION_PROJECT_ID'] == null ||
req.variables['APPWRITE_FUNCTION_API_KEY'] == null
) {
throw Exception('Please provide all required environment variables.');
}
Expand All @@ -45,9 +45,9 @@ Future<void> start(final req, final res) async {
final locale = new Locale(client);

client
.setEndpoint(req.env['APPWRITE_FUNCTION_ENDPOINT'] ?? '')
.setProject(req.env['APPWRITE_FUNCTION_PROJECT_ID'] ?? '')
.setKey(req.env['APPWRITE_FUNCTION_API_KEY'] ?? '');
.setEndpoint(req.variables['APPWRITE_FUNCTION_ENDPOINT'] ?? '')
.setProject(req.variables['APPWRITE_FUNCTION_PROJECT_ID'] ?? '')
.setKey(req.variables['APPWRITE_FUNCTION_API_KEY'] ?? '');

// Fetch and store phone number prefixes
final serverResponse = await locale.getCountriesPhones();
Expand Down
93 changes: 93 additions & 0 deletions dart/deepgram_Topic_Detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# 📝 Topic detection using Deepgram

A Dart Cloud Function that detects topics from pre-recorded audio using [Deepgram](https://developers.deepgram.com/).

_Example input 1:_

```json
{
"fileUrl": "https://static.deepgram.com/examples/interview_speech-analytics.wav"
}
```

_Example output 1:_

```json
{
"success": true,
"deepgramData": { }
}
```

_Example input 2:_

```json
{
"fileUrl": "https://static.deepgram.com/examples/interview_speech-analytics.wav"
}
```

_Example output 2:_

```json
{
"success": false,
"message": "Please provide a valid file URL."
}
```

_Example input 3:_

```json
{ }
```

_Example output 3:_

```json
{
"success": false,
"message": "Please provide a valid file URL."
}
```

If the API Key is not provided, you would get:

```json
{
"success": false,
"message": "Please provide a valid deepgram API key"
}
```

## 📝 Variables

List of variables used by this cloud function:

- **DEEPGRAM_API_KEY** - API Key for Deepgram

## 🚀 Deployment

1. Clone this repository, and enter this function folder:

```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ cd dart/deepgram_Topic_Detection
```

2. Enter this function folder and build the code:
```
docker run -e INTERNAL_RUNTIME_ENTRYPOINT=lib/main.dart --rm --interactive --tty --volume $PWD:/usr/code openruntimes/dart:v2-2.16 sh /usr/local/src/build.sh
```
As a result, a `code.tar.gz` file will be generated.

3. Start the Open Runtime:
```
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/dart:v2-2.16 sh /usr/local/src/start.sh
```

Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Dart runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/dart-2.16).

## 📝 Notes
- This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions).
- This example is compatible with Dart 2.16. Other versions may work but are not guaranteed to work as they haven't been tested. Versions below Dart 2.14 will not work, because Apwrite SDK requires Dart 2.14,
Loading

0 comments on commit de62760

Please sign in to comment.