Skip to content

Commit

Permalink
Merge pull request open-runtimes#24 from open-runtimes/generate_giphy…
Browse files Browse the repository at this point in the history
…_gif_dart

Port generate giphy gif to Dart
  • Loading branch information
christyjacob4 authored Mar 15, 2022
2 parents 2225a45 + ebca517 commit 520f254
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Example functions to show off what you can achieve with Open Runtimes.
| Send SMS with Twilio | | | | | | [](/ruby/send-sms-with-twilio) | |
| Translate text with Google | | | | | | [](/ruby/translate-text-with-google) | |
| Object Detection |[](/dart/object_detection) | | | | [](/python/object-detection) | | |
| Generate Giphy GIF | | | | | [](/python/generate-giphy-gif) | | |
| Generate Giphy GIF | [](/dart/generate_giphy_gif) | | | | [](/python/generate-giphy-gif) | | |
| Get COVID-19 Stats | | | | | | | [](/swift/get-covid-stats) |

## Contributing
Expand Down
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

0 comments on commit 520f254

Please sign in to comment.