1
+ import logging
1
2
import typing
2
3
import urllib .request
3
4
from typing import List
4
5
5
6
from openeo .job import Job , JobResult
7
+ from openeo .rest import OpenEoClientException
6
8
7
9
if hasattr (typing , 'TYPE_CHECKING' ) and typing .TYPE_CHECKING :
8
10
# Only import this for type hinting purposes. Runtime import causes circular dependency issues.
9
11
# Note: the `hasattr` check is necessary for Python versions before 3.5.2.
10
12
from openeo .rest .connection import Connection
11
13
14
+ logger = logging .getLogger (__name__ )
15
+
12
16
13
17
class RESTJobResult (JobResult ):
14
18
def __init__ (self , url ):
@@ -54,9 +58,10 @@ def estimate_job(self):
54
58
def start_job (self ):
55
59
""" Start / queue a job for processing."""
56
60
# 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 ))
60
65
61
66
def stop_job (self ):
62
67
""" Stop / cancel job processing."""
@@ -72,34 +77,24 @@ def list_results(self, type=None):
72
77
73
78
def download_results (self , target ):
74
79
""" 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 ):
103
98
""" Download the result as a raster."""
104
99
try :
105
100
return self .connection .download_job (self .job_id , outputfile , outputformat )
@@ -117,6 +112,3 @@ def queue(self):
117
112
def results (self ) -> List [RESTJobResult ]:
118
113
""" Returns this job's results. """
119
114
return [RESTJobResult (link ['href' ]) for link in self .connection .job_results (self .job_id )['links' ]]
120
-
121
-
122
-
0 commit comments