vtlim commented on code in PR #13787: URL: https://github.com/apache/druid/pull/13787#discussion_r1116353348
########## examples/quickstart/jupyter-notebooks/druidapi/rest.py: ########## @@ -0,0 +1,180 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import requests +from .util import dict_get, is_blank +from urllib.parse import quote +from .error import ClientError + +def check_error(response): + """ + Raises a requests HttpError if the response code is not OK or Accepted. + + If the response included a JSON payload, then the message is extracted + from that payload, else the message is from requests. The JSON + payload, if any, is returned in the json field of the error. + """ + code = response.status_code + if code == requests.codes.ok or code == requests.codes.accepted: + return + error = None + json = None + try: + json = response.json() + except Exception: + # If we can't get the JSON, just move on, we'll figure + # things out another way. + pass + msg = dict_get(json, 'errorMessage') + if msg is None: + msg = dict_get(json, 'error') + if not is_blank(msg): + raise ClientError(msg) + if code == requests.codes.not_found and error is None: + error = "Not found" + if error is not None: + response.reason = error + try: + response.raise_for_status() + except Exception as e: + e.json = json + raise e + +class DruidRestClient: + ''' + Wrapper around the basic Druid REST API operations using the + requests Python package. Handles the grunt work of building up + URLs, working with JSON, etc. + ''' + + def __init__(self, endpoint): + self.endpoint = endpoint + self.trace = False + self.session = requests.Session() + + def enable_trace(self, flag=True): + self.trace = flag + + def build_url(self, req, args=None) -> str: + """ + Returns the full URL for a REST call given the relative request API and + optional parameters to fill placeholders within the request URL. + + Parameters + ---------- + req : str + relative URL, with optional {} placeholders Review Comment: ```suggestion Relative URL, with optional {} placeholders. Example: `/status` ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
