예제 #1
0
    def _get_json(self, url, data=None):
        t1 = time.time()
        headers = self.custom_headers
        if data:  # POST request
            headers.update({
                'Content-type': 'application/json',
                'Accept': 'text/plain',
                'Accept': 'application/json'
            })
            response = self.requester.post(url,
                                           auth=self.auth,
                                           headers=headers,
                                           verify=self.verify_ssl,
                                           stream=True,
                                           data=json.dumps(data))
        else:
            response = self.requester.get(url,
                                          auth=self.auth,
                                          headers=headers,
                                          verify=self.verify_ssl,
                                          stream=True)

        duration = time.time() - t1
        method = "POST" if data else "GET"
        log_client_rest_api_call(url, method, duration, headers)
        if response.status_code != 200:  # Error message is text
            response.charset = "utf-8"  # To be able to access ret.text (ret.content are bytes)
            raise get_exception_from_error(response.status_code)(response.text)

        result = json.loads(decode_text(response.content))
        if not isinstance(result, dict):
            raise ConanException("Unexpected server response %s" % result)
        return result
예제 #2
0
파일: rest_client.py 프로젝트: nesono/conan
    def _get_json(self, url, data=None):
        t1 = time.time()
        headers = self.custom_headers
        if data:  # POST request
            headers.update({'Content-type': 'application/json',
                            'Accept': 'text/plain',
                            'Accept': 'application/json'})
            response = self.requester.post(url, auth=self.auth, headers=headers,
                                           verify=self.verify_ssl,
                                           stream=True,
                                           data=json.dumps(data))
        else:
            response = self.requester.get(url, auth=self.auth, headers=headers,
                                          verify=self.verify_ssl,
                                          stream=True)

        duration = time.time() - t1
        method = "POST" if data else "GET"
        log_client_rest_api_call(url, method, duration, headers)
        if response.status_code != 200:  # Error message is text
            response.charset = "utf-8"  # To be able to access ret.text (ret.content are bytes)
            raise get_exception_from_error(response.status_code)(response.text)

        result = json.loads(decode_text(response.content))
        if not isinstance(result, dict):
            raise ConanException("Unexpected server response %s" % result)
        return result
예제 #3
0
파일: rest_client.py 프로젝트: uael/conan
 def check_credentials(self):
     """If token is not valid will raise AuthenticationException.
     User will be asked for new user/pass"""
     url = "%s/users/check_credentials" % self._remote_api_url
     t1 = time.time()
     ret = self.requester.get(url, auth=self.auth, headers=self.custom_headers,
                              verify=self.verify_ssl)
     duration = time.time() - t1
     log_client_rest_api_call(url, "GET", duration, self.custom_headers)
     return ret
예제 #4
0
파일: rest_client.py 프로젝트: uael/conan
 def authenticate(self, user, password):
     '''Sends user + password to get a token'''
     auth = HTTPBasicAuth(user, password)
     url = "%s/users/authenticate" % self._remote_api_url
     t1 = time.time()
     ret = self.requester.get(url, auth=auth, headers=self.custom_headers,
                              verify=self.verify_ssl)
     duration = time.time() - t1
     log_client_rest_api_call(url, "GET", duration, self.custom_headers)
     return ret
예제 #5
0
 def authenticate(self, user, password):
     """Sends user + password to get a token"""
     auth = HTTPBasicAuth(user, password)
     url = "%s/users/authenticate" % self._remote_api_url
     t1 = time.time()
     ret = self.requester.get(url, auth=auth, headers=self.custom_headers,
                              verify=self.verify_ssl)
     if ret.status_code == 401:
         raise AuthenticationException("Wrong user or password")
     # Cannot check content-type=text/html, conan server is doing it wrong
     if not ret.ok or "html>" in str(ret.content):
         raise ConanException("%s\n\nInvalid server response, check remote URL and "
                              "try again" % str(ret.content))
     duration = time.time() - t1
     log_client_rest_api_call(url, "GET", duration, self.custom_headers)
     return ret
예제 #6
0
 def authenticate(self, user, password):
     """Sends user + password to get a token"""
     auth = HTTPBasicAuth(user, password)
     url = "%s/users/authenticate" % self._remote_api_url
     t1 = time.time()
     ret = self.requester.get(url, auth=auth, headers=self.custom_headers,
                              verify=self.verify_ssl)
     if ret.status_code == 401:
         raise AuthenticationException("Wrong user or password")
     # Cannot check content-type=text/html, conan server is doing it wrong
     if not ret.ok or "html>" in str(ret.content):
         raise ConanException("%s\n\nInvalid server response, check remote URL and "
                              "try again" % str(ret.content))
     duration = time.time() - t1
     log_client_rest_api_call(url, "GET", duration, self.custom_headers)
     return ret
예제 #7
0
 def _call_method(self, method, url, **kwargs):
     popped = False
     if self.proxies or self._no_proxy_match:
         old_env = dict(os.environ)
         # Clean the proxies from the environ and use the conan specified proxies
         for var_name in ("http_proxy", "https_proxy", "ftp_proxy", "all_proxy", "no_proxy"):
             popped = True if os.environ.pop(var_name, None) else popped
             popped = True if os.environ.pop(var_name.upper(), None) else popped
     try:
         t1 = time.time()
         all_kwargs = self._add_kwargs(url, kwargs)
         tmp = getattr(self._http_requester, method)(url, **all_kwargs)
         duration = time.time() - t1
         log_client_rest_api_call(url, method.upper(), duration, all_kwargs.get("headers"))
         return tmp
     finally:
         if popped:
             os.environ.clear()
             os.environ.update(old_env)