def __init__(self, apiKey=None, secretKey=None, apiMethod=None, params=None, useHTTPS=False, userKey=None): """ Constructs a request using the apiKey and secretKey. @param apiKey @param secretKey @param apiMethod the api method (including namespace) to call. for example: socialize.getUserInfo If namespaces is not supplied "socialize" is assumed @param params the request parameters @param useHTTPS useHTTPS set this to true if you want to use HTTPS. @param userKey A key of an admin user with extra permissions. If this parameter is provided, then the secretKey parameter is assumed to be the admin user's secret key and not the site's secret key. """ if apiMethod is None: return self._method = apiMethod if params is None: self._params = {} elif isinstance(params, dict): self._params = copy.copy(params) elif isinstance(params, string_types): self._params = jsonparse(params, encoding='utf-8') else: self._params = dict([k, v.encode('utf-8')] for k, v in params.items()) self._domain = self._params.get("_host", self._domain) self._useHTTPS = useHTTPS self._apiKey = apiKey self._secretKey = secretKey self._userKey = userKey self._traceLog = list() self.traceField("apiMethod", apiMethod) self.traceField("apiKey", apiKey)
def check_availability(): r = requests.get(status_url) if r.status_code == 200: status = jsonparse(r.text)['status'] if status == 'nominal': print('Online') return True print('Offline. Status:', status) else: print('Offline') return False
def api_call(url, data, process_name, auth=None, forcereturn=False): r = requests.post(url, data=data, auth=auth) # print(r.url) if r.status_code != 200: print('[ERROR] Request error during', process_name) print(' Code:', r.status_code) print(' Response:', r.text) if not forcereturn: return None try: data = jsonparse(r.text) except JSONDecodeError: data = {} return data
def __init__(self, method, responseText=None, params=None, errorCode=None, errorMessage=None, traceLog=None): self.traceLog = traceLog self.method = method if params: self.params = params else: self.params = {} if responseText: self.traceField("responseText", responseText) self.rawData = responseText.encode('utf-8') if (responseText.lstrip().find('{') != -1): self.data = jsonparse(responseText, encoding='utf-8') if self.data: self.errorCode = self.data.get("errorCode") self.errorMessage = self.data.get("errorMessage") else: matches = search(r"<errorCode>([^>]+)</errorCode>", self.rawData) if matches: errCodeStr = matches.group(1) if errCodeStr: self.errorCode = int(errCodeStr) matches = search( r"<errorMessage>([^>]+)</errorMessage>", self.rawData) if matches: self.errorMessage = matches.group(1) else: self.errorCode = errorCode self.errorMessage = errorMessage if errorMessage != None else self.getErrorMessage( ) self.populateClientResponseText() self.traceField("responseText", self.rawData)
def fetch_json(url): response = get_external(url) data = jsonparse(response) return data