Skip to content

expose requests.get method used in features.GeoJson #1457

@beautah

Description

@beautah

Is your feature request related to a problem? Please describe.
I have an unreliable server that constantly hangs when adding a GeoJson, since the default behavior of a website link is to simply use: requests.get(data).json() (line 529 of folium.features.py), I end up with hung scripts whenever the server doesn't respond

Describe the solution you'd like
Would be nice if the requests.get() method was exposed in the header of the features.py or GeoJson class itself so it can be mutated.

Describe alternatives you've considered
Since it appears the requests.get() method is only used once in the project, specifically in the GeoJson class, perhaps just let the class hold a MUTATOR that can overwrite the "get" process.

class GeoJson(Layer):
    ...
    ...
    GET_MUTATOR = None
    if GET_MUTATOR:
        get_geojson = GET_MUTATOR
    else:
        get_geojson = requests.get
    ...
    ...
    def process_data(self, data):
        ...
        ...
        return get_geojson(data).json()    #  <-- this is line 529 of folium.features.py

so then in my code I can do something like:

def try_get(url, max_tries=MAX_TRIES, timeout=TIMEOUT_TUPLE):
    tries = 0
    while tries < max_tries:
        try:
            results = get(url, timeout=TIMEOUT_TUPLE)
            return results
        except ConnectionError as err:
            print(f'      Trying again {tries + 1}/{max_tries}- {err}')
            tries += 1
            time.sleep(1)
    class results:
        def __init__(self):
            self.status_code = 500
        
    return results

folium.features.GeoJson.GET_MUTATOR = try_get

Additional context
This is a pattern I've seen used in other projects, but there are simpler ways too, like how the js/css is exposed etc... Really anything that exposes the requests.get() method prior to use would do the trick

Implementation
Happy to make the PR if some direction as to the style/method you'd prefer for exposing the method, my proposed code above:

GET_MUTATOR = None
    if GET_MUTATOR:
        get_geojson = GET_MUTATOR
    else:
        get_geojson = requests.get

was just off the top of my head as I was writing this, perhaps taking a style nod from your js/css overrides makes more sense, could easily just override at the time of requests' import, ala line 37 of folium.features.py

import requests
get_geojson_data = requests.get

then an override can be directly applied to get_geojson_data , lots of ways to deal with this, open to any options

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions