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
|
# Copyright 2014-2022 Vincent Texier <vit@free.fr>
#
# DuniterPy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DuniterPy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import copy
import json
import logging
from http.client import HTTPResponse
from typing import Any, Callable, Dict, Optional, Union
from urllib import parse, request
import jsonschema
from websocket import WebSocket
from duniterpy.api import endpoint
from .errors import DuniterError
logger = logging.getLogger("duniter")
# Response type constants
RESPONSE_JSON = "json"
RESPONSE_TEXT = "text"
RESPONSE_HTTP = "http"
# jsonschema validator
ERROR_SCHEMA = {
"type": "object",
"properties": {"ucode": {"type": "number"}, "message": {"type": "string"}},
"required": ["ucode", "message"],
}
def parse_text(text: str, schema: dict) -> Any:
"""
Validate and parse the BMA answer from websocket
:param text: the bma answer
:param schema: dict for jsonschema
:return: the json data
"""
try:
data = json.loads(text)
jsonschema.validate(data, schema)
except (TypeError, json.decoder.JSONDecodeError) as e:
raise jsonschema.ValidationError("Could not parse json") from e
return data
def parse_error(text: str) -> dict:
"""
Validate and parse the BMA answer from websocket
:param text: the bma error
:return: the json data
"""
try:
data = json.loads(text)
jsonschema.validate(data, ERROR_SCHEMA)
except (TypeError, json.decoder.JSONDecodeError) as e:
raise jsonschema.ValidationError(f"Could not parse json : {str(e)}") from e
return data
def parse_response(response: str, schema: dict) -> Any:
"""
Validate and parse the BMA answer
:param response: Response content
:param schema: The expected response structure
:return: the json data
"""
try:
data = json.loads(response)
if schema is not None:
jsonschema.validate(data, schema)
return data
except (TypeError, json.decoder.JSONDecodeError) as exception:
raise jsonschema.ValidationError(
f"Could not parse json : {str(exception)}"
) from exception
class WSConnection:
"""
Abstraction layer on websocket library
"""
def __init__(self, connection: WebSocket) -> None:
"""
Init WSConnection instance
:param connection: Connection instance of the websocket library
"""
self.connection = connection
def send_str(self, data: str):
"""
Send a data string to the web socket connection
:param data: Data string
:return:
"""
if self.connection is None:
raise Exception("Connection property is empty")
self.connection.send(data)
def receive_str(self, timeout: Optional[float] = None) -> str:
"""
Wait for a data string from the web socket connection
:param timeout: Timeout in seconds
:return:
"""
if self.connection is None:
raise Exception("Connection property is empty")
if timeout is not None:
self.connection.settimeout(timeout)
return self.connection.recv()
def receive_json(self, timeout: Optional[float] = None) -> Any:
"""
Wait for json data from the web socket connection
:param timeout: Timeout in seconds
:return:
"""
if self.connection is None:
raise Exception("Connection property is empty")
if timeout is not None:
self.connection.settimeout(timeout)
return json.loads(self.connection.recv())
def close(self) -> None:
"""
Close the web socket connection
:return:
"""
if self.connection is None:
raise Exception("Connection property is empty")
self.connection.close()
class API:
"""
API is a class used as an abstraction layer over the http/websocket libraries.
"""
def __init__(
self,
connection_handler: endpoint.ConnectionHandler,
headers: Optional[dict] = None,
) -> None:
"""
Asks a module in order to create the url used then by derivated classes.
:param connection_handler: Connection handler
:param headers: Headers dictionary (optional, default None)
"""
self.connection_handler = connection_handler
self.headers = {} if headers is None else headers
def reverse_url(self, scheme: str, path: str) -> str:
"""
Reverses the url using scheme and path given in parameter.
:param scheme: Scheme of the url
:param path: Path of the url
:return:
"""
# remove starting slash in path if present
path = path.lstrip("/")
address, port = self.connection_handler.address, self.connection_handler.port
if self.connection_handler.path:
url = f"{scheme}://{address}:{port}/{self.connection_handler.path}"
else:
url = f"{scheme}://{address}:{port}"
if len(path.strip()) > 0:
return f"{url}/{path}"
return url
def request_url(
self,
path: str,
method: str = "GET",
rtype: str = RESPONSE_JSON,
schema: Optional[dict] = None,
json_data: Optional[dict] = None,
bma_errors: bool = False,
**kwargs: Any,
) -> Any:
"""
Requests wrapper in order to use API parameters.
:param path: the request path
:param method: Http method 'GET' or 'POST' (optional, default='GET')
:param rtype: Response type (optional, default RESPONSE_JSON, can be RESPONSE_TEXT, RESPONSE_HTTP)
:param schema: Json Schema to validate response (optional, default None)
:param json_data: Json data as dict (optional, default None)
:param bma_errors: Set it to True to handle Duniter Error Response (optional, default False)
:return:
"""
logging.debug(
"Request: %s", self.reverse_url(self.connection_handler.http_scheme, path)
)
url = self.reverse_url(self.connection_handler.http_scheme, path)
duniter_request = request.Request(url, method=method)
if kwargs:
# urlencoded http form content as bytes
duniter_request.data = parse.urlencode(kwargs).encode("utf-8")
logging.debug("%s : %s, data=%s", method, url, duniter_request.data)
if json_data:
# json content as bytes
duniter_request.data = json.dumps(json_data).encode("utf-8")
logging.debug("%s : %s, data=%s", method, url, duniter_request.data)
# http header to send json body
self.headers["Content-Type"] = "application/json"
if self.headers:
duniter_request.headers = self.headers
if self.connection_handler.proxy:
# proxy host
duniter_request.set_proxy(
self.connection_handler.proxy, self.connection_handler.http_scheme
)
with request.urlopen(
duniter_request, timeout=15
) as response: # type: HTTPResponse
if response.status != 200:
content = response.read().decode("utf-8")
if bma_errors:
try:
error_data = parse_error(content)
raise DuniterError(error_data)
except (TypeError, jsonschema.ValidationError) as exception:
raise ValueError(
f"status code != 200 => {response.status} ({content})"
) from exception
raise ValueError(f"status code != 200 => {response.status} ({content})")
# get response content
return_response = copy.copy(response)
content = response.read().decode("utf-8")
# if schema supplied...
if schema is not None:
# validate response
parse_response(content, schema)
# return the chosen type
result = return_response # type: Any
if rtype == RESPONSE_TEXT:
result = content
elif rtype == RESPONSE_JSON:
result = json.loads(content)
return result
def connect_ws(self, path: str) -> WSConnection:
"""
Connect to a websocket
:param path: the url path
:return:
"""
url = self.reverse_url(self.connection_handler.ws_scheme, path)
ws = WebSocket()
if self.connection_handler.proxy:
proxy_split = ":".split(self.connection_handler.proxy)
if len(proxy_split) == 2:
host = proxy_split[0]
port = int(proxy_split[1])
else:
host = self.connection_handler.proxy
port = 80
ws.connect(url, http_proxy_host=host, http_proxy_port=port)
else:
ws.connect(url)
return WSConnection(ws)
class Client:
"""
Main class to create an API client
"""
def __init__(
self,
_endpoint: Union[str, endpoint.Endpoint],
proxy: Optional[str] = None,
) -> None:
"""
Init Client instance
:param _endpoint: Endpoint string in duniter format
:param proxy: Proxy address as hostname:port (optional, default None)
"""
if isinstance(_endpoint, str):
# Endpoint Protocol detection
self.endpoint = endpoint.endpoint(_endpoint)
else:
self.endpoint = _endpoint
if isinstance(self.endpoint, endpoint.UnknownEndpoint):
raise NotImplementedError(f"{self.endpoint.api} endpoint in not supported")
self.proxy = proxy
def get(
self,
url_path: str,
params: Optional[dict] = None,
rtype: str = RESPONSE_JSON,
schema: Optional[dict] = None,
) -> Any:
"""
GET request on endpoint host + url_path
:param url_path: Url encoded path following the endpoint
:param params: Url query string parameters dictionary (optional, default None)
:param rtype: Response type (optional, default RESPONSE_JSON, can be RESPONSE_TEXT, RESPONSE_HTTP)
:param schema: Json Schema to validate response (optional, default None)
:return:
"""
if params is None:
params = {}
client = API(self.endpoint.conn_handler(self.proxy))
# get response
return client.request_url(
url_path, "GET", rtype, schema, bma_errors=True, **params
)
def post(
self,
url_path: str,
params: Optional[dict] = None,
rtype: str = RESPONSE_JSON,
schema: Optional[dict] = None,
) -> Any:
"""
POST request on endpoint host + url_path
:param url_path: Url encoded path following the endpoint
:param params: Url query string parameters dictionary (optional, default None)
:param rtype: Response type (optional, default RESPONSE_JSON, can be RESPONSE_TEXT, RESPONSE_HTTP)
:param schema: Json Schema to validate response (optional, default None)
:return:
"""
if params is None:
params = {}
client = API(self.endpoint.conn_handler(self.proxy))
# get response
return client.request_url(
url_path, "POST", rtype, schema, bma_errors=True, **params
)
def query(
self,
query: str,
variables: Optional[dict] = None,
schema: Optional[dict] = None,
) -> Any:
"""
GraphQL query or mutation request on endpoint
:param query: GraphQL query string
:param variables: Variables for the query (optional, default None)
:param schema: Json Schema to validate response (optional, default None)
:return:
"""
payload = {"query": query} # type: Dict[str, Union[str, dict]]
if variables is not None:
payload["variables"] = variables
client = API(self.endpoint.conn_handler(self.proxy))
# get json response
response = client.request_url(
"", "POST", rtype=RESPONSE_JSON, schema=schema, json_data=payload
)
# if schema supplied...
if schema is not None:
# validate response
parse_response(response, schema)
return response
def connect_ws(self, path: str = "") -> WSConnection:
"""
Connect to a websocket in order to use API parameters
:param path: the url path
:return:
"""
client = API(self.endpoint.conn_handler(self.proxy))
return client.connect_ws(path)
def __call__(self, _function: Callable, *args: Any, **kwargs: Any) -> Any:
"""
Call the _function given with the args given
So we can call many packages wrapping the REST API
:param _function: The function to call
:param args: The parameters
:param kwargs: The key/value parameters
:return:
"""
return _function(self, *args, **kwargs)
|