Skip to content

Commit

Permalink
PR review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Feb 23, 2023
1 parent 9884fc9 commit 7b3fca7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
# generateShortUrl() - Kotlin
# 🔗 Shorten a url

## Function Details
A Kotlin Cloud Function for shorten a url with two providers:

Generate a short URL.
Introduce support for bitly and tinyurl APIs.

If necessary, introduce their secret keys as function variables. URL and provider to use must be provided as payload, and shortened URL should be returned.
- [Bitly](https://bitly.com/)
- [Tinyurl](https://tinyurl.com/)

## Example input:

```json
{
"provider":"bitly",
"url":"https://google.com/",
"provider_api_key": "--the api key from the provider--"
}
```

Expand Down Expand Up @@ -44,20 +41,17 @@ Error response:
}
```

```json
{
"success":false,
"message":"Payload doesn't contain valid provider - 'random'"
}
```
## 📝 Variables

- `PROVIDER_API_KEY`: Bitly or Tinyurl API key

## 🚀 Deployment

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

```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ cd kotlin/generateShortUrl
$ cd kotlin/generate-short-url
```

2. Build the code:
Expand All @@ -69,24 +63,15 @@ docker run -e INTERNAL_RUNTIME_ENTRYPOINT=src/Index.kt --rm --interactive --tty
3. Spin-up open-runtime:

```bash
docker run -p 3001:3000 -e INTERNAL_RUNTIME_KEY=secret-key --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/kotlin:v2-1.6 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/kotlin:v2-1.6 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 Kotlin runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/kotlin-1.6).

4. Execute command:
```bash
curl -X 'POST' \
'http:https://localhost:3000/' \
-H 'Accept: application/json' \
-H 'Content-Type: application/json' \
-H 'X-Internal-Challenge: secret-key' \
-d '{
"payload": "{\"url\":\"https://facebook.com/test\",\"provider\":\"bitly\",\"provider_api_key\":\"to-be-created\"}"
}'
curl -X 'POST' 'http:https://localhost:3000/' -H 'Content-Type: application/json' -H 'X-Internal-Challenge: secret-key' -d '{"payload": "{\"url\":\"https://appwrite.io/\",\"provider\":\"bitly\"}","variables": {"PROVIDER_API_KEY":"<YOUR_API_KEY>"}}'
```
The provider_api_key should be created


## 📝 Notes

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ suspend fun main(req: RuntimeRequest, res: RuntimeResponse): RuntimeResponse {

val provider = payloadMap["provider"]?:""
val longUrl = payloadMap["url"] ?: ""
val providerApiKey = payloadMap["provider_api_key"]?:""
var providerResponse="";
val providerApiKey = req.variables["PROVIDER_API_KEY"]?:""
var providerResponse=""

try{

Expand Down

0 comments on commit 7b3fca7

Please sign in to comment.