2
2
# Copyright (C) 2009 Robert Collins <robertc@robertcollins.net>
3
3
# Licenced under the txaws licence available at /LICENSE in the txaws source.
5
from twisted.web.client import _parse
5
7
from txaws.credentials import AWSCredentials
6
from txaws import regions
7
from txaws.util import parse
10
10
__all__ = ["AWSServiceEndpoint", "AWSServiceRegion", "REGION_US", "REGION_EU"]
13
# These old variable names are maintained for backwards compatibility.
14
REGION_US = regions.REGION_US
15
REGION_EU = regions.REGION_EU
16
EC2_ENDPOINT_US = regions.EC2_ENDPOINT_US
17
EC2_ENDPOINT_EU = regions.EC2_ENDPOINT_EU
18
S3_ENDPOINT = regions.S3_ENDPOINT
15
EC2_ENDPOINT_US = "https://us-east-1.ec2.amazonaws.com/"
16
EC2_ENDPOINT_EU = "https://eu-west-1.ec2.amazonaws.com/"
21
20
class AWSServiceEndpoint(object):
23
22
@param uri: The URL for the service.
24
23
@param method: The HTTP method used when accessing a service.
25
@param ssl_hostname_verification: Whether or not SSL hotname verification
26
will be done when connecting to the endpoint.
29
def __init__(self, uri="", method="GET", ssl_hostname_verification=False):
26
def __init__(self, uri="", method="GET"):
28
self.port = DEFAULT_PORT
33
30
self.method = method
34
self.ssl_hostname_verification = ssl_hostname_verification
35
31
self._parse_uri(uri)
36
32
if not self.scheme:
37
33
self.scheme = "http"
39
35
def _parse_uri(self, uri):
40
scheme, host, port, path = parse(
41
str(uri), defaultPort=False)
36
scheme, host, port, path = _parse(
37
str(uri), defaultPort=DEFAULT_PORT)
42
38
self.scheme = scheme
47
def set_host(self, host):
53
def get_canonical_host(self):
55
Return the canonical host as for the Host HTTP header specification.
57
host = self.host.lower()
58
if self.port is not None:
59
host = "%s:%s" % (host, self.port)
62
def set_canonical_host(self, canonical_host):
64
Set host and port from a canonical host string as for the Host HTTP
67
parts = canonical_host.lower().split(":")
69
if len(parts) > 1 and parts[1]:
70
self.port = int(parts[1])
74
43
def set_path(self, path):
78
47
"""Get a URL representation of the service."""
79
uri = "%s://%s%s" % (self.scheme, self.get_canonical_host(), self.path)
82
def set_method(self, method):
48
uri = "%s://%s" % (self.scheme, self.host)
49
if self.port and self.port != DEFAULT_PORT:
50
uri = "%s:%s" % (uri, self.port)
51
return uri + self.path
86
54
class AWSServiceRegion(object):
98
66
associated creds will be used against a collection of services.
99
67
@param uri: an endpoint URI that, if provided, will override the region
101
@param method: The method argument forwarded to L{AWSServiceEndpoint}.
103
# XXX update unit test to check for both ec2 and s3 endpoints
104
70
def __init__(self, creds=None, access_key="", secret_key="",
105
region=REGION_US, uri="", ec2_uri="", s3_uri="",
71
region=REGION_US, uri=""):
108
73
creds = AWSCredentials(access_key, secret_key)
109
74
self.creds = creds
110
# Provide backwards compatibility for the "uri" parameter.
111
if uri and not ec2_uri:
113
if not ec2_uri and region == REGION_US:
114
ec2_uri = EC2_ENDPOINT_US
115
elif not ec2_uri and region == REGION_EU:
116
ec2_uri = EC2_ENDPOINT_EU
119
75
self._clients = {}
120
self.ec2_endpoint = AWSServiceEndpoint(uri=ec2_uri, method=method)
121
self.s3_endpoint = AWSServiceEndpoint(uri=s3_uri, method=method)
78
elif region == REGION_US:
79
ec2_endpoint = EC2_ENDPOINT_US
80
elif region == REGION_EU:
81
ec2_endpoint = EC2_ENDPOINT_EU
82
self.ec2_endpoint = AWSServiceEndpoint(uri=ec2_endpoint)
123
84
def get_client(self, cls, purge_cache=False, *args, **kwds):
141
102
self.creds = creds
142
103
return self.get_client(EC2Client, creds=self.creds,
143
104
endpoint=self.ec2_endpoint, query_factory=None)
145
def get_s3_client(self, creds=None):
146
from txaws.s3.client import S3Client
150
return self.get_client(S3Client, creds=self.creds,
151
endpoint=self.s3_endpoint, query_factory=None)