Last active
October 30, 2019 16:02
-
-
Save Arinono/71a917bd2ef1653bcd2e92484565b803 to your computer and use it in GitHub Desktop.
remove.bg as a story
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
http server as server | |
# Working, just a proxy over an existing service | |
when server listen method:"post" path:"/" as request | |
url = request.body.get(key:"url" default:"") | |
output = Arinono/remove-bg remove :url | |
request write content:output | |
# Compiling, real example of what the story could be | |
when server listen method:"post" path:"/debug" as request | |
url = (request.body.get(key:"url" default:"")) to string | |
if url.length() == 0 | |
request set_status code:400 | |
request write content:"Missing URL" | |
return | |
log info msg:"Image URL retrieved: {url}" | |
headers = {"X-Api-Key": app.secrets.REMOVE_BG_API_KEY} | |
body = {"image_url": url, "size": "auto"} | |
log info msg:(json stringify content:body) | |
response = (http fetch url:"https://api.remove.bg/v1.0/removebg" :headers :body method:"post") to Map[string, List[Map[string, string]]] | |
if response["error"].length() > 0 | |
log info msg:(json stringify content:response["errors"]) | |
request set_status code:500 | |
request write content:"Unable to remove background." | |
else | |
log info msg:"Uploading image to S3 bucket" | |
file = (Arinono/aws-s3 upload file:response bucket:"oms-arinono" key:"remove-bg/image-{(uuid generate)}") to Map[string, string] | |
if file["Location"].length() > 0 | |
log info msg:"Shortening final URL" | |
o_url = Arinono/rebrandly shorten url:file["Location"] | |
if o_url.length() > 0 | |
log info msg:o_url | |
request set_status code:200 | |
request write content:o_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can try this story with a simple CURL command:
Required ENV Variables
S3_BUCKET: bucket-name
S3_DIRECTORY: bucket-directory
AWS_REGION: eu-west-2
AWS_ACCESS_KEY_ID: your-aws-access-key-id
AWS_SECRET_ACCESS_KEY: your-aws-secret-access-key
REMOVE_BG_API_KEY: your-remov.bg-api-key