forked from open-runtimes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request open-runtimes#24 from open-runtimes/generate_giphy…
…_gif_dart Port generate giphy gif to Dart
- Loading branch information
Showing
4 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |