Skip to content

Commit c859958

Browse files
committed
[client] Update download vector layer beta function
1 parent 2f5f0e5 commit c859958

File tree

2 files changed

+25
-39
lines changed

2 files changed

+25
-39
lines changed

src/picterra/detector_platform_client.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
class DetectorPlatformClient(BaseAPIClient):
2828
def __init__(self, **kwargs):
2929
super().__init__("public/api/v2/", **kwargs)
30-
30+
3131
def upload_raster(
3232
self,
3333
filename: str,
@@ -831,19 +831,11 @@ def download_vector_layer_to_file(self, vector_layer_id: str, filename: str):
831831
vector_layer_id: The id of the vector layer to download
832832
filename: existing file to save the vector layer in
833833
"""
834-
resp = self.sess.get(self._full_url("vector_layers/%s/" % vector_layer_id))
834+
resp = self.sess.post(self._full_url("vector_layers/%s/download/" % vector_layer_id))
835835
if not resp.ok:
836836
raise APIError(resp.text)
837-
urls = resp.json()["geojson_urls"]
838-
final_fc: FeatureCollection = {"type": "FeatureCollection", "features": []}
839-
for url in urls:
840-
with tempfile.NamedTemporaryFile("w+") as f:
841-
_download_to_file(url, f.name)
842-
fc = json.load(f)
843-
for feature in fc["features"]:
844-
final_fc["features"].append(feature)
845-
with open(filename, "w") as fp:
846-
json.dump(final_fc, fp)
837+
op = self._wait_until_operation_completes(resp.json())
838+
_download_to_file(op["results"]["download_url"], filename)
847839

848840
def list_raster_markers(
849841
self,

tests/test_client.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -383,38 +383,32 @@ def add_mock_vector_layer_responses(upload_id, raster_id, name, color):
383383
)
384384

385385

386-
def add_mock_vector_layer_download_responses(layer_id, urls_num=1):
387-
url = "vector_layers/%s/" % layer_id
388-
data = {
389-
"name": "layer%s" % layer_id,
390-
"raster_id": "raster-id",
391-
"count": 1,
392-
"color": "#aa0000",
393-
"id": layer_id,
394-
"geojson_urls": [
395-
"http://layer%s.geojson.example.com" % str(i) for i in range(urls_num)
396-
],
386+
def add_mock_vector_layer_download_responses(layer_id, num_features):
387+
url = "vector_layers/%s/download/" % layer_id
388+
data = {"operation_id": OPERATION_ID, "poll_interval": TEST_POLL_INTERVAL}
389+
_add_api_response(detector_api_url(url), verb=responses.POST, json=data)
390+
results = {
391+
"expiration": "2021-11-03T10:55:16.000000Z",
392+
"download_url": "http://layer.geojson.example.com",
397393
}
398-
_add_api_response(detector_api_url(url), json=data)
394+
add_mock_operations_responses("success", results=results)
399395
features = []
400-
for i in range(urls_num):
401-
url = "http://layer%s.geojson.example.com" % str(i)
402-
temp_features = [
396+
for i in range(num_features):
397+
url = results["download_url"]
398+
features.append(
403399
{
404400
"type": "Feature",
405-
"geometry": make_geojson_multipolygon(j + 1),
401+
"geometry": make_geojson_multipolygon(i + 1),
406402
"properties": {},
407403
}
408-
for j in range(i + 1)
409-
]
410-
for f in temp_features:
411-
features.append(f)
412-
responses.add(
413-
responses.GET,
414-
url,
415-
body=json.dumps({"type": "FeatureCollection", "features": temp_features}),
416404
)
417-
return {"type": "FeatureCollection", "features": features}
405+
fc = {"type": "FeatureCollection", "features": features}
406+
responses.add(
407+
responses.GET,
408+
url,
409+
body=json.dumps(fc),
410+
)
411+
return fc
418412

419413

420414
def make_geojson_multipolygon(npolygons=1):
@@ -1015,11 +1009,11 @@ def test_download_vector_layer_to_file(monkeypatch):
10151009
with tempfile.NamedTemporaryFile() as fp:
10161010
client.download_vector_layer_to_file("foobar", fp.name)
10171011
fc = json.load(fp)
1018-
assert fc == expected_content and len(fc["features"]) == 3
1012+
assert fc == expected_content and len(fc["features"]) == 2
10191013
assert (
10201014
fc["type"] == "FeatureCollection" and fc["features"][0]["type"] == "Feature"
10211015
)
1022-
assert len(responses.calls) == 3
1016+
assert len(responses.calls) == 3 # POST /download, GET /operations, GET url
10231017

10241018

10251019
@responses.activate

0 commit comments

Comments
 (0)