Пример #1
0
 def exists(self):
   req = Request("https://pypi.python.org/pypi/{}".format(self.name))
   req.get_method = lambda: "HEAD"
   try:
     urlopen(req)
     return True
   except HTTPError as e:
     if e.code == 404:
       return False
     raise
Пример #2
0
 def exists(self):
     req = Request("https://pypi.org/pypi/{}".format(self.name))
     req.get_method = lambda: "HEAD"
     try:
         urlopen(req)
         return True
     except HTTPError as e:
         if e.code == 404:
             return False
         raise
Пример #3
0
 def _build_send_req(self, url, method='GET'):
     h = {
         'Authorization': 'Splunk %s' % self.session_key,
         'Content-Type': 'application/json'
     }
     try:
         request = Request(url, headers=h)
         request.get_method = lambda: method
         response = urlopen(request)
         return json.loads(response.read())
     except HTTPError as e:
         if e.code == http.client.NOT_FOUND:
             return None
         else:
             raise e
Пример #4
0
    def _build_req(self, method, data=None, name=None, query=None):
        """
        Build request object

        :param method: HTTP Method
        :param data: body data
        :param name: key,etc.
        :param query: query params
        :return: request object
        """
        h = {'Authorization': 'Splunk %s' % self.session_key}
        if h is not None:
            h['Content-Type'] = 'application/json'
        data_bytes = to_bytes(json.dumps(data))
        req = Request(self._build_uri(name, query=query), data_bytes, h)
        req.get_method = lambda: method
        return req
Пример #5
0
 def _build_send_req(self, url, method='GET', data=None):
     h = {
         'Authorization': 'Splunk %s' % self.session_key,
         'Content-Type': 'application/json'
     }
     try:
         encoded_data = urlencode(data) if data else None
         # Note: cannot use json.dumps on data because splunk savedsearch REST doesn't like it
         request = Request(url,
                           data=utils.to_bytes(encoded_data),
                           headers=h)
         request.get_method = lambda: method
         response = urlopen(request)
         return json.loads(response.read())
     except HTTPError as e:
         if e.code == http.client.NOT_FOUND:
             return None
         else:
             raise SavedsearchInternalException(e)
Пример #6
0
    def _generate_cloudwatch_input_request(self, method, data=None, name=None):
        base_url = '%s/servicesNS/nobody/Splunk_TA_aws/splunk_ta_aws_aws_cloudwatch/%s?%s'
        headers = {
            'Authorization': 'Splunk %s' % self.session_key,
            'Content-Type': 'application/json'
        }

        # Handle the query params that are passed in
        server_uri = em_common.get_server_uri()
        query_params = dict(output_mode='json')
        query_params['count'] = 1
        query_params['offset'] = 0

        # Build the URL and make the request
        url = base_url % (server_uri, name or '', urlencode(query_params))
        request = Request(url,
                          to_bytes(urlencode(data)) if data else None,
                          headers=headers)
        request.get_method = lambda: method

        return request
Пример #7
0
    def _build_send_req(self, method, stanza_name=None, data=None, query=None):
        """
        Build request object

        :param method: HTTP Method
        :param data: body data
        :param stanza_name: stanza in conf file to write to
        :param query: query params
        :return: response object
        """
        headers = {
            'Authorization': 'Splunk %s' % self.session_key,
            'Content-Type': 'application/json'
        }
        '''
        The REALLY annoying thing about CONF endpoints is that, unlike KV store endpoints,
        the format in which they take data arguments is the application/x-www-form-urlencoded.
        This means, if you have {a: 'b', c: 'd'}, you would just stringify this for KV store calls
        but for CONF endpoints, you need to send it like &a=b&c=d.

        :param params: dict of data we want to convert to this other format
        :return: a formatted string like a=b&c=d&enough=nonsense
        '''
        try:
            data_string = to_bytes(urlencode(data)) if data else None
            req = Request(url=self._build_uri(stanza_name, query=query),
                          data=data_string,
                          headers=headers)
            req.get_method = lambda: method
            res = urlopen(req)
            return json.loads(res.read())
        except HTTPError as e:
            if e.code == http.client.NOT_FOUND:
                return None
            else:
                raise ConfManagerInternalException(e)