Ejemplo n.º 1
0
 def _get(self, key, dtype, **options):
     if self.local_server:
         return self.local_server.load(key, dtype, **options)
     else:
         json = LoadEntry(key, dtype, **options)._asdict()
         # note: reading stuff from the server is always synchronous via the result call.
         res = self.session.get(self.url, json=json).result()
         # todo: better error handling.
         return deserialize(res.text)
Ejemplo n.º 2
0
 async def stream_handler(self, req):
     import sanic
     if not req.json:
         msg = f'request json is empty: {req.text}'
         print(msg)
         return sanic.response.text(msg)
     load_entry = LoadEntry(**req.json)
     print(f"streaming: {load_entry.key}")
     path = os.path.join(self.root, load_entry.key)
     return await sanic.response.file_stream(path)
Ejemplo n.º 3
0
 async def read_handler(self, req):
     import sanic
     if not req.json:
         msg = f'request json is empty: {req.text}'
         print(msg)
         return sanic.response.text(msg)
     load_entry = LoadEntry(**req.json)
     print("loading: {} type: {}".format(load_entry.key, load_entry.type))
     res = self.load(load_entry.key, load_entry.type, load_entry.start,
                     load_entry.stop)
     data = serialize(res)
     return sanic.response.text(data)
Ejemplo n.º 4
0
    def stream_download(self, path):
        buf = BytesIO()
        if self.local_server:
            for d in self.local_server.load(path, dtype="byte"):
                buf.write(d)
            buf.seek(0)
            return buf
        else:
            from requests_toolbelt.downloadutils import stream

            json = LoadEntry(path, "byte")._asdict()
            # note: reading stuff from the server is always synchronous.
            #  with (future) sessions, this is forced by the result call.
            r = self.session.get(self.stream_url, json=json, stream=True)
            assert stream.stream_response_to_file(r.result(), path=buf) is None
            buf.seek(0)
            return buf