/usr/share/pyshared/piston_mini_client/__init__.py is in python-piston-mini-client 0.7.2-0ubuntu1.
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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 | # -*- coding: utf-8 -*-
# Copyright 2010-2012 Canonical Ltd. This software is licensed under the
# GNU Lesser General Public License version 3 (see the file LICENSE).
import httplib2
try:
# ensure we have a version with a fix for
# http://code.google.com/p/httplib2/issues/detail?id=38
# and if not, patch in our own socks with the fix
from httplib2.socks import PROXY_TYPE_HTTP_NO_TUNNEL
from httplib2 import socks
except ImportError:
from piston_mini_client import socks
httplib2.socks = socks
import json
import os
import socket
import urllib
from datetime import datetime
from functools import wraps
from urlparse import urlparse, urlunparse
from piston_mini_client.failhandlers import (
APIError,
ExceptionFailHandler,
format_request,
format_response,
SocketError,
TimeoutError,
)
from piston_mini_client.consts import (
DISABLE_SSL_VALIDATION_ENVVAR,
LOG_FILENAME_ENVVAR,
TIMEOUT_ENVVAR,
)
class OfflineModeException(Exception):
pass
# taken from lazr.restfulclients _browser.py file to work around
# the problem that ecryptfs is very unhappy about long filenames
# upstream commented here:
# http://code.google.com/p/httplib2/issues/detail?id=92
MAXIMUM_CACHE_FILENAME_LENGTH = 143
from httplib2 import _md5, re_url_scheme, re_slash
def safename(filename):
"""Return a filename suitable for the cache.
Strips dangerous and common characters to create a filename we
can use to store the cache in.
"""
# this is a stock httplib2 copy
try:
if re_url_scheme.match(filename):
if isinstance(filename, str):
filename = filename.decode('utf-8')
filename = filename.encode('idna')
else:
filename = filename.encode('idna')
except UnicodeError:
pass
if isinstance(filename, unicode):
filename = filename.encode('utf-8')
filemd5 = _md5(filename).hexdigest()
filename = re_url_scheme.sub("", filename)
filename = re_slash.sub(",", filename)
# This is the part that we changed. In stock httplib2, the
# filename is trimmed if it's longer than 200 characters, and then
# a comma and a 32-character md5 sum are appended. This causes
# problems on eCryptfs filesystems, where the maximum safe
# filename length is closer to 143 characters.
#
# We take a (user-hackable) maximum filename length from
# RestfulHttp and subtract 33 characters to make room for the comma
# and the md5 sum.
#
# See:
# http://code.google.com/p/httplib2/issues/detail?id=92
# https://bugs.launchpad.net/bugs/344878
# https://bugs.launchpad.net/bugs/545197
maximum_filename_length = MAXIMUM_CACHE_FILENAME_LENGTH
maximum_length_before_md5_sum = maximum_filename_length - 32 - 1
if len(filename) > maximum_length_before_md5_sum:
filename = filename[:maximum_length_before_md5_sum]
return ",".join((filename, filemd5))
def returns_json(func):
"""The response data will be deserialized using a simple JSON decoder"""
@wraps(func)
def wrapper(*args, **kwargs):
body = func(*args, **kwargs)
if not isinstance(body, basestring):
return body
return json.loads(body)
return wrapper
def returns(cls, none_allowed=False):
"""The response data will be deserialized into an instance of ``cls``.
The provided class should be a descendant of ``PistonResponseObject``,
or some other class that provides a ``from_response`` method.
``none_allowed``, defaulting to ``False``, specifies whether or not
``None`` is a valid response. If ``True`` then the api can return ``None``
instead of a ``PistonResponseObject``.
"""
def decorator(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
body = func(self, *args, **kwargs)
if not isinstance(body, basestring):
return body
return cls.from_response(body, none_allowed)
return wrapper
return decorator
def returns_list_of(cls):
"""The response data will be deserialized into a list of ``cls``.
The provided class should be a descendant of ``PistonResponseObject``,
or some other class that provides a ``from_response`` method.
"""
def decorator(func):
@wraps(func)
def wrapper(self, *args, **kwargs):
body = func(self, *args, **kwargs)
if not isinstance(body, basestring):
return body
data = json.loads(body)
items = []
for datum in data:
items.append(cls.from_dict(datum))
return items
return wrapper
return decorator
class PistonResponseObject(object):
"""Base class for objects that are returned from api calls."""
@classmethod
def from_response(cls, body, none_allowed=False):
data = json.loads(body)
if none_allowed and data is None:
return data
obj = cls.from_dict(data)
return obj
@classmethod
def from_dict(cls, data):
obj = cls()
for key, value in data.items():
setattr(obj, key, value)
return obj
class PistonSerializable(object):
"""Base class for objects that want to be used as api call arguments.
Children classes should at least redefine ``_atts`` to state the list of
attributes that will be serialized into each request.
"""
_atts = ()
def __init__(self, **kwargs):
for (key, value) in kwargs.items():
setattr(self, key, value)
def as_serializable(self):
"""Return a serializable representation of this object."""
data = {}
for att in self._atts:
if not hasattr(self, att):
raise ValueError("Attempted to serialize attribute '%s'"
% att)
data[att] = getattr(self, att)
return data
def _as_serializable(self):
"""_as_serializable is deprecated; use as_serializable() instead."""
import warnings
warnings.warn("_as_serializable is deprecated; "
"use as_serializable instead", DeprecationWarning)
return self.as_serializable()
class PistonAPI(object):
"""This class provides methods to make http requests slightly easier.
It's a wrapper around ``httplib2`` to allow for a bit of state to
be stored (like the service root) so that you don't need to repeat
yourself as much.
It's not intended to be used directly. Children classes should implement
methods that actually call out to the api methods.
When you define your API's methods you'll
want to just call out to the ``_get``, ``_post``, ``_put`` or ``_delete``
methods provided by this class.
"""
SUPPORTED_SCHEMAS = ("http", "https")
default_service_root = ''
default_content_type = 'application/json'
default_timeout = None
fail_handler = ExceptionFailHandler
extra_headers = None
serializers = None
def __init__(self, service_root=None, cachedir=None, auth=None,
offline_mode=False, disable_ssl_validation=False,
log_filename=None, timeout=None):
"""Initialize a ``PistonAPI``.
``service_root`` is the url to the server's service root.
Children classes can provide a ``default_service_root`` class
attribute that will be used if ``service_root`` is ``None``.
``cachedir`` will be used as ``httplib2``'s cache directory if
provided.
``auth`` can be an instance of ``BasicAuthorizer`` or
``OAuthAuthorizer`` or any object that provides a ``sign_request``
method. If ``auth`` is ``None`` you'll only be able to make public
API calls. See :ref:`authentication` for details.
``disable_ssl_validation`` will skip server SSL certificate
validation when using secure connections. ``httplib2`` < 0.7.0
doesn't support certificate validation anyway, so if you're using an
older ``httplib2`` this will have no effect.
``offline_mode`` will not touch the network. In this case only cached
results will be available.
If you pass in a ``log_filename``, all requests and responses
including headers will be logged to this file.
``timeout`` will be used as a socket timeout for all calls this
instance makes. To explicitly set no timeout, set timeout=0. The
default timeout=None will first check for an environment variable
``PISTON_MINI_CLIENT_DEFAULT_TIMEOUT`` and try to use that. If this
environment variable is not found or it is an invalid float, the
class's ``default_timeout`` will be used. Finally, if the class's
default is also None, Python's default timeout for sockets will be
used. All these should be in seconds.
"""
if service_root is None:
service_root = self.default_service_root
if not service_root:
raise ValueError("No service_root provided, and no default found")
parsed_service_root = urlparse(service_root)
scheme = parsed_service_root.scheme
if scheme not in self.SUPPORTED_SCHEMAS:
raise ValueError("service_root's scheme must be http or https")
self._service_root = service_root
self._parsed_service_root = list(parsed_service_root)
if cachedir:
self._httplib2_cache = httplib2.FileCache(cachedir, safe=safename)
else:
self._httplib2_cache = None
self._auth = auth
self._offline_mode = offline_mode
self._disable_ssl_validation = disable_ssl_validation
if timeout is None:
try:
timeout = float(os.environ.get(TIMEOUT_ENVVAR))
except (TypeError, ValueError):
timeout = self.default_timeout
self._timeout = timeout
# create one httplib2.Http object per scheme so that we can
# have per-scheme proxy settings (see also Issue 26
# http://code.google.com/p/httplib2/issues/detail?id=26)
self._http = {}
for scheme in self.SUPPORTED_SCHEMAS:
self._http[scheme] = self._get_http_obj_for_scheme(scheme)
if self.serializers is None:
self.serializers = {}
if log_filename is None:
log_filename = os.environ.get(LOG_FILENAME_ENVVAR)
self.log_filename = log_filename
def _get_http_obj_for_scheme(self, scheme):
proxy_info = self._get_proxy_info(scheme)
http = None
if (self._disable_ssl_validation or
os.environ.get(DISABLE_SSL_VALIDATION_ENVVAR)):
try:
http = httplib2.Http(
cache=self._httplib2_cache,
timeout=self._timeout,
disable_ssl_certificate_validation=True,
proxy_info=proxy_info)
except TypeError:
# httplib2 < 0.7.0 doesn't support cert validation anyway
pass
if http is None:
http = httplib2.Http(cache=self._httplib2_cache,
timeout=self._timeout, proxy_info=proxy_info)
return http
def _get_proxy_info(self, scheme):
envvar = "%s_proxy" % scheme
if envvar in os.environ:
url = urlparse(os.environ[envvar])
user_pass, sep, host_and_port = url.netloc.rpartition("@")
user, sep, passw = user_pass.partition(":")
host, sep, port = host_and_port.partition(":")
if port:
port = int(port)
proxy_type = socks.PROXY_TYPE_HTTP
if scheme == "http":
# this will not require the CONNECT acl from squid and
# is good enough for http connections
proxy_type = socks.PROXY_TYPE_HTTP_NO_TUNNEL
proxy_info = httplib2.ProxyInfo(
proxy_type=proxy_type,
proxy_host=host,
proxy_port=port or 8080,
proxy_user=user or None,
proxy_pass=passw or None)
return proxy_info
return None
def _get(self, path, args=None, scheme=None, extra_headers=None):
"""Perform an HTTP GET request.
The provided ``path`` is appended to this resource's ``_service_root``
attribute to obtain the absolute URL that will be requested.
If provided, ``args`` should be a dict specifying additional GET
arguments that will be encoded on to the end of the url.
``scheme`` must be one of *http* or *https*, and will determine the
scheme used for this particular request. If not provided the
service_root's scheme will be used.
``extra_headers`` is an optional dictionary of header key/values that
will be added to the http request.
"""
if args is not None:
if '?' in path:
path += '&'
else:
path += '?'
path += urllib.urlencode(args)
headers = self._prepare_headers(extra_headers=extra_headers)
return self._request(path, method='GET', scheme=scheme,
headers=headers)
def _post(self, path, data=None, content_type=None, scheme=None,
extra_headers=None):
"""Perform an HTTP POST request.
The provided ``path`` is appended to this api's ``_service_root``
attribute to obtain the absolute URL that will be requested. ``data``
should be:
- A string, in which case it will be used directly as the request's
body, or
- A ``list``, ``dict``, ``int``, ``bool`` or ``PistonSerializable``
(something with an ``as_serializable`` method) or even ``None``,
in which case it will be serialized into a string according to
``content_type``.
If ``content_type`` is ``None``, ``self.default_content_type`` will
be used.
``scheme`` must be one of *http* or *https*, and will determine the
scheme used for this particular request. If not provided the
service_root's scheme will be used.
``extra_headers`` is an optional dictionary of header key/values that
will be added to the http request.
"""
body, headers = self._prepare_request(data, content_type,
extra_headers=extra_headers)
return self._request(path, method='POST', body=body,
headers=headers, scheme=scheme)
def _put(self, path, data=None, content_type=None, scheme=None,
extra_headers=None):
"""Perform an HTTP PUT request.
The provided ``path`` is appended to this api's ``_service_root``
attribute to obtain the absolute URL that will be requested. ``data``
should be:
- A string, in which case it will be used directly as the request's
body, or
- A ``list``, ``dict``, ``int``, ``bool`` or ``PistonSerializable``
(something with an ``as_serializable`` method) or even ``None``,
in which case it will be serialized into a string according to
``content_type``.
If ``content_type`` is ``None``, ``self.default_content_type`` will be
used.
``scheme`` must be one of *http* or *https*, and will determine the
scheme used for this particular request. If not provided the
service_root's scheme will be used.
``extra_headers`` is an optional dictionary of header key/values that
will be added to the http request.
"""
body, headers = self._prepare_request(data, content_type,
extra_headers=extra_headers)
return self._request(path, method='PUT', body=body,
headers=headers, scheme=scheme)
def _delete(self, path, scheme=None, extra_headers=None):
"""Perform an HTTP DELETE request.
The provided ``path`` is appended to this resource's ``_service_root``
attribute to obtain the absolute URL that will be requested.
``scheme`` must be one of *http* or *https*, and will determine the
scheme used for this particular request. If not provided the
service_root's scheme will be used.
``extra_headers`` is an optional dictionary of header key/values that
will be added to the http request.
"""
headers = self._prepare_headers(extra_headers=extra_headers)
return self._request(path, method='DELETE', scheme=scheme,
headers=headers)
def _prepare_request(self, data=None, content_type=None,
extra_headers=None):
"""Put together a set of headers and a body for a request.
If ``content_type`` is not provided, ``self.default_content_type``
will be assumed.
You probably never need to call this method directly.
"""
if content_type is None:
content_type = self.default_content_type
body = self._prepare_body(data, content_type)
headers = self._prepare_headers(content_type, extra_headers)
return body, headers
def _prepare_headers(self, content_type=None, extra_headers=None):
"""Put together and return a complete set of headers.
If ``content_type`` is provided, it will be added as
the Content-type header.
Any provided ``extra_headers`` will be added last.
You probably never need to call this method directly.
"""
headers = {}
if content_type:
headers['Content-type'] = content_type
if self.extra_headers is not None:
headers.update(self.extra_headers)
if extra_headers is not None:
headers.update(extra_headers)
return headers
def _prepare_body(self, data=None, content_type=None):
"""Serialize data into a request body.
``data`` will be serialized into a string, according to
``content_type``.
You probably never need to call this method directly.
"""
body = data
if not isinstance(data, basestring):
serializer = self._get_serializer(content_type)
body = serializer.serialize(data)
return body
def _request(self, path, method, body='', headers=None, scheme=None):
"""Perform an HTTP request.
You probably want to call one of the ``_get``, ``_post``, ``_put``
methods instead.
"""
if headers is None:
headers = {}
if scheme not in [None, 'http', 'https']:
raise ValueError('Invalid scheme %s' % scheme)
url = self._path2url(path, scheme=scheme)
# in offline mode either get it from the cache or return None
if self._offline_mode:
if method in ('POST', 'PUT'):
err = "method '%s' not allowed in offline-mode" % method
raise OfflineModeException(err)
return self._get_from_cache(url)
if scheme is None:
scheme = urlparse(url).scheme
if self._auth:
self._auth.sign_request(url, method, body, headers)
if self.log_filename:
self._dump_request(url, method, body, headers)
try:
response, response_body = self._http[scheme].request(url,
method=method, body=body, headers=headers)
except AttributeError, e:
# Special case out httplib2's way of telling us unable to connect
if e.args[0] == "'NoneType' object has no attribute 'makefile'":
raise APIError('Unable to connect to %s' % self._service_root)
else:
raise
except socket.timeout, e:
raise TimeoutError('Timed out attempting to connect to %s' %
self._service_root)
except (socket.gaierror, socket.error), e:
raise SocketError('connecting to %s: %s' % (self._service_root,
e.message))
if self.log_filename:
self._dump_response(response, response_body)
handler = self.fail_handler(url, method, body, headers)
body = handler.handle(response, response_body)
return body
def _dump_request(self, url, method, body, headers):
try:
with open(self.log_filename, 'a') as f:
f.write("{0}: {1}".format(datetime.now(),
format_request(url, method, body, headers)))
except IOError:
pass
def _dump_response(self, response, body):
try:
with open(self.log_filename, 'a') as f:
f.write("{0}: {1}".format(datetime.now(),
format_response(response, body)))
except IOError:
pass
def _get_from_cache(self, url):
""" get a given url from the cachedir even if its expired
or return None if no data is available
"""
scheme = urlparse(url).scheme
if self._http[scheme].cache:
cached_value = self._http[scheme].cache.get(
httplib2.urlnorm(url)[-1])
if cached_value:
info, content = cached_value.split('\r\n\r\n', 1)
return content
def _path2url(self, path, scheme=None):
if scheme is None:
service_root = self._service_root
else:
parts = [scheme] + self._parsed_service_root[1:]
service_root = urlunparse(parts)
return service_root.strip('/') + '/' + path.lstrip('/')
def _get_serializer(self, content_type=None):
# Import here to avoid a circular import
from piston_mini_client.serializers import get_serializer
if content_type is None:
content_type = self.default_content_type
default_serializer = get_serializer(content_type)
return self.serializers.get(content_type, default_serializer)
|