Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297

298

299

300

301

302

303

304

305

306

307

308

309

310

311

312

313

314

315

# This file is part of verify. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (https://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <https://www.gnu.org/licenses/>. 

"""SQUASH (https://squash.lsst.codes) client. 

 

Data objects, particularly Job, Metric, and Specification, use this client to 

upload and retrieve data from the SQUASH verification database. 

 

SQUASH will likely be replaced by a database served behind Data Management's 

webserv API. This client is considered a shim during construction. 

""" 

 

__all__ = ['get', 'post', 'get_endpoint_url', 'reset_endpoint_cache', 

'get_default_timeout', 'get_default_api_version', 

'make_accept_header'] 

 

 

import requests 

 

import lsst.log 

 

# Version of the SQUASH API this client is compatible with 

_API_VERSION = '1.0' 

 

# Default HTTP timeout (seconds) for all SQUASH client requests. 

_TIMEOUT = 900.0 

 

# URLs for SQUASH endpoints, cached by `get_endpoint_url()`. 

_ENDPOINT_URLS = None 

 

 

def get_endpoint_url(api_url, api_endpoint, **kwargs): 

"""Lookup SQUASH endpoint URL. 

 

Parameters 

---------- 

api_url : `str` 

Root URL of the SQUASH API. For example, 

``'https://squash.lsst.codes/dashboard/api/'``. 

api_endpoint : `str` 

Name of the SQUASH API endpoint. For example, ``'job'``. 

**kwargs : optional 

Additional keyword arguments passed to `get`. 

 

Returns 

------- 

endpoint_url : `str` 

Full SQUASH endpoint URL. 

 

Notes 

----- 

Endpoints are discovered from the SQUASH API itself. The SQUASH API is 

queried on the first call to `get_endpoint_url`. Subsequent calls use 

cached results for all endpoints. This cache can be reset with the 

`reset_endpoint_cache` function. 

""" 

global _ENDPOINT_URLS 

 

if _ENDPOINT_URLS is None: 

r = get(api_url, **kwargs) 

_ENDPOINT_URLS = r.json() 

 

return _ENDPOINT_URLS[api_endpoint] 

 

 

def reset_endpoint_cache(): 

"""Reset the cache used by `get_endpoint_url`. 

""" 

global _ENDPOINT_URLS 

_ENDPOINT_URLS = None 

 

 

def get_default_timeout(): 

"""Get the default HTTP client timeout setting. 

 

Returns 

------- 

timeout : `float` 

Default timeout setting, in seconds. 

""" 

global _TIMEOUT 

return _TIMEOUT 

 

 

def get_default_api_version(): 

"""Get the default SQUASH API versioned used by the lsst.verify.squash 

client functions. 

 

Returns 

------- 

version : `str` 

API version. For example, ``'1.0'``. 

""" 

global _API_VERSION 

return _API_VERSION 

 

 

def make_accept_header(version=None): 

"""Make the ``Accept`` HTTP header for versioned SQUASH API requests. 

 

Parameters 

---------- 

version : `str`, optional 

Semantic API version, such as ``'1.0'``. By default, the API version 

this client is designed for is used (`get_default_api_version`). 

 

Returns 

------- 

accept_header : `str` 

The ``Accept`` header value. 

 

Examples 

-------- 

>>> make_accept_header() 

'application/json; version=1.0' 

""" 

if version is None: 

version = get_default_api_version() 

template = 'application/json; version={0}' 

return template.format(version) 

 

 

def get_access_token(api_url, api_user, api_password, 

api_auth_endpoint='auth'): 

"""Get access token from the SQUASH API assuming the API user exists. 

 

Parameters 

---------- 

api_url : `str` 

Root URL of the SQUASH API. For example, 

```https://squash-restful-api.lsst.codes```. 

api_user : `str` 

API username. 

api_password : `str` 

API password. 

api_auth_endpoint : `str` 

API authorization endpoint. 

 

Returns 

------- 

access_token: `str` 

The access token from the SQUASH API authorization endpoint. 

""" 

json_doc = {'username': api_user, 'password': api_password} 

 

r = post(api_url, api_auth_endpoint, json_doc) 

 

json_r = r.json() 

 

return json_r['access_token'] 

 

 

def make_authorization_header(access_token): 

"""Make an ``Authorization`` HTTP header using a SQUASH access token. 

 

Parameters 

---------- 

access_token : `str` 

Access token returned by `get_access_token`. 

 

Returns 

------- 

authorization_header : `str` 

The Authorization header value. 

""" 

authorization_header = 'JWT {0}' 

return authorization_header.format(access_token) 

 

 

def post(api_url, api_endpoint, json_doc=None, 

timeout=None, version=None, access_token=None): 

"""POST a JSON document to SQUASH. 

 

Parameters 

---------- 

api_url : `str` 

Root URL of the SQUASH API. For example, 

``'https://squash.lsst.codes/api'``. 

api_endpoint : `str` 

Name of the API endpoint to post to. 

json_doc : `dict` 

A JSON-serializable object. 

timeout : `float`, optional 

Request timeout. The value of `get_default_timeout` is used by default. 

version : `str`, optional 

API version. The value of `get_default_api_version` is used by default. 

access_token : `str`, optional 

Access token (see `get_access_token`). Not required when a POST is done 

to the API authorization endpoint. 

 

Raises 

------ 

requests.exceptions.RequestException 

Raised if the HTTP request fails. 

 

Returns 

------- 

response : `requests.Response` 

Response object. Obtain JSON content with ``response.json()``. 

""" 

log = lsst.log.Log.getLogger('verify.squash.post') 

 

api_endpoint_url = get_endpoint_url(api_url, api_endpoint) 

 

headers = { 

'Accept': make_accept_header(version) 

} 

 

if access_token: 

headers['Authorization'] = make_authorization_header(access_token) 

 

try: 

# Disable redirect following for POST as requests will turn a POST into 

# a GET when following a redirect. http://ls.st/pbx 

r = requests.post(api_endpoint_url, 

json=json_doc, 

allow_redirects=False, 

headers=headers, 

timeout=timeout or get_default_timeout()) 

log.info('POST {0} status: {1}'.format(api_endpoint_url, 

r.status_code)) 

r.raise_for_status() 

 

# be pedantic about return status. requests#status_code will not error 

# on 3xx codes 

if r.status_code != 200 and r.status_code != 201 \ 

and r.status_code != 202: 

message = 'Expected status = 200(OK), 201(Created) or 202' \ 

'(Accepted). Got status={0}. {1}'.format(r.status_code, 

r.reason) 

raise requests.exceptions.RequestException(message) 

except requests.exceptions.RequestException as e: 

log.error(str(e)) 

raise e 

 

return r 

 

 

def get(api_url, api_endpoint=None, 

api_user=None, api_password=None, timeout=None, version=None): 

"""GET request to the SQUASH API. 

 

Parameters 

---------- 

api_url : `str` 

Root URL of the SQUASH API. For example, 

``'https://squash.lsst.codes/api'``. 

api_endpoint : `str`, optional 

Name of the API endpoint to post to. The ``api_url`` is requested if 

unset. 

api_user : `str`, optional 

API username. 

api_password : `str`, optional 

API password. 

timeout : `float`, optional 

Request timeout. The value of `get_default_timeout` is used by default. 

version : `str`, optional 

API version. The value of `get_default_api_version` is used by default. 

 

Raises 

------ 

requests.exceptions.RequestException 

Raised if the HTTP request fails. 

 

Returns 

------- 

response : `requests.Response` 

Response object. Obtain JSON content with ``response.json()``. 

""" 

log = lsst.log.Log.getLogger('verify.squash.get') 

 

if api_user is not None and api_password is not None: 

auth = (api_user, api_password) 

else: 

auth = None 

 

if api_endpoint is not None: 

api_endpoint_url = get_endpoint_url(api_url, api_endpoint) 

else: 

api_endpoint_url = api_url 

 

headers = { 

'Accept': make_accept_header(version) 

} 

 

try: 

r = requests.get(api_endpoint_url, 

auth=auth, 

headers=headers, 

timeout=timeout or get_default_timeout()) 

log.info('GET {0} status: {1}'.format(api_endpoint_url, 

r.status_code)) 

r.raise_for_status() 

except requests.exceptions.RequestException as e: 

log.error(str(e)) 

raise e 

 

return r