/usr/lib/python3/dist-packages/ripe/atlas/cousteau/request.py is in python3-ripe-atlas-cousteau 1.3-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | # Copyright (c) 2016 RIPE NCC
#
# 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 <http://www.gnu.org/licenses/>.
"""
Module containing the main class that create the post data and makes the HTTP
request according to the ATLAS API.
"""
import calendar
import requests
from dateutil import parser
from datetime import datetime
from .version import __version__
class AtlasRequest(object):
"""
Base class for doing Atlas requests. Contains functions that can be used by
most Atlas requests.
"""
http_methods = {
"GET": requests.get,
"POST": requests.post,
"DELETE": requests.delete
}
def __init__(self, **kwargs):
self.url = ""
self.key = kwargs.get("key")
self.url_path = kwargs.get("url_path", "")
self.server = kwargs.get("server") or "atlas.ripe.net"
self.verify = kwargs.get("verify", True)
self.proxies = kwargs.get("proxies", {})
self.headers = kwargs.get("headers", None)
default_user_agent = "RIPE ATLAS Cousteau v{0}".format(__version__)
self.http_agent = kwargs.get("user_agent") or default_user_agent
self.http_method_args = {
"params": {"key": self.key},
"headers": self.get_headers(),
"verify": self.verify,
"proxies": self.proxies
}
self.post_data = {}
def get_headers(self):
"""Return header for the HTTP request."""
headers = {
"User-Agent": self.http_agent,
"Content-Type": "application/json",
"Accept": "application/json"
}
if self.headers:
headers.update(self.headers)
return headers
def http_method(self, method):
"""
Execute the given HTTP method and returns if it's success or not
and the response as a string if not success and as python object after
unjson if it's success.
"""
self.build_url()
try:
response = self.get_http_method(method)
is_success = response.ok
try:
response_message = response.json()
except ValueError:
response_message = response.text
except requests.exceptions.RequestException as exc:
is_success = False
response_message = exc.args
return is_success, response_message
def get_http_method(self, method):
"""Gets the http method that will be called from the requests library"""
return self.http_methods[method](self.url, **self.http_method_args)
def build_url(self):
"""
Builds the request's url combining server and url_path
classes attributes.
"""
self.url = "https://{0}{1}".format(self.server, self.url_path)
def get(self, **url_params):
"""
Makes the HTTP GET to the url.
"""
if url_params:
self.http_method_args["params"].update(url_params)
return self.http_method("GET")
def post(self):
"""
Makes the HTTP POST to the url sending post_data.
"""
self._construct_post_data()
post_args = {"json": self.post_data}
self.http_method_args.update(post_args)
return self.http_method("POST")
def _construct_post_data(self):
raise NotImplementedError
def clean_time(self, time):
"""
Transform time field to datetime object if there is any.
"""
if isinstance(time, int):
time = datetime.utcfromtimestamp(time)
elif isinstance(time, str):
time = parser.parse(time)
return time
class AtlasCreateRequest(AtlasRequest):
"""
Class responsible for creating a request for creating a new Atlas
measurement. Takes as arguments Atlas API key, a list of Atlas measurement
objects and a list of Atlas sources. Optionally the start and end time and
whether the measurement is a oneoff can be specified.
Usage:
from ripe.atlas import AtlasCreateRequest
ar = AtlasCreateRequest(**{
"start_time": start,
"stop_time": stop,
"key": "path_to_key",
"measurements":[measurement1, ...],
"sources": [source1, ...],
"is_oneoff": True/False
})
ar.create()
"""
def __init__(self, **kwargs):
super(AtlasCreateRequest, self).__init__(**kwargs)
self.url_path = '/api/v2/measurements/'
self.measurements = kwargs["measurements"]
self.sources = kwargs["sources"]
self.start_time = self.clean_time(kwargs.get("start_time"))
self.stop_time = self.clean_time(kwargs.get("stop_time"))
if kwargs.get("is_oneoff"):
self.is_oneoff = kwargs["is_oneoff"]
else:
self.is_oneoff = False
def _construct_post_data(self):
"""
Constructs the data structure that is required from the atlas API based
on measurements, sources and times user has specified.
"""
definitions = [msm.build_api_struct() for msm in self.measurements]
probes = [source.build_api_struct() for source in self.sources]
self.post_data = {
"definitions": definitions,
"probes": probes,
"is_oneoff": self.is_oneoff
}
if self.is_oneoff:
self.post_data.update({"is_oneoff": self.is_oneoff})
if self.start_time:
self.post_data.update(
{"start_time": int(calendar.timegm(self.start_time.timetuple()))}
)
if self.stop_time:
self.post_data.update(
{"stop_time": int(calendar.timegm(self.stop_time.timetuple()))}
)
def create(self):
"""Sends the POST request"""
return self.post()
class AtlasChangeRequest(AtlasRequest):
"""Atlas request for changing probes for a running measurement.
post_data = [{
"action": "add|remove",
"requested": probe_number,
# when action=remove only probes is supported
"type": "area|country|asn|prefix|msm|probes",
"value": probe_values
}]
"""
def __init__(self, **kwargs):
super(AtlasChangeRequest, self).__init__(**kwargs)
self.url_path = '/api/v2/measurements/{0}/participation-requests/'
self.msm_id = kwargs["msm_id"]
self.sources = kwargs["sources"]
self.url_path = self.url_path.format(self.msm_id)
def _construct_post_data(self):
"""
Constructs the data structure that is required from the atlas API based
on measurement id, and the sources.
"""
self.post_data = [source.build_api_struct() for source in self.sources]
def create(self):
"""Sends the POST request"""
return self.post()
class AtlasStopRequest(AtlasRequest):
"""Atlas request for stopping a measurement."""
def __init__(self, **kwargs):
super(AtlasStopRequest, self).__init__(**kwargs)
self.url_path = '/api/v2/measurements/'
self.msm_id = kwargs["msm_id"]
self.url_path = "{0}{1}".format(self.url_path, self.msm_id)
def delete(self):
"""
Makes the HTTP DELETE to the url.
"""
return self.http_method("DELETE")
def create(self):
"""Sends the DELETE request"""
return self.delete()
class AtlasLatestRequest(AtlasRequest):
def __init__(self, msm_id=None, probe_ids=(), **kwargs):
super(AtlasLatestRequest, self).__init__(**kwargs)
self.url_path = "/api/v2/measurements/{0}/latest"
self.msm_id = msm_id
self.probe_ids = None
self.url_path = self.url_path.format(self.msm_id)
if probe_ids:
self.add_probe_parameters(probe_ids)
def add_probe_parameters(self, probe_ids):
"""
Creates string format if needed and add probe ids to HTTP
query parameters.
"""
if isinstance(probe_ids, (tuple, list)): # tuples & lists > x,y,z
self.probe_ids = ",".join([str(_) for _ in probe_ids])
else:
self.probe_ids = probe_ids
additional_params = {
"probe_ids": self.probe_ids
}
self.http_method_args["params"].update(additional_params)
def create(self):
"""Sends the GET request."""
return self.get()
class AtlasResultsRequest(AtlasRequest):
"""Atlas request for fetching results of a measurement."""
def __init__(self, **kwargs):
super(AtlasResultsRequest, self).__init__(**kwargs)
self.url_path = '/api/v2/measurements/{0}/results'
self.msm_id = kwargs["msm_id"]
self.start = self.clean_time(kwargs.get("start"))
self.stop = self.clean_time(kwargs.get("stop"))
self.probe_ids = self.clean_probes(kwargs.get("probe_ids"))
self.url_path = self.url_path.format(self.msm_id)
self.update_http_method_params()
def clean_probes(self, probe_ids):
"""
Checks format of probe ids and transform it to something API
understands.
"""
if isinstance(probe_ids, (tuple, list)): # tuples & lists > x,y,z
probe_ids = ",".join([str(_) for _ in probe_ids])
return probe_ids
def update_http_method_params(self):
"""
Update HTTP url parameters based on msm_id and query filters if
there are any.
"""
url_params = {}
if self.start:
url_params.update(
{"start": int(calendar.timegm(self.start.timetuple()))}
)
if self.stop:
url_params.update(
{"stop": int(calendar.timegm(self.stop.timetuple()))}
)
if self.probe_ids:
url_params.update({"probe_ids": self.probe_ids})
self.http_method_args["params"].update(url_params)
def create(self):
"""Sends the GET request."""
return self.get()
__all__ = [
"AtlasStopRequest", "AtlasCreateRequest",
"AtlasChangeRequest", "AtlasRequest",
"AtlasResultsRequest"
]
|