Skip to content

Commit

Permalink
Merge branch 'main' into send_email_with_mailgun_deno
Browse files Browse the repository at this point in the history
  • Loading branch information
PineappleIOnic authored Mar 16, 2022
2 parents 4487bac + 520f254 commit 5387947
Show file tree
Hide file tree
Showing 47 changed files with 1,819 additions and 7 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ Example functions to show off what you can achieve with Open Runtimes.
| Example | Dart | Deno | Node | PHP | Python | Ruby | Swift |
|-----------------------------|-----------------------------|------|------|-----|--------|------|-------|
| Send email with Mailgun | [](/dart/send_email_with_mailgun) | [](/deno/send_email_with_mailgun) | | | | | |
| Convert phone number to country name | [](/dart/convert-phone-number-to-country-name) | [](/deno/convert-phone-number-to-country-name) | [](/node/convert-phone-number-to-country-name) | [](/php/convert-phone-number-to-country-name) | [](/python/convert-phone-number-to-country-name) | [](/ruby/convert-phone-number-to-country-name) | [](/swift/convert-phone-number-to-country-name) |
| Send SMS with Twilio | | | | | | [](/ruby/send-sms-with-twilio) | |
| Translate text with Google | | | | | | [](/ruby/translate-text-with-google) | |
| Object Detection | | | | | [](/python/object-detection) | | |
| Generate Giphy GIF | | | | | [](/python/generate-giphy-gif) | | |
| Object Detection |[](/dart/object_detection) | | | | [](/python/object-detection) | | |
| Generate Giphy GIF | [](/dart/generate_giphy_gif) | | | | [](/python/generate-giphy-gif) | | |
| Get COVID-19 Stats | | | | | | | [](/swift/get-covid-stats) |

## Contributing

Expand Down
60 changes: 60 additions & 0 deletions dart/convert-phone-number-to-country-name/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# 📱 Validate phone number and get it's country information

A Dart Cloud Function that figures out country in which a phone number is registered.

_Example input:_

```json
{
"phoneNumber": "+421957215740"
}
```

> Function can also accept phone numbers with spaces, for instance `+421 957 215 740`.

_Example output:_


```json
{
"phoneNumber": "+421957215740",
"phonePrefix": "+421",
"countryCode": "SK",
"countryName": "Slovakia"
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

- **APPWRITE_FUNCTION_ENDPOINT** - Endpoint of your Appwrite server
- **APPWRITE_FUNCTION_API_KEY** - Appwrite API Key
- **APPWRITE_FUNCTION_PROJECT_ID** - Appwrite project ID. If running on Appwrite, this variable is provided automatically.

## 🚀 Deployment

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

```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ 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
```
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
```

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,
71 changes: 71 additions & 0 deletions dart/convert-phone-number-to-country-name/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import 'dart:convert';
import 'dart:io';
import 'package:dart_appwrite/dart_appwrite.dart';

/*
Globally scoped cache used by function. Example value:
[
{
"code": "+1",
"countryCode": "US",
"countryName": "United States"
}
]
*/
var phonePrefixList = null;

Future<void> start(final req, final res) async {
// Input validation
var phoneNumber = "";
try {
final payload = jsonDecode(req.payload);
phoneNumber = payload['phoneNumber'].replaceAll(' ', '');
} catch(err) {
print(err);
throw Exception('Payload is invalid.');
}

if(phoneNumber == null || !phoneNumber.startsWith('+')) {
throw Exception('Invalid phone number.');
}

// 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
) {
throw Exception('Please provide all required environment variables.');
}

// If we don't have cached list of phone number prefixes (first execution only)
if(phonePrefixList == null) {
// Init Appwrite SDK
final client = new Client();
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'] ?? '');

// Fetch and store phone number prefixes
final serverResponse = await locale.getCountriesPhones();
phonePrefixList = serverResponse.phones;
}

// Get phone prefix
final phonePrefix = phonePrefixList.firstWhere((prefix) => phoneNumber.startsWith(prefix.code));

if(phonePrefix == null) {
throw Exception('Invalid phone number.');
}

// Return phone number prefix
res.json({
'phoneNumber': phoneNumber,
'phonePrefix': phonePrefix.code,
'countryCode': phonePrefix.countryCode,
'countryName': phonePrefix.countryName
});
}
9 changes: 9 additions & 0 deletions dart/convert-phone-number-to-country-name/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: convert_phone_number_to_country_name
description: ""
version: 1.0.0

environment:
sdk: '>=2.14.0 <3.0.0'

dependencies:
dart_appwrite: ^4.0.0
53 changes: 53 additions & 0 deletions dart/generate_giphy_gif/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 🖼️ Get Giphy GIF

A Dart Cloud Function for generating Giphy GIF from the [Giphy API](https://developers.giphy.com/docs/api#quick-start-guide).

_Example input:_

```json
{
"search": "good morning"
}
```

_Example output:_


```json
{
"search": "good morning",
"gif": "https://giphy.com/gifs/text-gif-sticker-brittdoesdesign-MS9Yq6Y718CSiDTxR5"
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

**GIPHY_API_KEY** - Your Giphy API key.

## 🚀 Deployment

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

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

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
```
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
```

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,
37 changes: 37 additions & 0 deletions dart/generate_giphy_gif/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

Future<void> start(final req, final res) async {
final APIKey = req.env["GIPHY_API_KEY"];

Map<String, dynamic> data = jsonDecode(req.payload);

if (!data.containsKey("search")) {
throw new Exception("Missing Search Query");
}

if (req.env['GIPHY_API_KEY'] == null) {
throw new Exception("Missing Giphy API Key in environment variables");
}

final search = data["search"];

// Download image into buffer
var parsedUrl = Uri.parse("https://api.giphy.com/v1/gifs/search?api_key=${APIKey}&q=${search}&limit=1");
var response = await http.get(parsedUrl);

if (response.statusCode != 200) {
throw new Exception("Failed to get GIF");
}

// Parse response
Map<String, dynamic> parsedBody = jsonDecode(response.body);

Map<String, dynamic> result = {
"search": search,
"url": parsedBody["data"][0]['url']
};

res.json(result);
}
9 changes: 9 additions & 0 deletions dart/generate_giphy_gif/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: object_detection
description: ""
version: 1.0.0

environment:
sdk: '>=2.14.0 <3.0.0'

dependencies:
http: ^0.13.4
58 changes: 58 additions & 0 deletions dart/object_detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 📷 Object Detection using Cloudmersive Vision API

A Dart Cloud Function for object detection from an image URL.

_Example input:_

```json
{
"url": "https://picsum.photos/seed/open___runtimes/1000/1000"
}
```

_Example output:_


```json
{
"url": "https://picsum.photos/seed/open___runtimes/1000/1000",
"name": "cake",
"confidence": 0.7977721691131592,
"x": 21,
"y": 5,
"width": 494,
"height": 333
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

**CLOUDMERSIVE_API_KEY** - Your Cloudmersive API key.

## 🚀 Deployment

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

```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ cd dart/object_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: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
```

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,
46 changes: 46 additions & 0 deletions dart/object_detection/lib/main.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;

Future<void> start(final req, final res) async {
final APIKey = req.env["CLOUDMERSIVE_API_KEY"];

Map<String, dynamic> data = jsonDecode(req.payload);

if (!data.containsKey("url")) {
throw new Exception("Missing url");
}

if (req.env['CLOUDMERSIVE_API_KEY'] == null) {
throw new Exception("Missing Cloudmersive API Key in environment variables");
}

final url = data["url"];

// Download image into buffer
var parsedUrl = Uri.parse(url);
var response = await http.get(parsedUrl);

if (response.statusCode != 200) {
throw new Exception("Failed to download image");
}

var imageBuffer = response.bodyBytes;

var request = new http.MultipartRequest('POST', Uri.parse("https://api.cloudmersive.com/image/recognize/detect-objects"))
..files.add(new http.MultipartFile.fromBytes('imageFile', imageBuffer))
..headers['Apikey'] = APIKey;

// Call Cloudmersive OCR API
var cloudmersiveResponse = await request.send();

// Parse response
var responseBody = await cloudmersiveResponse.stream.bytesToString();
Map<String, dynamic> parsedBody = jsonDecode(responseBody);

if (parsedBody["Objects"] == null) {
throw new Exception("No objects detected");
}

res.json(parsedBody["Objects"][0]);
}
9 changes: 9 additions & 0 deletions dart/object_detection/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: object_detection
description: ""
version: 1.0.0

environment:
sdk: '>=2.14.0 <3.0.0'

dependencies:
http: ^0.13.4
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ Your function is now listening on port `3000`, and you can execute it by sending

## 📝 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.13, 2.14 and 2.15. Other versions may work but are not guarenteed to work as they haven't been tested.
- This example is compatible with Dart 2.13, 2.14 and 2.15. Other versions may work but are not guaranteed to work as they haven't been tested.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 5387947

Please sign in to comment.