1+ import logging
12import typing
23import urllib .request
34from typing import List
45
56from openeo .job import Job , JobResult
7+ from openeo .rest import OpenEoClientException
68
79if hasattr (typing , 'TYPE_CHECKING' ) and typing .TYPE_CHECKING :
810 # Only import this for type hinting purposes. Runtime import causes circular dependency issues.
911 # Note: the `hasattr` check is necessary for Python versions before 3.5.2.
1012 from openeo .rest .connection import Connection
1113
14+ logger = logging .getLogger (__name__ )
15+
1216
1317class RESTJobResult (JobResult ):
1418 def __init__ (self , url ):
@@ -54,9 +58,10 @@ def estimate_job(self):
5458 def start_job (self ):
5559 """ Start / queue a job for processing."""
5660 # POST /jobs/{job_id}/results
57- request = self .connection .post ("/jobs/{}/results" .format (self .job_id ))
58-
59- return request .status_code
61+ url = "/jobs/{}/results" .format (self .job_id )
62+ request = self .connection .post (url )
63+ if request .status_code != 202 :
64+ logger .warning ("{u} returned with status code {s} instead of 202" .format (u = url , s = request .status_code ))
6065
6166 def stop_job (self ):
6267 """ Stop / cancel job processing."""
@@ -72,34 +77,24 @@ def list_results(self, type=None):
7277
7378 def download_results (self , target ):
7479 """ Download job results."""
75- # GET /jobs/{job_id}/results > ...
76-
77- download_url = "/jobs/{}/results" .format (self .job_id )
78- r = self .connection .get (download_url , stream = True )
79-
80- if r .status_code == 200 :
81-
82- url = r .json ()
83- if "links" in url :
84- download_url = url ["links" ][0 ]
85- if "href" in download_url :
86- download_url = download_url ["href" ]
87-
88- with open (target , 'wb' ) as handle :
89- response = self .connection .get (download_url , stream = True )
90-
91- for block in response .iter_content (1024 ):
92-
93- if not block :
94- break
95-
96- handle .write (block )
97- else :
98- raise ConnectionAbortedError (r .text )
99- return r .status_code
100-
101- # TODO: All below methods are deprecated (at least not specified in the coreAPI)
102- def download (self , outputfile :str , outputformat = None ):
80+ results_url = "/jobs/{}/results" .format (self .job_id )
81+ r = self .connection .get (results_url , expected_status = 200 )
82+ links = r .json ()["links" ]
83+ if len (links ) != 1 :
84+ # TODO handle download of multiple files?
85+ raise OpenEoClientException ("Expected 1 result file to download, but got {c}" .format (c = len (links )))
86+ file_url = links [0 ]["href" ]
87+
88+ with open (target , 'wb' ) as handle :
89+ response = self .connection .get (file_url , stream = True )
90+ for block in response .iter_content (1024 ):
91+ if not block :
92+ break
93+ handle .write (block )
94+ return target
95+
96+ # TODO: All below methods are deprecated (at least not specified in the coreAPI)
97+ def download (self , outputfile : str , outputformat = None ):
10398 """ Download the result as a raster."""
10499 try :
105100 return self .connection .download_job (self .job_id , outputfile , outputformat )
@@ -117,6 +112,3 @@ def queue(self):
117112 def results (self ) -> List [RESTJobResult ]:
118113 """ Returns this job's results. """
119114 return [RESTJobResult (link ['href' ]) for link in self .connection .job_results (self .job_id )['links' ]]
120-
121-
122-
0 commit comments