OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2012 Amazon.com, Inc. or its affiliates. |
| 3 # All Rights Reserved |
| 4 # |
| 5 # Permission is hereby granted, free of charge, to any person obtaining a |
| 6 # copy of this software and associated documentation files (the |
| 7 # "Software"), to deal in the Software without restriction, including |
| 8 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 9 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 10 # persons to whom the Software is furnished to do so, subject to the fol- |
| 11 # lowing conditions: |
| 12 # |
| 13 # The above copyright notice and this permission notice shall be included |
| 14 # in all copies or substantial portions of the Software. |
| 15 # |
| 16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 17 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 18 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 19 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 22 # IN THE SOFTWARE. |
| 23 # |
| 24 |
| 25 import requests |
| 26 from .auth import SigV2Auth |
| 27 from .credentials import get_credentials |
| 28 from .dictresponse import Element, XmlHandler |
| 29 |
| 30 |
| 31 class Service(object): |
| 32 """ |
| 33 This is a simple example service that connects to the EC2 endpoint |
| 34 and supports a single request (DescribeInstances) to show how to |
| 35 use the requests-based code rather than the standard boto code which |
| 36 is based on httplib. At the moment, the only auth mechanism |
| 37 supported is SigV2. |
| 38 """ |
| 39 |
| 40 def __init__(self, host='https://ec2.us-east-1.amazonaws.com', |
| 41 path='/', api_version='2012-03-01', persona=None): |
| 42 self.credentials = get_credentials(persona) |
| 43 self.auth = SigV2Auth(self.credentials, api_version=api_version) |
| 44 self.host = host |
| 45 self.path = path |
| 46 |
| 47 def get_response(self, params, list_marker=None): |
| 48 r = requests.post(self.host, params=params, |
| 49 hooks={'args': self.auth.add_auth}) |
| 50 r.encoding = 'utf-8' |
| 51 body = r.text.encode('utf-8') |
| 52 e = Element(list_marker=list_marker, pythonize_name=True) |
| 53 h = XmlHandler(e, self) |
| 54 h.parse(body) |
| 55 return e |
| 56 |
| 57 def build_list_params(self, params, items, label): |
| 58 if isinstance(items, str): |
| 59 items = [items] |
| 60 for i in range(1, len(items) + 1): |
| 61 params['%s.%d' % (label, i)] = items[i - 1] |
| 62 |
| 63 def describe_instances(self, instance_ids=None): |
| 64 params = {} |
| 65 if instance_ids: |
| 66 self.build_list_params(params, instance_ids, 'InstanceId') |
| 67 return self.get_response(params) |
OLD | NEW |