def query(method, url, server=None, token=None, params=None, json=False): if method not in ("GET", "POST"): raise exceptions.QueryException() server_url = get_server(server) if not server_url: raise exceptions.QueryException() full_url = server_url + url headers = get_headers(token, json) log('network').info("[query] %s %s%s%s", method, full_url, headers, params) # issue request try: params = build_params(method, params) if method == "GET": request = urllib2.Request(full_url + "?" + params, headers=headers) elif method == "POST": request = urllib2.Request(full_url, data=params, headers=headers) response = opener.open(request) # return response response_obj = response.read() log('network').info("[response] %s", response_obj) if json: response_obj = loads(response_obj) return response_obj except Exception as ex: exceptions.factory(ex)
def query(method, url, server=None, token=None, params=None, json=False): if method not in ("GET", "POST"): raise exceptions.QueryException() server_url = get_server(server) full_url = server_url + url headers = get_headers(token, json) logger('network').info("[query] %s%s%s", full_url, headers, params) # issue request try: if method == "GET": if params: full_url += "?" + urllib.urlencode(params) request = urllib2.Request(full_url, headers=headers) elif method == "POST": if not params: params = "" elif json: params = dumps(params) request = urllib2.Request(full_url, data=params, headers=headers) response = opener.open(request) # return response return_obj = loads(response.read()) if json else response.read() logger('network').info("[response] %s", return_obj) return return_obj except Exception as ex: import traceback logger('network').error("%s", traceback.format_exc()) rematch_ex = exceptions.factory(ex) logger('network').debug(rematch_ex) raise rematch_ex
def get_server(server): """getting and finalzing server address""" if not server: if 'server' not in config or not config['server']: raise exceptions.QueryException() server = config['server'] if not (server.startswith("http://") or server.startswith("http://")): server = "http://" + server if not server.endswith("/"): server = server + "/" return server
def get_server(server): """getting and finalzing server address""" try: if not server: if 'server' not in config and not config['login']['server']: raise exceptions.QueryException() server = config['login']['server'] if not (server.startswith("http://") or server.startswith("http://")): server = "http://" + server if not server.endswith("/"): server = server + "/" except Exception: log('network').exception("Failed generating server aadress") return server
def get_server(server): """getting and finalzing server address""" try: if not server: if 'server' not in config and not config['login']['server']: raise exceptions.QueryException() server = config['login']['server'] if not (server.startswith("http://") or server.startswith("http://")): server = "http://" + server if not server.endswith("/"): server = server + "/" except Exception: import traceback logger('network').error(traceback.format_exc()) return server
def query(method, url, server=None, token=None, params=None, json=False): if method not in ("GET", "POST"): raise exceptions.QueryException() full_url = get_server(server) + url headers = get_headers(token, json) logger('network').info("[query] {full_url}{headers}{params}" "".format(full_url=full_url, headers=headers, params=params)) # issue request try: if method == "GET": if params: full_url += "?" + urllib.urlencode(params) request = urllib2.Request(full_url, headers=headers) elif method == "POST": if not params: params = "" elif json: params = dumps(params) request = urllib2.Request(full_url, data=params, headers=headers) response = opener.open(request) # return response return_obj = loads(response.read()) if json else response.read() logger('network').info("[response] {}".format(return_obj)) return return_obj except urllib2.HTTPError as ex: logger('network').debug(ex) logger('network').debug(ex.read()) logger('network').debug(ex.__dict__) if ex.code in (500,): raise exceptions.ServerException() if ex.code in (401,): raise exceptions.AuthenticationException() if ex.code in (404,): raise exceptions.NotFoundException() raise except urllib2.URLError as ex: raise exceptions.ConnectionException()