emilhe / dash-leaflet

MIT License
204 stars 33 forks source link

Consuming GeoJSON from secure API (token-based) #213

Open jajberni opened 8 months ago

jajberni commented 8 months ago

I am planning to use the url for a GeoJSON layer to consume data generated by an API. However, the API uses token authentication. Is it possible to specify the HTTP headers for the URL request?

I guess I can just pull the data and pass it as data instead of the url, but I just wondering about that option.

prl900 commented 8 months ago

Hey @jajberni, could you add an example of what the headers would look like in your use case even if with a mocked token?

The reason is we are working on securing our WMS server and we'll eventually like to have similar functionality added to the dash-leaflet WmsTileLayer.

jajberni commented 8 months ago

Sure! Here is my code to get the geojson from the API

api_endpoint = settings.API_BASE_URL + f'/fields/{field_id}/geojson'
headers = {'Authorization': f'Bearer {access_token}'}
response = requests.get(api_endpoint, headers=headers)

if response.status_code == 200:
    data = response.json()

data contains the geojson that can be loaded as:

dl.GeoJSON(data=data)

Maybe adding an optional argument such as headers would be enough:

api_endpoint = settings.API_BASE_URL + f'/fields/{field_id}/geojson'
headers = {'Authorization': f'Bearer {access_token}'}
dl.GeoJSON(url=api_endpoint, headers=headers)
emilhe commented 8 months ago

@jajberni do you have a (publicly accessable) url that I could use for testing a potential implementation?