vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / loading / request.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  You may obtain a copy of the License at
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 import os
17 import tempfile
18
19 from requests import Session
20 from requests.exceptions import (ConnectionError, InvalidSchema)
21 from cachecontrol import CacheControl
22 from cachecontrol.caches import FileCache
23
24 from .exceptions import LoaderException, DocumentNotFoundException
25 from .loader import Loader
26
27 SESSION = None
28 SESSION_CACHE_PATH = os.path.join(tempfile.gettempdir(), 'aria_requests')
29
30
31 class RequestLoader(Loader):
32     """
33     Base class for ARIA request-based loaders.
34
35     Extracts a document from a URI by performing a request.
36
37     Note that the "file:" schema is not supported: :class:`FileTextLoader` should
38     be used instead.
39     """
40
41     def __init__(self, context, uri, headers=None):
42         if headers is None:
43             headers = {}
44         self.context = context
45         self.uri = uri
46         self.headers = headers
47         self._response = None
48
49     def load(self):
50         pass
51
52     def open(self):
53         global SESSION
54         if SESSION is None:
55             SESSION = CacheControl(Session(), cache=FileCache(SESSION_CACHE_PATH))
56
57         try:
58             self._response = SESSION.get(self.uri, headers=self.headers)
59         except InvalidSchema as e:
60             raise DocumentNotFoundException('document not found: "%s"' % self.uri, cause=e)
61         except ConnectionError as e:
62             raise LoaderException('request connection error: "%s"' % self.uri, cause=e)
63         except Exception as e:
64             raise LoaderException('request error: "%s"' % self.uri, cause=e)
65
66         status = self._response.status_code
67         if status == 404:
68             self._response = None
69             raise DocumentNotFoundException('document not found: "%s"' % self.uri)
70         elif status != 200:
71             self._response = None
72             raise LoaderException('request error %d: "%s"' % (status, self.uri))
73
74
75 class RequestTextLoader(RequestLoader):
76     """
77     ARIA request-based text loader.
78     """
79
80     def load(self):
81         if self._response is not None:
82             try:
83                 if self._response.encoding is None:
84                     self._response.encoding = 'utf8'
85                 return self._response.text
86             except Exception as e:
87                 raise LoaderException('request error: %s' % self.uri, cause=e)
88         return None