gicait / geoserver-rest

Python library for management of geospatial data in GeoServer.
https://geoserver-rest.readthedocs.io
MIT License
191 stars 77 forks source link

Support for uploading SLD files from an io.BytesIO object instead of just a path-like string #116

Closed iboates closed 6 months ago

iboates commented 1 year ago

I have a workflow in which i programmatically generate a layer's SLD from the layer data at the same moment that I upload the layer. To keep things fast, neat & tidy I write the contents of my SLD to an io.BytesIO (in-memory file) rather than writing it to disk.

When I call Geoserver.Geoserver.upload_style, it explicitly expects a path-like string and tried to open said path (source). I cannot figure out a way to "trick" it into accepting my in-memory file, since the in-memory file is effectively an analog for what is returned from open().

I propose modifying this function to accept either a path-like string or an io.BytesIO object. With a very quick hack, I verified that the function will still work if you pass an io.BytesIO object by just skipping the file read step:

            if r.status_code == 201:
                # with open(path, "rb") as f:
                #     r_sld = requests.put(
                #         url + "/" + name,
                #         data=f.read(),
                #         auth=(self.username, self.password),
                #         headers=header_sld,
                #     )
                r_sld = requests.put(
                    url + "/" + name,
                    data=path.read(),
                    auth=(self.username, self.password),
                    headers=header_sld,
                )

Thoughts on this? I guess we can check the object type at runtime and then make a call to open() only if it is a string.

I can make PR for this if you agree.

iamtekson commented 1 year ago

Are you sure that it will work like this without saving it the disk? If that's the case, please send the PR. I will also recheck the feature and merge it, if it work.