Skip to content

Commit

Permalink
PR review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldiron committed Jan 23, 2023
1 parent 76004a9 commit 85548d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
22 changes: 15 additions & 7 deletions python/generate-map/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ A Python Cloud Function that generate static map image from longitude and latitu
_Example input:_
```json
{
"payload": {
"lng": 50,
"lat": 60
},
"variables":{
"MAPBOX_ACCESS_TOKEN": "YOUR_MAPBOX_ACCESS_TOKEN"
}
"lng": 12.561767,
"lat": 41.857177
}
```
_Example output:_
Expand All @@ -21,7 +16,13 @@ _Example output:_
}
```
## 📝 Environment Variables

List of environment variables used by this cloud function:

**MAPBOX_ACCESS_TOKEN** - Your Mapbox API key.

## 🚀 Deployment

1. Clone this repository, and enter this function folder:
```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
Expand All @@ -41,6 +42,13 @@ docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_E

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 Python runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/python-3.10).

4. Execute function:

```
curl http:https://localhost:3000/ -d '{"variables":{"MAPBOX_ACCESS_TOKEN":"YOUR_API_KEY"},"payload": "{\"lng\":50,\"lat\":60}"}' -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json"
```

## 📝 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 Python 3.10. Other versions may work but are not guaranteed to work as they haven't been tested.
28 changes: 15 additions & 13 deletions python/generate-map/main.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import json
from PIL import Image
import requests
from io import BytesIO
from base64 import b64encode
import base64

def error(res,message):
return res.json({"success":False,"message":message})

def main(req,res):

try:
payload = req.payload
lng = payload["lng"]
lat = payload["lat"]
access_token = req.variables["token"]
payload = json.loads('{}' if not req.payload else req.payload)
lng = payload.get('lng', None)
lat = payload.get('lat', None)
access_token = req.variables["MAPBOX_ACCESS_TOKEN"]
except Exception:
return error(res,"The payload must contain lng and lat or access token is not present")
if lng == None or lat == None:
return error(res,"The payload must contain lng and lat")
elif((type(lng)!=float or type(lat)!=float) and (type(lng)!=int or type(lat)!=int)):
return error(res,"The longitude and latitude must be a number")
elif lng >180 or lng < -180 :
elif lng >180 or lng < -180:
return error(res,"The longitude must be between -180 and 180")
elif lat >90 or lat < -90 :
elif lat >90 or lat < -90:
return error(res,"The latitude must be between -90 and 90")

url ="https://api.mapbox.com/styles/v1/mapbox/streets-v11/static/{0},{1},15,0,0/1000x1000?access_token={2}".format(lng,lat,access_token)
response = requests.get(url,timeout=30)

if response.status_code ==requests.codes.ok:
with Image.open(BytesIO(response.content)) as img:
byt = b64encode(img.tobytes())
str1 = byt.decode()
return res.json({"success":True,"image":str1})
image = base64.b64encode(response.content)
return res.json({"success":True,"image":image.decode("utf-8")})
else:
return error(res,"Nothing to retrieve please check the url")
return error(res,"Error " + str(response.status_code) + ": " + str(response.content))

0 comments on commit 85548d2

Please sign in to comment.