|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +from .configuration import configuration |
| 14 | +from .rest import ApiException |
| 15 | + |
| 16 | +import certifi |
| 17 | +import websocket |
| 18 | +import six |
| 19 | +import ssl |
| 20 | +from six.moves.urllib.parse import urlencode |
| 21 | +from six.moves.urllib.parse import quote_plus |
| 22 | + |
| 23 | + |
| 24 | +class WSClient: |
| 25 | + def __init__(self, configuration, url, headers): |
| 26 | + self.messages = [] |
| 27 | + self.errors = [] |
| 28 | + websocket.enableTrace(False) |
| 29 | + header = None |
| 30 | + |
| 31 | + # We just need to pass the Authorization, ignore all the other |
| 32 | + # http headers we get from the generated code |
| 33 | + if 'Authorization' in headers: |
| 34 | + header = "Authorization: %s" % headers['Authorization'] |
| 35 | + |
| 36 | + self.ws = websocket.WebSocketApp(url, |
| 37 | + on_message=self.on_message, |
| 38 | + on_error=self.on_error, |
| 39 | + on_close=self.on_close, |
| 40 | + header=[header] if header else None) |
| 41 | + self.ws.on_open = self.on_open |
| 42 | + |
| 43 | + if url.startswith('wss://') and configuration.verify_ssl: |
| 44 | + ssl_opts = { |
| 45 | + 'cert_reqs': ssl.CERT_REQUIRED, |
| 46 | + 'keyfile': configuration.key_file, |
| 47 | + 'certfile': configuration.cert_file, |
| 48 | + 'ca_certs': configuration.ssl_ca_cert or certifi.where(), |
| 49 | + } |
| 50 | + if configuration.assert_hostname is not None: |
| 51 | + ssl_opts['check_hostname'] = configuration.assert_hostname |
| 52 | + else: |
| 53 | + ssl_opts = {'cert_reqs': ssl.CERT_NONE} |
| 54 | + |
| 55 | + self.ws.run_forever(sslopt=ssl_opts) |
| 56 | + |
| 57 | + def on_message(self, ws, message): |
| 58 | + if message[0] == '\x01': |
| 59 | + message = message[1:] |
| 60 | + if message: |
| 61 | + if six.PY3 and isinstance(message, six.binary_type): |
| 62 | + message = message.decode('utf-8') |
| 63 | + self.messages.append(message) |
| 64 | + |
| 65 | + def on_error(self, ws, error): |
| 66 | + self.errors.append(error) |
| 67 | + |
| 68 | + def on_close(self, ws): |
| 69 | + pass |
| 70 | + |
| 71 | + def on_open(self, ws): |
| 72 | + pass |
| 73 | + |
| 74 | + |
| 75 | +def GET(configuration, url, query_params, _request_timeout, headers): |
| 76 | + # switch protocols from http to websocket |
| 77 | + url = url.replace('http://', 'ws://') |
| 78 | + url = url.replace('https://', 'wss://') |
| 79 | + |
| 80 | + # patch extra / |
| 81 | + url = url.replace('//api', '/api') |
| 82 | + |
| 83 | + # Extract the command from the list of tuples |
| 84 | + commands = None |
| 85 | + for key, value in query_params: |
| 86 | + if key == 'command': |
| 87 | + commands = value |
| 88 | + break |
| 89 | + |
| 90 | + # drop command from query_params as we will be processing it separately |
| 91 | + query_params = [(key, value) for key, value in query_params if |
| 92 | + key != 'command'] |
| 93 | + |
| 94 | + # if we still have query params then encode them |
| 95 | + if query_params: |
| 96 | + url += '?' + urlencode(query_params) |
| 97 | + |
| 98 | + # tack on the actual command to execute at the end |
| 99 | + if isinstance(commands, list): |
| 100 | + for command in commands: |
| 101 | + url += "&command=%s&" % quote_plus(command) |
| 102 | + else: |
| 103 | + url += '&command=' + quote_plus(commands) |
| 104 | + |
| 105 | + client = WSClient(configuration, url, headers) |
| 106 | + if client.errors: |
| 107 | + raise ApiException( |
| 108 | + status=0, |
| 109 | + reason='\n'.join([str(error) for error in client.errors]) |
| 110 | + ) |
| 111 | + return ''.join(client.messages) |
0 commit comments