Пример #1
0
    def _check(self, instance):
        addr, username, password, timeout, include_content, headers, response_time, tags, disable_ssl_validation, ssl_expire = self._load_conf(instance)
        content = ''
        start = time.time()

        service_checks = []
        resp = None

        try:
            self.log.debug("Connecting to %s" % addr)
            if disable_ssl_validation and urlparse(addr)[0] == "https":
                self.warning("Skipping SSL certificate validation for %s based on configuration" % addr)
            h = Http(timeout=timeout, disable_ssl_certificate_validation=disable_ssl_validation)
            if username is not None and password is not None:
                h.add_credentials(username, password)
            resp, content = h.request(addr, "GET", headers=headers)

        except socket.timeout, e:
            length = int((time.time() - start) * 1000)
            self.log.info("%s is DOWN, error: %s. Connection failed after %s ms" % (addr, str(e), length))
            service_checks.append((
                self.SC_STATUS,
                Status.DOWN,
                "%s. Connection failed after %s ms" % (str(e), length)
            ))
Пример #2
0
def create_item():

    USER = credentials['USER']
    PASSWORD = credentials['PASSWORD']

    http = Http('.cache')
    http.add_credentials(USER, PASSWORD)
    url = 'http://survey.houseofkoffler.com/jsonapi/node/article'
    headers = {
        'Content-type': 'application/vnd.api+json',
        'Accept': 'application/vnd.api+json'
    }

    post_data = {
        "data": {
            "type": "node--article",
            "attributes": {
                "title": "My custom title",
                "body": {
                    "value": "Custom value",
                    "format": "plain_text"
                }
            }
        }
    }

    resp, content = http.request(uri=url,
                                 method='POST',
                                 headers=headers,
                                 body=json.dumps(post_data))

    print(resp)
Пример #3
0
def get_data(*args):
    #TODO Pass in a timeout value here
    from httplib2 import Http
    from urllib import urlencode
    req = Http(timeout=10)
    if (Prefs['authrequired']):
        req.add_credentials(Prefs['username'], Prefs['password'])
    results = []
    for item in args:
        u = item[0]
        data = item[1]
        print data
        try:
            if data:
                headers = {'Content-type': 'application/x-www-form-urlencoded'}
                resp, content = req.request(u, "POST", headers=headers, body=urlencode(data))
                print resp, content
            else:
                resp, content = req.request(u, "GET", data)
            soup = BeautifulSoup(content)
            results.append(soup)
        except:

            raise
    return results
def build_http_connection(config, timeout=120, disable_ssl_validation=False):
    """
    :config: dict like, proxy and account information are in the following
             format {
                 "username": xx,
                 "password": yy,
                 "proxy_url": zz,
                 "proxy_port": aa,
                 "proxy_username": bb,
                 "proxy_password": cc,
                 "proxy_type": http,http_no_tunnel,sock4,sock5,
                 "proxy_rdns": 0 or 1,
             }
    :return: Http2.Http object
    """

    proxy_type_to_code = {
        "http": socks.PROXY_TYPE_HTTP,
        "http_no_tunnel": socks.PROXY_TYPE_HTTP_NO_TUNNEL,
        "socks4": socks.PROXY_TYPE_SOCKS4,
        "socks5": socks.PROXY_TYPE_SOCKS5,
    }
    if config.get("proxy_type") in proxy_type_to_code:
        proxy_type = proxy_type_to_code[config["proxy_type"]]
    else:
        proxy_type = socks.PROXY_TYPE_HTTP

    rdns = scu.is_true(config.get("proxy_rdns"))

    proxy_info = None
    if config.get("proxy_url") and config.get("proxy_port"):
        if config.get("proxy_username") and config.get("proxy_password"):
            proxy_info = ProxyInfo(
                proxy_type=proxy_type,
                proxy_host=config["proxy_url"],
                proxy_port=int(config["proxy_port"]),
                proxy_user=config["proxy_username"],
                proxy_pass=config["proxy_password"],
                proxy_rdns=rdns,
            )
        else:
            proxy_info = ProxyInfo(
                proxy_type=proxy_type,
                proxy_host=config["proxy_url"],
                proxy_port=int(config["proxy_port"]),
                proxy_rdns=rdns,
            )
    if proxy_info:
        http = Http(
            proxy_info=proxy_info,
            timeout=timeout,
            disable_ssl_certificate_validation=disable_ssl_validation,
        )
    else:
        http = Http(timeout=timeout,
                    disable_ssl_certificate_validation=disable_ssl_validation)

    if config.get("username") and config.get("password"):
        http.add_credentials(config["username"], config["password"])
    return http
def get_current_events(feed_url_or_path, files):
    """Retrieves data from iCal iCal feed and returns an ics.Calendar object 
    containing the parsed data.

    Returns the parsed Calendar object or None if an error occurs.
    """

    events_end = datetime.now()
    if config.get('ICAL_DAYS_TO_SYNC', 0) == 0:
        # default to 1 year ahead
        events_end += DEFAULT_TIMEDELTA
    else:
        # add on a number of days
        events_end += timedelta(days=config['ICAL_DAYS_TO_SYNC'])

    try:
        if files:
            cal = events(file=feed_url_or_path, end=events_end)
        else:
            # directly configure http connection object to set ssl and authentication parameters
            http_conn = Http(disable_ssl_certificate_validation=not config.get('ICAL_FEED_VERIFY_SSL_CERT', True))

            if config.get('ICAL_FEED_USER') and config.get('ICAL_FEED_PASS'):
                # credentials used for every connection
                http_conn.add_credentials(name=config.get('ICAL_FEED_USER'), password=config.get('ICAL_FEED_PASS'))

            cal = events(feed_url_or_path, start=datetime.now()-timedelta(days=config.get('PAST_DAYS_TO_SYNC', 0)), end=events_end, http=http_conn)
    except Exception as e:
        logger.error('> Error retrieving iCal data ({})'.format(e))
        return None

    return cal
Пример #6
0
def api_get(url, acnt, params=None):
    #does a simple get request to the api.
    http = Http(disable_ssl_certificate_validation=True)
    http.add_credentials(acnt['user'], acnt['pass'])
    headers = { 'Content-type': 'application/json', }
    resp, content = http.request(url, "GET", headers=headers)
    return content
Пример #7
0
    def _check(self, instance):
        addr, username, password, timeout, include_content, headers, response_time, tags, disable_ssl_validation, ssl_expire = self._load_conf(
            instance)
        content = ''
        start = time.time()

        service_checks = []
        resp = None

        try:
            self.log.debug("Connecting to %s" % addr)
            if disable_ssl_validation and urlparse(addr)[0] == "https":
                self.warning(
                    "Skipping SSL certificate validation for %s based on configuration"
                    % addr)
            h = Http(timeout=timeout,
                     disable_ssl_certificate_validation=disable_ssl_validation)
            if username is not None and password is not None:
                h.add_credentials(username, password)
            resp, content = h.request(addr, "GET", headers=headers)

        except socket.timeout, e:
            length = int((time.time() - start) * 1000)
            self.log.info(
                "%s is DOWN, error: %s. Connection failed after %s ms" %
                (addr, str(e), length))
            service_checks.append(
                (self.SC_STATUS, Status.DOWN,
                 "%s. Connection failed after %s ms" % (str(e), length)))
Пример #8
0
 def savedsearch(self,q=""):
     """
     Runs one of your saved searches
     """
     query=Http(timeout=10)
     query.add_credentials(self.username,self.password)
     resp, cont=query.request("http://"+self.subdomain+".loggly.com/api/savedsearches","GET")
     content=loads(cont)
     saved=None
     for search in content:
         if search['name']==q:
            saved=search
     if saved==None:
         raise ValueError("Your account does not have a search of that name,\
         please go to "+self.subdomain+".loggly.com to check your saved searches")
     params=saved['context']
     opts={}
     inputs=""
     devices=""
     for x in params:
         if x!="terms" and x!="inputs" and x!="devices":
             opts[self.ssdict[x]]=params[x]
     if params['inputs']:
         inputs+=" AND (inputname:"+" OR inputname:".join(params['inputs'])+")"
     if params['devices']:
         devices+=" AND (ip:"+" OR ip:".join(params['devices'])+")"
     return self.search(q=params['terms']+inputs+devices,**opts)
Пример #9
0
def build_http_connection(config, timeout=120):
    """
    @config: dict like, proxy and account information are in the following
             format {
                 "username": xx,
                 "password": yy,
                 "proxy_url": zz,
                 "proxy_port": aa,
                 "proxy_username": bb,
                 "proxy_password": cc,
             }
    @return: Http2.Http object
    """

    proxy_info = None
    if config.get("proxy_url") and config.get("proxy_port"):
        if config.get("proxy_username") and config.get("proxy_password"):
            proxy_info = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=config["proxy_port"],
                                   proxy_user=config["proxy_username"],
                                   proxy_pass=config["proxy_password"])
        else:
            proxy_info = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=config["proxy_port"])
    http = Http(proxy_info=proxy_info, timeout=timeout,
                disable_ssl_certificate_validation=True)
    if config.get("username") and config.get("password"):
        http.add_credentials(config["username"], config["password"])
    return http
Пример #10
0
def build_http_connection(config, timeout=120):
    """
    @config: dict like, proxy and account information are in the following
             format {
                 "username": xx,
                 "password": yy,
                 "proxy_url": zz,
                 "proxy_port": aa,
                 "proxy_username": bb,
                 "proxy_password": cc,
             }
    @return: Http2.Http object
    """

    proxy_info = None
    if config.get("proxy_url") and config.get("proxy_port"):
        if config.get("proxy_username") and config.get("proxy_password"):
            proxy_info = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=config["proxy_port"],
                                   proxy_user=config["proxy_username"],
                                   proxy_pass=config["proxy_password"])
        else:
            proxy_info = ProxyInfo(proxy_type=socks.PROXY_TYPE_HTTP,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=config["proxy_port"])
    http = Http(proxy_info=proxy_info,
                timeout=timeout,
                disable_ssl_certificate_validation=True)
    if config.get("username") and config.get("password"):
        http.add_credentials(config["username"], config["password"])
    return http
Пример #11
0
def get_data(*args):
    #TODO Pass in a timeout value here
    from httplib2 import Http
    from urllib import urlencode
    req = Http(timeout=10)
    if (Prefs['authrequired']):
        req.add_credentials(Prefs['username'], Prefs['password'])
    results = []
    for item in args:
        u = item[0]
        data = item[1]
        print data
        try:
            if data:
                headers = {'Content-type': 'application/x-www-form-urlencoded'}
                resp, content = req.request(u,
                                            "POST",
                                            headers=headers,
                                            body=urlencode(data))
                print resp, content
            else:
                resp, content = req.request(u, "GET", data)
            soup = BeautifulSoup(content)
            results.append(soup)
        except:

            raise
    return results
Пример #12
0
    def _make_request(self, url, method, payload={}, headers={}):
        """
        A wrapper around httplib2.Http.request.
        
        Required Arguments:
        
            url
                The url of the request.
                
            method
                The method of the request. I.e. 'GET', 'PUT', 'POST', 'DELETE'.
            
        Optional Arguments:
            
            payload
                The urlencoded parameters.
            
            headers
                Additional headers of the request.
                
        """
        try:
            if self._meta.ignore_ssl_validation:
                http = Http(disable_ssl_certificate_validation=True)
            else:
                http = Http()

            if self._auth_credentials:
                http.add_credentials(self._auth_credentials[0], self._auth_credentials[1])

            return http.request(url, method, payload, headers=headers)
        except socket.error as e:
            raise exc.dRestAPIError(e.args[1])
Пример #13
0
 def findsavedsearchnames(self):
     query=Http(timeout=10)
     query.add_credentials(self.username,self.password)
     resp, cont=query.request("http://"+self.subdomain+".loggly.com/api/savedsearches","GET")
     content=loads(cont)
     names=[x['name'] for x in content]
     return names
Пример #14
0
 def test_authentication(self):
     url = address + '/token'
     h = Http()
     h.add_credentials('unit_test', 'unit_test')
     resp, result = h.request(url,
                              'POST',
                              headers={"Content-Type": "application/json"})
     self.assertEqual(resp['status'], '200')
Пример #15
0
def _http():
    """Shortcut for httplib2.Http with module-specific options."""
    ret = Http(**_HTTPLIB2_OPTIONS)
    for username, password in _HTTPLIB2_CREDENTIALS:
        ret.add_credentials(username, password)
    for key, cert, domain in _HTTPLIB2_CERTIFICATES:
        ret.add_certificate(key, cert, credentials)
    return ret
Пример #16
0
def request(url, meth, data='', user=None, passwd=None):
    headers={'content-type' : 'application/x-www-form-urlencoded'}
    h = Http()
    if user:
        # send the header is ('authorization', 'Basic user:pwd') in base64
        # the server should see REMOTE_USER and HTTP_AUTHORIZATION
        h.add_credentials(user, passwd)
    return h.request(url, meth, urlencode(data), headers)
Пример #17
0
def _http():
    """Shortcut for httplib2.Http with module-specific options."""
    ret = Http(**_HTTPLIB2_OPTIONS)
    for username, password in _HTTPLIB2_CREDENTIALS:
        ret.add_credentials(username, password)
    for key, cert, domain in _HTTPLIB2_CERTIFICATES:
        ret.add_certificate(key, cert, domain)
    return ret
Пример #18
0
class Session(object):

    def __init__(self, op_config):
        self.op_config = op_config
        self.http = Http(disable_ssl_certificate_validation=True)
        self.http.follow_all_redirects = True
        self.http.add_credentials(self.op_config.username,
                                  self.op_config.password)
        response = self.get('/')
        self.api_version = response.get('api_version')
        if self.api_version not in ['1']:
            raise HTTPError('Invalid API version: %s' % self.api_version)
        self.languages = response.get('languages', {})

    def request(self, method, url, data=None, params=None):
        data = data or {}
        params = params or {}
        headers = {'Accept': 'application/json',
                   'Content-Type': 'application/json',
                   'User-Agent': 'op/%s' % __version__}
        if 'language' in data:
            if data['language'] is not None \
               and data['language'] not in self.languages:
                raise HTTPError('Invalid language: %s' % data['language'])
        data = json.dumps(data)
        url = self.op_config.base_url + url.rstrip('/') + '/'
        query_string_params = {}
        for key, value in params.iteritems():
            if value is not None:
                query_string_params[key] = value
        if len(query_string_params):
            url += '?' + urlencode(query_string_params)
        response_headers, response = \
            self.http.request(url, method=method.upper(), body=data,
                              headers=headers)
        if response_headers['content-type'] != 'application/json':
            raise HTTPError('No application/json response found! Please ' \
                            'verify your ownpaste server URL!', url=url)
        try:
            data = json.loads(response)
        except ValueError:
            raise HTTPError('Failed to parse JSON response')
        if data['status'] == 'ok':
            return data
        raise HTTPError(data['error'],
                        status_code=int(response_headers['status']), url=url)

    def get(self, url, data=None, params=None):
        return self.request('GET', url, data, params)

    def post(self, url, data=None, params=None):
        return self.request('POST', url, data, params)

    def patch(self, url, data=None, params=None):
        return self.request('PATCH', url, data, params)

    def delete(self, url, data=None, params=None):
        return self.request('DELETE', url, data, params)
Пример #19
0
def build_http_connection(config, timeout=120, disable_ssl_validation=False):
    """
    :config: dict like, proxy and account information are in the following
             format {
                 "username": xx,
                 "password": yy,
                 "proxy_url": zz,
                 "proxy_port": aa,
                 "proxy_username": bb,
                 "proxy_password": cc,
                 "proxy_type": http,http_no_tunnel,sock4,sock5,
                 "proxy_rdns": 0 or 1,
             }
    :return: Http2.Http object
    """

    proxy_type_to_code = {
        "http": socks.PROXY_TYPE_HTTP,
        "http_no_tunnel": socks.PROXY_TYPE_HTTP_NO_TUNNEL,
        "socks4": socks.PROXY_TYPE_SOCKS4,
        "socks5": socks.PROXY_TYPE_SOCKS5,
    }
    if config.get("proxy_type") in proxy_type_to_code:
        proxy_type = proxy_type_to_code[config["proxy_type"]]
    else:
        proxy_type = socks.PROXY_TYPE_HTTP

    rdns = scu.is_true(config.get("proxy_rdns"))

    proxy_info = None
    if config.get("proxy_url") and config.get("proxy_port"):
        if config.get("proxy_username") and config.get("proxy_password"):
            proxy_info = ProxyInfo(proxy_type=proxy_type,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=int(config["proxy_port"]),
                                   proxy_user=config["proxy_username"],
                                   proxy_pass=config["proxy_password"],
                                   proxy_rdns=rdns)
        else:
            proxy_info = ProxyInfo(proxy_type=proxy_type,
                                   proxy_host=config["proxy_url"],
                                   proxy_port=int(config["proxy_port"]),
                                   proxy_rdns=rdns)
    if proxy_info:
        http = Http(proxy_info=proxy_info,
                    timeout=timeout,
                    disable_ssl_certificate_validation=disable_ssl_validation)
    else:
        http = Http(timeout=timeout,
                    disable_ssl_certificate_validation=disable_ssl_validation)

    if config.get("username") and config.get("password"):
        http.add_credentials(config["username"], config["password"])
    return http
Пример #20
0
 def get_stats(self, metric, dimension=None, filters=None):
     h = Http('test-scripts/.cache')
     h.add_credentials(self.email, self.password)
     if dimension:
         url = "https://api.addthis.com/analytics/1.0/pub/%s/%s.json" % (metric, dimension)
     else:
         url = "https://api.addthis.com/analytics/1.0/pub/%s.json" % metric
     data = dict(("pubid", self.pubid) + filters.items()) if \
         filters else {"pubid": self.pubid}
     resp, content = h.request("%s?%s" % (url, urlencode(data)), "GET")
     return content
Пример #21
0
def request_rest_process(process, params): 
    """
    Make a REST request for the WPS process named, with the provided parameters.
    """
    url = "%srest/process/%s" % (settings.GEOSERVER_BASE_URL, process)
    user, pw = settings.GEOSERVER_CREDENTIALS
    http = Http()
    http.add_credentials(user, pw)
    response, content = http.request(url, "POST", json.dumps(params))
    if (response.status is not 200):
        raise Exception("REST process responded with\nStatus: {0}\nResponse:\n{1}".format(response.status, content))
    return json.loads(content)
Пример #22
0
class SimpleRestClient:
    """A simple REST client library"""
    def __init__(self, api_url, api_user, api_password):
        self.url, self.user, self.password = api_url, api_user, api_password
        self.client = Http()
        self.client.follow_all_redirect = True
        self.client.add_credentials(self.user, self.password)

    def GET(self, uri):
        if uri.startswith('http://'):
            current_url = ''
        else:
            current_url = self.url
        status, response = self.client.request(
            '{url}{uri}'.format(url=current_url, uri=uri),
            'GET',
            headers={'accept': 'application/xml'})
        response = self.parse_xml(response)
        return status, response

    def POST(self, uri, params={}):
        if uri.startswith('http://'):
            current_url = ''
        else:
            current_url = self.url
        if not params:
            params = {}
        status, response = self.client.request(
            '{url}{uri}'.format(url=current_url, uri=uri),
            'POST',
            urlencode(params),
            headers={'accept': 'application/xml'})
        response = self.parse_xml(response)
        return status, response

    def DELETE(self, uri):
        if uri.startswith('http://'):
            current_url = ''
        else:
            current_url = self.url
        return self.client.request(
            '{url}{uri}'.format(url=current_url, uri=uri), 'DELETE')

    def PUT(self, uri):
        if uri.startswith('http://'):
            current_url = ''
        else:
            current_url = self.url
        return self.client.request(
            '{url}{uri}'.format(url=current_url, uri=uri), 'PUT')

    def parse_xml(self, response):
        return libxml2.parseDoc(response)
Пример #23
0
class SimpleRestClient:
    """A simple REST client library"""

    def __init__(self, api_url, api_user, api_password):
        self.url, self.user, self.password = api_url, api_user, api_password
        self.client = Http()
        self.client.follow_all_redirect = True
        self.client.add_credentials(self.user, self.password)

    def GET(self, uri):
        if uri.startswith("http://"):
            current_url = ""
        else:
            current_url = self.url
        status, response = self.client.request(
            "{url}{uri}".format(url=current_url, uri=uri), "GET", headers={"accept": "application/xml"}
        )
        response = self.parse_xml(response)
        return status, response

    def POST(self, uri, params={}):
        if uri.startswith("http://"):
            current_url = ""
        else:
            current_url = self.url
        if not params:
            params = {}
        status, response = self.client.request(
            "{url}{uri}".format(url=current_url, uri=uri),
            "POST",
            urlencode(params),
            headers={"accept": "application/xml"},
        )
        response = self.parse_xml(response)
        return status, response

    def DELETE(self, uri):
        if uri.startswith("http://"):
            current_url = ""
        else:
            current_url = self.url
        return self.client.request("{url}{uri}".format(url=current_url, uri=uri), "DELETE")

    def PUT(self, uri):
        if uri.startswith("http://"):
            current_url = ""
        else:
            current_url = self.url
        return self.client.request("{url}{uri}".format(url=current_url, uri=uri), "PUT")

    def parse_xml(self, response):
        return libxml2.parseDoc(response)
Пример #24
0
def wowlog():
    """Query user connections info from Wowza streaming server and save to DB.
    """

    h = Http()
    h.add_credentials(login, password)

    root = etree.fromstring(h.request('http://' + server_ip + ':' + server_port +
                            '/connectioncounts/')[1])

    value = int(root[0].text)
    WowzaConnections.objects.create(query_time=datetime.now(), conn_counts=value)

    return str(value)
Пример #25
0
 def test_add_new_update(self):
     url = address + '/version'
     h = Http()
     h.add_credentials('unit_test', 'unit_test')
     data = dict(versionNumber="3.0.0",
                 nameUpdate="Version 3",
                 newFeatures="None",
                 bugFixes="None")
     data = json.dumps(data)
     resp, result = h.request(url,
                              'POST',
                              body=data,
                              headers={"Content-Type": "application/json"})
     self.assertEqual(resp['status'], '200')
Пример #26
0
    def send_http_request(self, url, basic_auth_username, basic_auth_password):
        h = Http(disable_ssl_certificate_validation=True)
        if basic_auth_username and basic_auth_password:
            h.add_credentials(basic_auth_username, basic_auth_password)

        project = self.video.get_team_video().project.slug if self.video else None
        data = {
            'event': self.event_name,
            'api_url': self.api_url,
        }
        if self.team:
            data['team'] = self.team.slug
        if self.partner:
            data['partner'] = self.partner.slug
        if project:
            data['project'] = project
        if self.video:
            data['video_id'] = self.video_id
        if self.application_pk:
            data['application_id'] = self.application_pk
        if self.language:
            data.update({
                "language_code": self.language_code,
                "language_id": self.language.pk,
            })
        data_sent = data
        data = urlencode(data)
        url = "%s?%s" % (url , data)
        try:
            resp, content = h.request(url, method="POST", body=data, headers={
                'referer': '%s://%s' % (DEFAULT_PROTOCOL, Site.objects.get_current().domain)
            })
            success = 200 <= resp.status < 400
            if success is False:
                logger.error("Failed to notify team %s " % (self.team),
                     extra={
                        'team': self.team or self.partner,
                        'url': url,
                        'response': resp,
                        'content': content,
                        'data_sent':data_sent,
                    })

                Meter('http-callback-notification-error').inc()
            else:
                Meter('http-callback-notification-success').inc()
            return success, content
        except:
            logger.exception("Failed to send http notification ")
        return None, None
Пример #27
0
 def test_get_all_users(self):
     create_user('stijn', 'test', '*****@*****.**')
     h = Http()
     h.add_credentials(name='stijn', password='******')
     url = 'http://localhost:5000/api/v1/users'
     try:
         response, content_bytes = h.request(url, method='GET')
         if response['status'] != '201' and response['status'] != '200':
             raise Exception('Received an unsuccessful status code of %s' %
                             response['status'])
         content = json.loads(content_bytes.decode())
         self.assertTrue(content.get('users'))
     except Exception as e:
         self.assertTrue(False, 'Received an unsuccessful status code of %s' %
                             response['status'])
def post_comment(message_id, comment):
    """
    Posts new comment to Basecamp message
    """
    h = Http()
    h.add_credentials(BASECAMP_USER, BASECAMP_PASSWORD)
    url = '%s/posts/%s/comments.xml' % (BASECAMP_URL, message_id)
    req = ET.Element('comment')
    ET.SubElement(req, 'body').text = str(comment)
    headers = {
        'Content-Type': 'application/xml',
        'Accept': 'application/xml',
    }
    response = h.request(url, method="POST", body=ET.tostring(req), headers=headers)
    print response
Пример #29
0
 def test_delete_request(self):
     user = create_user('stijn', 'test', '*****@*****.**')
     request = create_request('stijn', 'test', False)
     self.assertTrue(request.get('id'))
     try:
         h = Http()
         h.add_credentials(name='stijn', password='******')
         url = 'http://localhost:5000/api/v1/requests/%s' % request['id']
         response, content_bytes = h.request(url, method='DELETE')
         content = json.loads(content_bytes.decode())
         self.assertFalse(content.get(request['id']))
     except Exception as err:
         self.assertTrue(False,
                         'Received an unsuccessful status code of %s' %
                         response['status'])
Пример #30
0
def get_item(title):

    USER = credentials['USER']
    PASSWORD = credentials['PASSWORD']

    http = Http('.cache')
    http.add_credentials(USER, PASSWORD)
    url = 'http://survey.houseofkoffler.com/jsonapi/node/article?filter[article-title][path]=title&filter[article-title][value]=%s&filter[article-title][operator]==' % title
    headers = {
        'Content-type': 'application/vnd.api+json',
        'Accept': 'application/vnd.api+json'
    }

    resp, content = http.request(uri=url, method='GET', headers=headers)

    print(resp)
Пример #31
0
class SimpleRestClient:
    """A simple REST client library"""
    
    def __init__(self, api_url, api_user, api_password):
      self.url, self.user, self.password = api_url, api_user, api_password
      self.client = Http()
      self.client.follow_all_redirect = True
      self.client.add_credentials(self.user, self.password)

    def GET(self, uri):
      if uri.startswith('http://'):
	current_url = ''
      else:
	current_url = self.url
      status, response = self.client.request('{url}{uri}'.format(url=current_url, uri=uri), 'GET', headers={'accept':'application/xml'})
      response = self.parse_xml(response)
      return status, response

    def POST(self, uri, params={}):
      if uri.startswith('http://'):
	current_url = ''
      else:
	current_url = self.url
      if not params:
	params = {}
      status, response = self.client.request('{url}{uri}'.format(url=current_url, uri=uri), 'POST',
	  urlencode(params), headers={'accept':'application/xml'})
      response = self.parse_xml(response)
      return status, response

    def DELETE(self, uri):
      if uri.startswith('http://'):
	current_url = ''
      else:
	current_url = self.url
      return self.client.request('{url}{uri}'.format(url=current_url, uri=uri), 'DELETE')

    def PUT(self, uri):
      if uri.startswith('http://'):
	current_url = ''
      else:
	current_url = self.url
      return self.client.request('{url}{uri}'.format(url=current_url, uri=uri), 'PUT')

    def parse_xml(self, response):
      return libxml2.parseDoc(response)
Пример #32
0
 def _send_request(self, method, path, body=None, headers=None):
     if TIMEOUTS_AVAILABLE:
         http = Http(timeout=self.timeout)
         if self.username is not None:
             http.add_credentials(self.username, self.password)
         url = self.base_url + path
         
         try:
             start_time = time.time()
             self.log.debug("Starting request to '%s' (%s) with body '%s'..." % (url, method, str(body)[:10]))
             headers, response = http.request(url, method=method, body=body, headers=headers)
             end_time = time.time()
             self.log.info("Finished '%s' (%s) with body '%s' in %0.3f seconds." % (url, method, str(body)[:10], end_time - start_time))
         except AttributeError:
             # For httplib2.
             error_message = "Failed to connect to server at '%s'. Are you sure '%s' is correct? Checking it in a browser might help..." % (url, self.base_url)
             self.log.error(error_message)
             raise SolrError(error_message)
         
         if int(headers['status']) != 200:
             error_message = self._extract_error(headers, response)
             self.log.error(error_message)
             raise SolrError(error_message)
         
         return response
     else:
         if headers is None:
             headers = {}
         
         conn = HTTPConnection(self.host, self.port)
         start_time = time.time()
         self.log.debug("Starting request to '%s:%s/%s' (%s) with body '%s'..." % (self.host, self.port, path, method, str(body)[:10]))
         conn.request(method, path, body, headers)
         response = conn.getresponse()
         end_time = time.time()
         self.log.info("Finished '%s:%s/%s' (%s) with body '%s' in %0.3f seconds." % (self.host, self.port, path, method, str(body)[:10], end_time - start_time))
         
         if response.status != 200:
             error_message = self._extract_error(dict(response.getheaders()), response.read())
             self.log.error(error_message)
             raise SolrError(error_message)
         
         return response.read()
def create_proposal(username, password, filled=False):
    try:
        h = Http()
        user_proposed_from = db.session.query(User).filter_by(username=username).first()
        h.add_credentials(name=username, password=password)
        url = 'http://*****:*****@example.com')
        request_id = create_request(username='******', password='******', filled=True)
        data = dict(user_proposed_to=user_proposed_to,
                    user_proposed_from=user_proposed_from,
                    request_id=request_id,
                    filled=filled)
        response, content_bytes = h.request(
            url, method='POST',
            body=json.dumps(data),
            headers={"Content-Type": "application/json"})
        content = json.loads(content_bytes.decode())
        return content
    except Exception as err:
        return None
Пример #34
0
def addDescription(couchUrl, couchDbName, docId, description):
    """
    Test the adddescription.js update function.
    For use in this test to mimic operators clicking on stuff (i.e. using
    httplib2 rather than CMSCouth direct REST interface).    

    """
    fragments = urlparse.urlparse(couchUrl)
    userpassw = fragments[1].split("@",1)[0]
    user,passw = userpassw.split(":",1)
    newUrl = couchUrl.replace(userpassw, "")
    newUrl = newUrl.replace("@", "")
    h = Http()
    h.add_credentials(user, passw)
    data = {"newDescription" : description}
    headers = {"Content-type": "application/x-www-form-urlencoded"}
    url = "%s/%s/_design/OpsClipboard/_update/adddescription/%s" % (newUrl, couchDbName, docId)
    resp, content = h.request(url, "POST", urlencode(data),headers= headers)
    if content != "OK":
        raise Exception("Exception at CouchDB server: response: '%s', content: '%s'" % (resp, content))
Пример #35
0
def create_request(username, password, filled=False):
    try:
        h = Http()
        user = db.session.query(User).filter_by(username=username).first()
        h.add_credentials(name=username, password=password)
        url = 'http://localhost:5000/api/v1/requests/'
        location_string = 'amsterdam'
        data = dict(meal_type='pizza',
                    location_string=location_string,
                    meal_time='lunch',
                    user_id=user.id,
                    filled=filled)
        response, content_bytes = h.request(
            url, method='POST',
            body=json.dumps(data),
            headers={"Content-Type": "application/json"})
        content = json.loads(content_bytes.decode())
        return content
    except Exception as err:
        return None
Пример #36
0
 def test_change_user(self):
     create_user('stijn', 'test', '*****@*****.**')
     h = Http()
     h.add_credentials(name='stijn', password='******')
     url = 'http://*****:*****@example.com')
     try:
         response, content_bytes = h.request(
             url, method='PUT',
             body=json.dumps(data),
             headers={"Content-Type": "application/json"})
         if response['status'] != '201' and response['status'] != '200':
             raise Exception('Received an unsuccessful status code of %s' %
                             response['status'])
         content = json.loads(content_bytes.decode())
         self.assertEquals(content.get('username'), 'casper')
     except Exception as e:
         self.assertTrue(False,
                         'Received an unsuccessful status code of %s' %
                         response['status'])
Пример #37
0
 def send_http_request(self, url, basic_auth_username, basic_auth_password):
     h = Http()
     if basic_auth_username and basic_auth_password:
         h.add_credentials(basic_auth_username, basic_auth_password)
         
     project = self.video.get_team_video().project.slug
     data = dict(event=self.event_name, api_url=self.api_url,
                 video_id=self.video_id, team=self.team.slug, project=project)
     if self.language_code:
         data.update({"language_code":self.language_code} )
     data = urlencode(data)
     url = "%s?%s"  % (url , data)
     try:
         resp, content = h.request(url, method="POST", body=data)
         success =  200<= resp.status <400
         if success is False:
             logger.error("Failed to send team notification to %s - from teams:%s, status code:%s, response:%s" %(
                      self.team, url, resp, content ))
         return success, content
     except:
         logger.exception("Failed to send http notification ")
     return None, None
Пример #38
0
 def test_update_request(self):
     user = create_user('stijn', 'test', '*****@*****.**')
     request = create_request('stijn', 'test', False)
     try:
         h = Http()
         h.add_credentials(name='stijn', password='******')
         url = 'http://localhost:5000/api/v1/requests/%s' % request['id']
         data = dict(meal_type='pasta',
                     location_string='rotterdam',
                     meal_time='lunch',
                     user_id=user['id'],
                     filled=True)
         response, content_bytes = h.request(
             url, method='PUT',
             body=json.dumps(data),
             headers={"Content-Type": "application/json"})
         content = json.loads(content_bytes.decode())
         self.assertTrue(content.get('meal_type'), 'pasta')
     except Exception as err:
         self.assertTrue(False,
                         'Received an unsuccessful status code of %s' %
                         response['status'])
Пример #39
0
    def test_get_all_requests(self):
        create_user('stijn', 'test', '*****@*****.**')

        # create one open and one filled meal request
        create_request('stijn', 'test', False)
        create_request('stijn', 'test', True)
        try:
            h = Http()
            h.add_credentials(name='stijn', password='******')
            url = 'http://localhost:5000/api/v1/requests/'
            response, content_bytes = h.request(url, method='GET')
            if response['status'] != '201' and response['status'] != '200':
                raise Exception('Received an unsuccessful status code of %s' %
                    response['status'])
            content = json.loads(content_bytes.decode())

            # expected one meal request
            self.assertTrue(len(content['requests']) == 1)
        except Exception as err:
            self.assertTrue(False,
                            'Received an unsuccessful status code of %s' %
                            response['status'])
Пример #40
0
 def _api_help(self, endpoint, params=None, method='GET'):
     h = Http()
     h.add_credentials(self.username, self.password)
     url = '%s://%s.%s/%s' % (self.protocol, self.subdomain, self.domain,
                              endpoint)
     if method == 'GET':
         body = ''
         if params:
             url += '?' + urlencode(params)
     elif params:
         body = urlencode(params)
     else:
         body = ''
     headers, results = h.request(url, method, body)
     status = headers['status']
     if int(status) == 401:
         raise AuthFail('Sorry, your authentication was not accepted.')
     # TODO check status, raise appropriate errors or something
     try:
         return loads(results.decode('utf-8'))
     except ValueError:
         return results
Пример #41
0
 def _api_help(self, endpoint, params=None, method='GET'):
     h = Http()
     h.add_credentials(self.username, self.password)
     url = '%s://%s.%s/%s' % (self.protocol, self.subdomain, self.domain,
                              endpoint)
     if method == 'GET':
         body = ''
         if params:
             url += '?' + urlencode(params)
     elif params:
         body = urlencode(params)
     else:
         body = ''
     headers, results = h.request(url, method, body)
     status = headers['status']
     if int(status) == 401:
         raise AuthFail('Sorry, your authentication was not accepted.')
     # TODO check status, raise appropriate errors or something
     try:
         return loads(results.decode('utf-8'))
     except ValueError:
         return results
Пример #42
0
    def request(self, path, method="GET", body=None, content_type=None):
        from httplib2 import Http
        import xml.etree.ElementTree as ET
        from xml.parsers.expat import ExpatError
        h = Http()

        h.add_credentials(self.username, self.password)

        self._validate_params()
        uri = "{0}://{1}:{2}/API/{3}".format(self.protocol, self.host,
                                             self.port, path)

        headers = {'Accept': 'application/xml'}
        if content_type is not None:
            headers['Content-Type'] = content_type

        if body is None:
            (response, content) = h.request(uri,
                                            method=method,
                                            headers=headers)
        else:
            (response, content) = h.request(uri,
                                            method=method,
                                            body=body,
                                            headers=headers)

        if int(response['status']) < 200 or int(response['status']) > 299:
            raise HttpError(response, content)

        #register default namespace as vidispine, http://stackoverflow.com/questions/8983041/saving-xml-files-using-elementtree
        #ET.register_namespace('', "http://xml.vidispine.com/schema/vidispine")
        #ET._namespace_map['']="http://xml.vidispine.com/schema/vidispine"
        try:
            return ET.fromstring(content)
        except ExpatError:
            return content
Пример #43
0
                              body=data,
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])
except Exception as err:
    print "Test 1 FAILED: Could not make a new user"
    print err.args
    sys.exit()
else:
    print "Test 1 PASS: Succesfully made a new user"

#TEST 2 ADD NEW BAGELS TO THE DATABASE
try:
    h = Http()
    h.add_credentials('TinnyTim', 'Udacity')
    url = address + '/product'
    data = dict(
        username="******",
        password="******",
        name="plain",
        picture=
        "http://bonacbagel.weebly.com/uploads/4/0/5/4/40548977/s318635836612132814_p1_i1_w240.jpeg",
        description="Old-Fashioned Plain Bagel",
        price="$1.99")
    resp, content = h.request(url,
                              'POST',
                              body=json.dumps(data),
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
Пример #44
0
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])

except Exception as err:
    print("Test 1 FAILED: Could not make a new user")
    print(err.args)
    sys.exit()
else:
    print("Test 1 PASS: Succesfully made a new user")

#TEST 2: OBTAIN A TOKEN
try:
    h = Http()
    h.add_credentials('Peter', 'Pan')
    url = address + '/token'
    resp, content = h.request(url,
                              'GET',
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])
    new_content = json.loads(content)
    if not new_content['token']:
        raise Exception('No Token Received!')
    token = new_content['token']
    print("received token: %s" % token)
except Exception as err:
    print("Test 2 FAILED: Could not exchange user credentials for a token")
    print(err.args)
def sampleAndPost(*args):
    print "----- Gathering sample at", datetime.now()
    reading = d.readRegister(MODBUS_REGISTER)
    print "===== Read", reading
    data = dict(value=reading)
    # Here is the call that posts to CloudDot
    resp, content = h.request(writeUrl, "POST", urlencode(data))
    print "+++++ Post to CloudDot at", datetime.now(
    ), "Response status:", resp["status"]
    if resp["status"].startswith('2') == False:
        raise Exception(
            "Post failed; check your CloudDot username, API key, and channel. Response status was: %s"
            % (resp["status"], ))
    print


d = u3.U3()
writeUrl = "http://cloudapi.labjack.com/%s/channels/%s/write.json" % (
    CLOUDDOT_USERNAME, CLOUDDOT_CHANNEL)
h = Http()
h.add_credentials(CLOUDDOT_USERNAME, CLOUDDOT_API_KEY)

# Call it once at first because we're impatient
sampleAndPost()

# Set it up to call every SAMPLE_INTERVAL
signal.signal(signal.SIGALRM, sampleAndPost)
signal.setitimer(signal.ITIMER_REAL, SAMPLE_INTERVAL, SAMPLE_INTERVAL)
while True:
    signal.pause()
                              body=data,
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])
except Exception as err:
    print "Test 1 FAILED: Could not make a new user"
    print err.args
    sys.exit()
else:
    print "Test 1 PASS: Succesfully made a new user"

# TEST 2 ADD NEW BAGELS TO THE DATABASE
try:
    h = Http()
    h.add_credentials('TinnyTim', 'Udacity')
    url = address + '/bagels'
    data = dict(username="******",
                password="******",
                name="plain",
                picture="http://bonacbagel.weebly.com/uploads/4/0/5/4/40548977/s318635836612132814_p1_i1_w240.jpeg",
                description="Old-Fashioned Plain Bagel",
                price="$1.99")
    resp, content = h.request(url, 'POST',
                              body=json.dumps(data),
                              headers={"Content-Type" : "application/json"})
    if resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])
except Exception as err:
    print "Test 2 FAILED: Could not add new bagels"
Пример #47
0
        url, 'POST', body=data, headers={"Content-Type": "application/json"})

    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception(
            'Received an unsuccessful status code of %s' % resp['status'])
except Exception as err:
    print("Test 1 FAILED: Could not make a new user")
    print((err.args))
    sys.exit()
else:
    print("Test 1 PASS: Succesfully made a new user")

#TEST 2: OBTAIN A TOKEN
try:
    h = Http()
    h.add_credentials('Peter', 'Pan')
    url = address + '/token'
    resp, content = h.request(
        url, 'GET', headers={"Content-Type": "application/json"})

    if resp['status'] != '200':
        raise Exception(
            'Received an unsuccessful status code of %s' % resp['status'])
    new_content = json.loads(content)
    if not new_content['token']:
        raise Exception('No Token Received!')
    token = new_content['token']
    print(("received token: %s" % token))
except Exception as err:
    print("Test 2 FAILED: Could not exchange user credentials for a token")
    print((err.args))
Пример #48
0
                'param': par,
                'database': '/openerp/databases/%s' % self.cr.dbname,
            }

            ###
            ## Execute the before query if it available
            ##
            if js_conf.get('before'):
                self.cr.execute(js_conf['before'], {'id': ex})

            body = BODY_TEMPLATE % body_args
            log_debug('****\n%s\n****' % body)

            headers = {'Content-type': 'text/xml', 'charset': 'UTF-8', 'SOAPAction': 'runReport'}
            h = Http()
            h.add_credentials(js_conf['user'], js_conf['pass'])
            try:
                uri = 'http://%s:%d%s' % (js_conf['host'], js_conf['port'], js_conf['repo'])
                resp, content = h.request(uri, "POST", body, headers)
            except ServerNotFoundError:
                raise JasperException(_('Error'), _('Server not found !'))
            except HttpLib2Error, e:
                raise JasperException(_('Error'), '%s' % str(e))
            except Exception, e:
                # Bad fix for bug in httplib2 http://code.google.com/p/httplib2/issues/detail?id=96
                # not fix yet
                if str(e).find("'makefile'") >= 0:
                    raise JasperException(_('Connection error'), _('Cannot find the JasperServer at this address %s') % (uri,))
                raise JasperException(_('Error'), '%s' % str(e))

            log_debug('HTTP -> RESPONSE:')
Пример #49
0
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])

except Exception as err:
    print "Test 1 FAILED: Could not make a new user"
    print err.args
    sys.exit()
else:
    print "Test 1 PASS: Succesfully made a new user"

#TEST 2: OBTAIN A TOKEN
try:
    h = Http()
    h.add_credentials('mandal', 'Pan')
    url = address + '/api/v2/token'
    resp, content = h.request(url,
                              'GET',
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])
    new_content = json.loads(content)
    if not new_content['token']:
        raise Exception('No Token Received!')
    token = new_content['token']
    print "received token: %s" % token
except Exception as err:
    print "Test 2 FAILED: Could not exchange user credentials for a token"
    print err.args
Пример #50
0
import getpass
import os, sys
sys.path[0:0] = os.path.join(os.path.dirname(__file__), "..")

from dolt import Dolt
from httplib2 import Http


class Readernaut(Dolt):
    _api_url = "http://readernaut.com"
    _url_template = '%(domain)s/api/v1/json/%(generated_url)s'


if __name__ == "__main__":
    http = Http()
    username = raw_input("Readernaut Username: "******"Readernaut Password: "******"Total books for webology: %d" % len(books)
Пример #51
0
class RequestHandler(meta.MetaMixin):
    """
    Generic class that handles HTTP requests.  Uses the Json Serialization
    handler by default, but only 'deserializes' response content.  
    
    Optional Arguments / Meta:

        debug
            Boolean.  Toggle debug console output.  Default: False.
            
        ignore_ssl_validation
            Boolean.  Whether or not to ignore ssl validation errors.  
            Default: False
            
        response_handler
            An un-instantiated Response Handler class used to return 
            responses to the caller.  Default: drest.response.ResponseHandler.
            
        serialization_handler
            An un-instantiated Serialization Handler class used to 
            serialize/deserialize data.  
            Default: drest.serialization.JsonSerializationHandler.
            
        serialize
            Boolean.  Whether or not to serialize data before sending 
            requests.  Default: False.
        
        deserialize
            Boolean.  Whether or not to deserialize data before returning
            the Response object.  Default: True.
            
        trailing_slash
            Boolean.  Whether or not to append a trailing slash to the 
            request url.  Default: True.
            
        timeout
            The amount of seconds where a request should timeout.  
            Default: None

    """
    class Meta:
        debug = False
        ignore_ssl_validation = False
        response_handler = response.ResponseHandler
        serialization_handler = serialization.JsonSerializationHandler
        serialize = False
        deserialize = True
        trailing_slash = True
        allow_get_body = False
        timeout = None

    def __init__(self, **kw):
        super(RequestHandler, self).__init__(**kw)
        self._extra_params = {}
        self._extra_url_params = {}
        self._extra_headers = {}
        self._auth_credentials = ()
        self._http = None
        
        if 'DREST_DEBUG' in os.environ and \
           os.environ['DREST_DEBUG'] in [1, '1']:
            self._meta.debug = True
    
        response.validate(self._meta.response_handler)
        if self._meta.serialization_handler:
            serialization.validate(self._meta.serialization_handler)
            self._serialization = self._meta.serialization_handler(**kw)
            headers = self._serialization.get_headers()
            for key in headers:
                self.add_header(key, headers[key])
        else:
            self._meta.serialize = False
            self._meta.deserialize = False
                            
    def _serialize(self, data):
        if self._meta.serialize:
            return self._serialization.serialize(data)
        else:
            return data
    
    def _deserialize(self, data):
        if self._meta.deserialize:
            return self._serialization.deserialize(data)
        else:
            return data
 
    def set_auth_credentials(self, user, password):
        """
        Set the authentication user and password that will be used for 
        HTTP Basic and Digest Authentication.
        
        Required Arguments:
        
            user
                The authentication username.
                
            password
                That user's password.
                
        """
        self._auth_credentials = (user, password)
        self._clear_http()
        
    def add_param(self, key, value):
        """
        Adds a key/value to self._extra_params, which is sent with every 
        request.
        
        Required Arguments:
        
            key
                The key of the parameter.
                
            value
                The value of 'key'.
        
        """
        self._extra_params[key] = value
    
    def add_url_param(self, key, value):
        """
        Adds a key/value to self._extra_url_params, which is sent with every
        request (in the URL).
        
        Required Arguments:
        
            key
                The key of the parameter.
                
            value
                The value of 'key'.
        
        """
        self._extra_url_params[key] = value
        
    def add_header(self, key, value):
        """
        Adds a key/value to self._extra_headers, which is sent with every
        request.
        
        Required Arguments:
        
            key
                The key of the parameter.
                
            value
                The value of 'key'.
        
        """
        self._extra_headers[key] = value
    
    def _get_http(self):
        """
        Returns either the existing (cached) httplib2.Http() object, or
        a new instance of one.
        
        """      
        if self._http == None:
            if self._meta.ignore_ssl_validation:
                self._http = Http(disable_ssl_certificate_validation=True,
                                  timeout=self._meta.timeout)
            else:
                self._http = Http(timeout=self._meta.timeout)

            if self._auth_credentials:
                self._http.add_credentials(self._auth_credentials[0],
                                           self._auth_credentials[1])
        return self._http

    def _clear_http(self):
        self._http = None
            
    def _make_request(self, url, method, payload=None, headers=None):
        """
        A wrapper around httplib2.Http.request.
        
        Required Arguments:
        
            url
                The url of the request.
                
            method
                The method of the request. I.e. 'GET', 'PUT', 'POST', 'DELETE'.
            
        Optional Arguments:
            
            payload
                The urlencoded parameters.
            
            headers
                Additional headers of the request.
                
        """
        if payload is None:
            payload = {}
        if headers is None:
            headers = {}

        try:
            http = self._get_http()
            return http.request(url, method, payload, headers=headers)

        except socket.error as e:
            # Try again just in case there was an issue with the cached _http
            try:
                self._clear_http()
                return self._get_http().request(url, method, payload, 
                                                headers=headers)
            except socket.error as e:
                raise exc.dRestAPIError(e)
        
        except ServerNotFoundError as e:
            raise exc.dRestAPIError(e.args[0])
            
    def _get_complete_url(self, method, url, params):
        url = "%s%s" % (url.strip('/'), '/' if self._meta.trailing_slash else '')
        
        if method == 'GET':
            url_params = dict(self._extra_url_params, **params)
        else:
            url_params = self._extra_url_params
            
        if url_params:
            url = "%s?%s" % (url, urlencode(url_params))
            
        return url
        
    def make_request(self, method, url, params=None, headers=None):
        """
        Make a call to a resource based on path, and parameters.
    
        Required Arguments:
    
            method 
                One of HEAD, GET, POST, PUT, PATCH, DELETE, etc.
                
            url
                The full url of the request (without any parameters).  Any 
                params (with GET method) and self.extra_url_params will be 
                added to this url.
                
        Optional Arguments:
        
            params
                Dictionary of additional (one-time) keyword arguments for the
                request.
            
            headers
                Dictionary of additional (one-time) headers of the request.
                
        """   
        if params is None:
            params = {}
        if headers is None:
            headers = {}
        params = dict(self._extra_params, **params)
        headers = dict(self._extra_headers, **headers)
        url = self._get_complete_url(method, url, params)

        if self._meta.serialize: 
            payload = self._serialize(params)
        else:
            payload = urlencode(params)

        if self._meta.debug:
            print('DREST_DEBUG: method=%s url=%s params=%s headers=%s' % \
                   (method, url, payload, headers))


        if method is 'GET' and not self._meta.allow_get_body:
            payload = ''
            if self._meta.debug:
                print("DREST_DEBUG: supressing body for GET request")

        res_headers, data = self._make_request(url, method, payload,
                                               headers=headers)
        unserialized_data = data
        serialized_data = None
        if self._meta.deserialize:
            serialized_data = data
            data = self._deserialize(data)

        return_response = response.ResponseHandler(
            int(res_headers['status']), data, res_headers,
            )
        
        return self.handle_response(return_response)
    
    def handle_response(self, response_object):
        """
        A simple wrapper to handle the response.  By default raises 
        exc.dRestRequestError if the response code is within 400-499, or 500.
        Must return the original, or modified, response object.
        
        Required Arguments:
        
            response_object
                The response object created by the request.
                
        """
        response = response_object
        if (400 <= response.status <=499) or (response.status == 500):
            msg = "Received HTTP Code %s - %s" % (
                   response.status, 
                   httplib.responses[int(response.status)])
            raise exc.dRestRequestError(msg, response=response)
        return response
Пример #52
0
    def _http_check(self, instance):
        addr, username, password, timeout, headers, response_time, \
            disable_ssl_validation, use_keystone, keystone_config, \
            instance_name = self._load_http_conf(instance)
        dimensions = self._set_dimensions({'url': addr}, instance)

        start = time.time()

        done = False
        retry = False
        while not done or retry:
            if use_keystone:
                ksclient = self._ksclients[instance_name]
                token = ksclient.get_token()
                if token:
                    headers["X-Auth-Token"] = token
                    headers["Content-type"] = "application/json"
                else:
                    warning_string = """Unable to get token. Keystone API
                    server may be down. Skipping check for {0}""".format(addr)
                    self.log.warning(warning_string)
                    return False, warning_string
            try:
                self.log.debug("Connecting to %s" % addr)
                if disable_ssl_validation:
                    self.warning(
                        "Skipping SSL certificate validation for %s based on configuration"
                        % addr)
                h = Http(
                    timeout=timeout,
                    disable_ssl_certificate_validation=disable_ssl_validation)
                if username is not None and password is not None:
                    h.add_credentials(username, password)
                resp, content = h.request(addr, "GET", headers=headers)

            except (socket.timeout, HttpLib2Error, socket.error) as e:
                length = int((time.time() - start) * 1000)
                warn_string = '{0} is DOWN, error: {1}. Connection failed ' \
                              'after {2} ms'.format(addr, repr(e), length)
                self.log.warn(warn_string)
                return False, warn_string

            except httplib.ResponseNotReady as e:
                length = int((time.time() - start) * 1000)
                warn_string = '{0} is DOWN, error: {1}. Network is not ' \
                              'routable after {2} ms'.format(addr, repr(e),
                                                             length)
                self.log.warn(warn_string)
                return False, warn_string

            except Exception as e:
                length = int((time.time() - start) * 1000)
                error_string = '{0} is DOWN, error: {1}. Connection failed after {2} ms'.format(
                    addr, repr(e), length)
                self.log.error(error_string)
                return False, error_string

            if response_time:
                # Stop the timer as early as possible
                running_time = time.time() - start
                self.gauge('http_response_time',
                           running_time,
                           dimensions=dimensions)

            if int(resp.status) >= 400:
                if use_keystone and int(resp.status) == 401:
                    if retry:
                        error_string = '{0} is DOWN, unable to get a valid token to connect with'.format(
                            addr)
                        self.log.error(error_string)
                        return False, error_string
                    else:
                        # Get a new token and retry
                        self.log.info(
                            "Token expired, getting new token and retrying...")
                        retry = True
                        ksclient.refresh_token()
                        continue
                else:
                    warn_string = '{0} is DOWN, error code: {1}'.\
                        format(addr, str(resp.status))
                    self.log.warn(warn_string)
                    return False, warn_string
            done = True
            return True, content
Пример #53
0
class Jasper(object):

    def __init__(self, host='localhost', port=8080, user='******',
                 pwd='jasperadmin'):
        """
        Initialise new Jasper Object
        """
        self.cnx = Http()

        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd
        self.repo = 'jasperserver/services/repository'
        self.uri = 'http://%s:%s/%s' % (self.host, self.port, self.repo)

        self.headers = {
            'Content-type': 'text/xml',
            'charset': 'UTF-8',
            'SOAPAction': 'runReport'
        }
        self.body = ''

    def auth(self,):
        """
        Add credential
        """
        self.cnx.add_credentials(self.user, self.pwd)

        # We must simulate a request if we want to check the auth is correct

        # Generate a soap query to verify the authentification
        rq = Request(operationName='list', locale='fr_FR')
        rq.append(RequestRD('folder', '', '/'))
        self.body = SoapEnv('list', etree.tostring(rq)).output()
        try:
            res, content = self.cnx.request(self.uri, 'POST', self.body,
                                            self.headers)
        except socket.error:
            raise ServerNotFound('Server not found')

        if res.get('status', '200') == '401':
            raise AuthError('Authentification Failed !')
        elif res.get('status', '200') != '200':
            return False

        return True

    def send(self, soapQuery=''):  # noqa
        try:
            res, content = self.cnx.request(self.uri, 'POST', soapQuery,
                                            self.headers)
        except socket.error:
            raise ServerNotFound('Server not found')

        if res.get('status', '200') == '401':
            raise AuthError('Authentification Failed !')
        elif res.get('status', '200') != '200':
            return False

        try:
            return self.parseMultipart(content)
        except NotMultipartContent:
            fp = StringIO(content)
            tree = etree.parse(fp)
            fp.close()
            r = tree.xpath('//runReportReturn')
            if not r:
                raise UnknownResponse(content)

            fp = StringIO(r[0].text.encode('utf-8'))
            tree = etree.parse(fp)
            fp.close()

            raise ServerError(
                u'[%s] %s' % (tree.xpath('//returnCode')[0].text,  # noqa
                tree.xpath('//returnMessage')[0].text)
            )

    def create_request(self, operation='list', wsType='', uri='/', name='',  # noqa
                       arguments=None, params=None):
        if arguments is None:
            arguments = {}

        if params is None:
            params = {}

        rq = Request(operationName=operation, locale='fr_FR')
        for k in arguments:
            rq.append(RequestArgument(name=k, value=arguments[k]))

        # Add resource descriptor
        rd = RequestRD(operation, name, uri)
        label = etree.SubElement(rd, 'label')
        label.text = 'null'

        # Add query parameters
        for k, v in params.items():
            p = etree.SubElement(rd, 'parameter', name=k)
            if isinstance(v, basestring):
                p.text = v
            else:
                p.text = str(v)

        rq.append(rd)
        res = etree.tostring(rq, pretty_print=True)
        _logger.debug(res)
        return res

    def run_report(self, uri='/', output='PDF', params=None):
        """
        Launch a runReport in Jasper
        """
        if output not in KNOWN_FORMAT:
            raise UnknownFormat(output)

        args = {
            'RUN_OUTPUT_FORMAT': output,
            'PAGE': '0',
        }

        return self.create_request(operation='runReport', wsType='reportUnit',
                                   uri=uri, arguments=args, params=params)

    @staticmethod
    def parseMultipart(res):  # noqa
        srch = re.search(r'----=[^\r\n]*', res)
        if srch is None:
            raise NotMultipartContent()

        boundary = srch.group()
        res = " \n" + res
        res = "Content-Type: multipart/alternative; boundary=%s\n%s" % \
              (boundary, res)
        message = email.message_from_string(res)
        attachment = message.get_payload()[1]
        return {'content-type': attachment.get_content_type(),
                'data': attachment.get_payload()}

    def log_last_request(self,):
        """
        Return the last SOAP query as text
        """
        return self.body
Пример #54
0
    def _check(self, instance):
        addr, username, password, timeout, headers, response_time, dimensions, disable_ssl_validation, pattern, use_keystone = self._load_conf(
            instance)

        content = ''

        new_dimensions = {
            'component': 'monasca-agent',
            'service': 'monitoring',
            'url': addr
        }
        if dimensions is not None:
            new_dimensions.update(dimensions.copy())

        start = time.time()
        done = False
        retry = False
        while not done or retry:
            if use_keystone:
                api_config = self.agent_config['Api']
                keystone = Keystone(api_config)
                token = keystone.get_token()
                if token:
                    headers["X-Auth-Token"] = token
                    headers["Content-type"] = "application/json"
                else:
                    self.log.warning(
                        """Unable to get token. Keystone API server may be down.
                                     Skipping check for {}""".format(addr))
                    return
            try:
                self.log.debug("Connecting to %s" % addr)
                if disable_ssl_validation:
                    self.warning(
                        "Skipping SSL certificate validation for %s based on configuration"
                        % addr)
                h = Http(
                    timeout=timeout,
                    disable_ssl_certificate_validation=disable_ssl_validation)
                if username is not None and password is not None:
                    h.add_credentials(username, password)
                resp, content = h.request(addr, "GET", headers=headers)

            except socket.timeout as e:
                length = int((time.time() - start) * 1000)
                self.log.info(
                    "%s is DOWN, error: %s. Connection failed after %s ms" %
                    (addr, str(e), length))
                self.gauge('http_status', 1, dimensions=new_dimensions)
                return Status.DOWN, "%s is DOWN, error: %s. Connection failed after %s ms" % (
                    addr, str(e), length)

            except HttpLib2Error as e:
                length = int((time.time() - start) * 1000)
                self.log.info(
                    "%s is DOWN, error: %s. Connection failed after %s ms" %
                    (addr, str(e), length))
                self.gauge('http_status', 1, dimensions=new_dimensions)
                return Status.DOWN, "%s is DOWN, error: %s. Connection failed after %s ms" % (
                    addr, str(e), length)

            except socket.error as e:
                length = int((time.time() - start) * 1000)
                self.log.info(
                    "%s is DOWN, error: %s. Connection failed after %s ms" %
                    (addr, repr(e), length))
                self.gauge('http_status', 1, dimensions=new_dimensions)
                return Status.DOWN, "%s is DOWN, error: %s. Connection failed after %s ms" % (
                    addr, str(e), length)

            except httplib.ResponseNotReady as e:
                length = int((time.time() - start) * 1000)
                self.log.info(
                    "%s is DOWN, error: %s. Network is not routable after %s ms"
                    % (addr, repr(e), length))
                self.gauge('http_status', 1, dimensions=new_dimensions)
                return Status.DOWN, "%s is DOWN, error: %s. Network is not routable after %s ms" % (
                    addr, str(e), length)

            except Exception as e:
                length = int((time.time() - start) * 1000)
                self.log.error(
                    "Unhandled exception %s. Connection failed after %s ms" %
                    (str(e), length))
                self.gauge('http_status', 1, dimensions=new_dimensions)
                return Status.DOWN, "%s is DOWN, error: %s. Connection failed after %s ms" % (
                    addr, str(e), length)

            if response_time:
                # Stop the timer as early as possible
                running_time = time.time() - start
                self.gauge('http_response_time',
                           running_time,
                           dimensions=new_dimensions)

            # TODO(dschroeder): Save/send content data when supported by API

            if int(resp.status) >= 400:
                if use_keystone and int(resp.status) == 401:
                    if retry:
                        return Status.DOWN, "%s is DOWN, unable to get a valid token to connect with" % (
                            addr)
                    else:
                        # Get a new token and retry
                        self.log.warning(
                            "Token expired, getting new token and retrying...")
                        HTTPCheck.token = self.keystone.refresh_token()
                        retry = True
                        continue
                else:
                    self.log.info("%s is DOWN, error code: %s" %
                                  (addr, str(resp.status)))
                    self.gauge('http_status', 1, dimensions=new_dimensions)
                    return Status.DOWN, "%s is DOWN, error code: %s" % (
                        addr, str(resp.status))

            if pattern is not None:
                if re.search(pattern, content, re.DOTALL):
                    self.log.debug("Pattern match successful")
                else:
                    self.log.info("Pattern match failed! '%s' not in '%s'" %
                                  (pattern, content))
                    self.gauge('http_status', 1, dimensions=new_dimensions)
                    return Status.DOWN, "Pattern match failed! '%s' not in '%s'" % (
                        pattern, content)

            self.log.debug("%s is UP" % addr)
            self.gauge('http_status', 0, dimensions=new_dimensions)
            done = True
            return Status.UP, "%s is UP" % addr
Пример #55
0
 def test_access_with_credentials(self):
     url = address + '/version'
     h = Http()
     h.add_credentials('unit_test', 'unit_test')
     resp, result = h.request(url, 'GET')
     self.assertEqual(resp['status'], '200')
Пример #56
0
                              body=data,
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                        resp['status'])
except Exception as err:
    print("Test 1 FAILED: Could not make a new user")
    print(err.args)
    sys.exit()
else:
    print("Test 1 PASS: Succesfully made a new user")

#TEST 2 ADD NEW BAGELS TO THE DATABASE
try:
    h = Http()
    h.add_credentials('TinnyTim', 'Udacity')
    url = address + '/bagels'
    data = dict(
        username="******",
        password="******",
        name="plain",
        picture=
        "http://bonacbagel.weebly.com/uploads/4/0/5/4/40548977/s318635836612132814_p1_i1_w240.jpeg",
        description="Old-Fashioned Plain Bagel",
        price="$1.99")
    resp, content = h.request(url,
                              'POST',
                              body=json.dumps(data),
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '200':
        raise Exception('Received an unsuccessful status code of %s' %
                              body=data,
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '201' and resp['status'] != '200':
        raise Exception(
            f"Received an unsuccessful status code of {resp['status']}")
except Exception as err:
    print("Test 1 FAILED: Could not make a new user")
    print(err.args)
    sys.exit()
else:
    print("Test 1 PASS: Successfully made a new user")

# TEST 2 ADD NEW BAGELS TO THE DATABASE
try:
    h = Http()
    h.add_credentials("douha", "password")
    url = address + '/api/bagels'
    data = dict(
        username="******",
        password="******",
        name="plain",
        picture=
        "http://bonacbagel.weebly.com/uploads/4/0/5/4/40548977/s318635836612132814_p1_i1_w240.jpeg",
        description="Old-Fashioned Plain Bagel",
        price="$1.99")
    resp, content = h.request(url,
                              'POST',
                              body=json.dumps(data),
                              headers={"Content-Type": "application/json"})
    if resp['status'] != '200':
        raise Exception(
        }

        ###
        ## Execute the before query if it available
        ##
        if js_conf.get('before'):
            self.cr.execute(js_conf['before'], {'id': ex})
        
        try:
            d2 = {}
             
            for k, v in par.items():
                d2[k.encode('utf8')] = v.encode('utf8')
             
            h = Http()
            h.add_credentials(js_conf['user'], js_conf['pass'])
            body=""
            uri = 'http://%(js_host)s:%(js_port)s/jasperserver/rest_v2/reports/openerp/bases/%(db_name)s/%(report_unit)s.%(format)s?%(params)s' % {
                                                       'js_host': js_conf['host'],
                                                       'js_port': js_conf['port'],
                                                       'db_name': self.cr.dbname,
                                                       'report_unit': current_document.report_unit,
                                                       'format': current_document.format.lower(),
                                                       'params': urllib.urlencode(d2),                                                                                         
                                                       }
            resp, content = h.request(uri, "GET")
            if str(resp.status) in rest_exceptions.StatusException:
                tt = ParseXML_REST(content)
                raise rest_exceptions.StatusException[str(resp.status)]('\n'.join(tt))
            
            if current_document.format.upper() == "PDF":
if len(args) == 1:
    accessFile = args[0]
else:
    accessFile = None

(base, appName, appPass) = connectionProperties(
    parseConfigFile(options.config or 'crowd.properties'))

# Crowd deployment base URL
um = base + '/rest/usermanagement/1'

http = Http(cache='.cache')

# Crowd application credentials
http.add_credentials(appName, appPass)

CC_FRESH = {'Cache-Control': 'max-age=0', 'Accept': 'application/json'}


def get(url):
    resp, content = http.request(url, headers=CC_FRESH)
    if resp.status != 200:
        print('Failed to fetch %s: %s' % (url, resp), file=stderr)
        exit(10)
    return json.loads(content.decode('utf-8'))


def getEventToken():
    url = um + '/event'
    resp, content = http.request(url, headers=CC_FRESH)