Ejemplo n.º 1
0
             LOGGER.error("Unexpected error (%s)", code)
             code = HTTP_INTERNAL_SERVER_ERROR
     except urlfetch.Error, exception:
         LOGGER.error("Error establishing connection: %s",
                      str(exception))
 else:
     try:
         request = urllib2.Request(self.source_url + self.auth,
                                   body, headers)
         # try using the new SSL checking in python 2.7.9
         try:
             if not self.verify and PYTHON_2_7_9:
                 context = ssl.create_default_context(
                     ssl.Purpose.CLIENT_AUTH)
                 context.verify_mode = ssl.CERT_NONE
                 https_handler = StreamingHTTPSHandler(context=context)
                 opener = urllib2.build_opener(https_handler)
                 urllib2.install_opener(opener)
                 response = urllib2.urlopen(request)
             else:
                 response = urllib2.urlopen(request)
         except AttributeError:
             response = urllib2.urlopen(request)
         clear_console_line(out=out)
         reset_console_line(out=out)
         code = response.getcode()
         if code == HTTP_CREATED:
             location = response.headers['location']
             content = response.read()
             resource = json_load(content)
             resource_id = resource['resource']
Ejemplo n.º 2
0
    def _process_source(self,
                        resource_id,
                        location,
                        resource,
                        args=None,
                        progress_bar=False,
                        callback=None,
                        out=sys.stdout):
        """Creates a new source.

        """
        code = HTTP_INTERNAL_SERVER_ERROR
        error = {
            "status": {
                "code": code,
                "message": "The resource couldn't be created"
            }
        }

        if progress_bar and callback is not None:
            body, headers = multipart_encode(args, cb=callback)
        else:
            body, headers = multipart_encode(args)

        request = urllib2.Request(self.source_url + self.auth, body, headers)

        try:
            # try using the new SSL checking in python 2.7.9
            try:
                if not self.verify and PYTHON_2_7_9:
                    context = ssl.create_default_context(
                        ssl.Purpose.CLIENT_AUTH)
                    context.verify_mode = ssl.CERT_NONE
                    https_handler = StreamingHTTPSHandler(context=context)
                    opener = urllib2.build_opener(https_handler)
                    urllib2.install_opener(opener)
                    response = urllib2.urlopen(request)
                else:
                    response = urllib2.urlopen(request)
            except AttributeError:
                response = urllib2.urlopen(request)
            clear_console_line(out=out)
            reset_console_line(out=out)
            code = response.getcode()
            if code == HTTP_CREATED:
                location = response.headers['location']
                content = response.read()
                resource = json.loads(content, 'utf-8')
                resource_id = resource['resource']
                error = {}
        except ValueError:
            LOGGER.error("Malformed response")
        except urllib2.HTTPError, exception:
            code = exception.code
            if code in [
                    HTTP_BAD_REQUEST, HTTP_UNAUTHORIZED, HTTP_PAYMENT_REQUIRED,
                    HTTP_NOT_FOUND, HTTP_TOO_MANY_REQUESTS
            ]:
                content = exception.read()
                error = json.loads(content, 'utf-8')
                LOGGER.error(self.error_message(error, method='create'))
            else:
                LOGGER.error("Unexpected error (%s)", code)
                code = HTTP_INTERNAL_SERVER_ERROR