Ejemplo n.º 1
0
 def op(self, mocker):
     return pipeline_ops_http.HTTPRequestAndResponseOperation(
         method=fake_method,
         path=fake_path,
         headers=fake_headers,
         body=fake_body,
         query_params=fake_query_params,
         callback=mocker.MagicMock(),
     )
 def op(self, mocker):
     return pipeline_ops_http.HTTPRequestAndResponseOperation(
         method="SOME_METHOD",
         path="fake/path",
         headers={"fake_key": "fake_val"},
         body="fake_body",
         query_params="arg1=val1;arg2=val2",
         callback=mocker.MagicMock(),
     )
    def _run_op(self, op):
        if isinstance(op, pipeline_ops_iothub.SetIoTHubConnectionArgsOperation):
            self.device_id = op.device_id
            self.module_id = op.module_id

            if op.gateway_hostname:
                logger.debug(
                    "Gateway Hostname Present. Setting Hostname to: {}".format(op.gateway_hostname)
                )
                self.hostname = op.gateway_hostname
            else:
                logger.debug(
                    "Gateway Hostname not present. Setting Hostname to: {}".format(
                        op.gateway_hostname
                    )
                )
                self.hostname = op.hostname
            worker_op = op.spawn_worker_op(
                worker_op_type=pipeline_ops_http.SetHTTPConnectionArgsOperation,
                hostname=self.hostname,
                server_verification_cert=op.server_verification_cert,
                client_cert=op.client_cert,
                sas_token=op.sas_token,
            )
            self.send_op_down(worker_op)

        elif isinstance(op, pipeline_ops_iothub_http.MethodInvokeOperation):
            logger.debug(
                "{}({}): Translating Method Invoke Operation for HTTP.".format(self.name, op.name)
            )
            query_params = "api-version={apiVersion}".format(
                apiVersion=pkg_constant.IOTHUB_API_VERSION
            )
            #  if the target is a module.

            body = json.dumps(op.method_params)
            path = http_path_iothub.get_method_invoke_path(op.target_device_id, op.target_module_id)
            # Note we do not add the sas Authorization header here. Instead we add it later on in the stage above
            # the transport layer, since that stage stores the updated SAS and also X509 certs if that is what is
            # being used.
            x_ms_edge_string = "{deviceId}/{moduleId}".format(
                deviceId=self.device_id, moduleId=self.module_id
            )  # these are the identifiers of the current module
            user_agent = urllib.parse.quote_plus(
                ProductInfo.get_iothub_user_agent()
                + str(self.pipeline_root.pipeline_configuration.product_info)
            )
            headers = {
                "Host": self.hostname,
                "Content-Type": "application/json",
                "Content-Length": len(str(body)),
                "x-ms-edge-moduleId": x_ms_edge_string,
                "User-Agent": user_agent,
            }
            op_waiting_for_response = op

            def on_request_response(op, error):
                logger.debug(
                    "{}({}): Got response for MethodInvokeOperation".format(self.name, op.name)
                )
                error = map_http_error(error=error, http_op=op)
                if not error:
                    op_waiting_for_response.method_response = json.loads(
                        op.response_body.decode("utf-8")
                    )
                op_waiting_for_response.complete(error=error)

            self.send_op_down(
                pipeline_ops_http.HTTPRequestAndResponseOperation(
                    method="POST",
                    path=path,
                    headers=headers,
                    body=body,
                    query_params=query_params,
                    callback=on_request_response,
                )
            )

        elif isinstance(op, pipeline_ops_iothub_http.GetStorageInfoOperation):
            logger.debug(
                "{}({}): Translating Get Storage Info Operation to HTTP.".format(self.name, op.name)
            )
            query_params = "api-version={apiVersion}".format(
                apiVersion=pkg_constant.IOTHUB_API_VERSION
            )
            path = http_path_iothub.get_storage_info_for_blob_path(self.device_id)
            body = json.dumps({"blobName": op.blob_name})
            user_agent = urllib.parse.quote_plus(
                ProductInfo.get_iothub_user_agent()
                + str(self.pipeline_root.pipeline_configuration.product_info)
            )
            headers = {
                "Host": self.hostname,
                "Accept": "application/json",
                "Content-Type": "application/json",
                "Content-Length": len(str(body)),
                "User-Agent": user_agent,
            }

            op_waiting_for_response = op

            def on_request_response(op, error):
                logger.debug(
                    "{}({}): Got response for GetStorageInfoOperation".format(self.name, op.name)
                )
                error = map_http_error(error=error, http_op=op)
                if not error:
                    op_waiting_for_response.storage_info = json.loads(
                        op.response_body.decode("utf-8")
                    )
                op_waiting_for_response.complete(error=error)

            self.send_op_down(
                pipeline_ops_http.HTTPRequestAndResponseOperation(
                    method="POST",
                    path=path,
                    headers=headers,
                    body=body,
                    query_params=query_params,
                    callback=on_request_response,
                )
            )

        elif isinstance(op, pipeline_ops_iothub_http.NotifyBlobUploadStatusOperation):
            logger.debug(
                "{}({}): Translating Get Storage Info Operation to HTTP.".format(self.name, op.name)
            )
            query_params = "api-version={apiVersion}".format(
                apiVersion=pkg_constant.IOTHUB_API_VERSION
            )
            path = http_path_iothub.get_notify_blob_upload_status_path(self.device_id)
            body = json.dumps(
                {
                    "correlationId": op.correlation_id,
                    "isSuccess": op.is_success,
                    "statusCode": op.request_status_code,
                    "statusDescription": op.status_description,
                }
            )
            user_agent = urllib.parse.quote_plus(
                ProductInfo.get_iothub_user_agent()
                + str(self.pipeline_root.pipeline_configuration.product_info)
            )

            # Note we do not add the sas Authorization header here. Instead we add it later on in the stage above
            # the transport layer, since that stage stores the updated SAS and also X509 certs if that is what is
            # being used.
            headers = {
                "Host": self.hostname,
                "Content-Type": "application/json; charset=utf-8",
                "Content-Length": len(str(body)),
                "User-Agent": user_agent,
            }
            op_waiting_for_response = op

            def on_request_response(op, error):
                logger.debug(
                    "{}({}): Got response for GetStorageInfoOperation".format(self.name, op.name)
                )
                error = map_http_error(error=error, http_op=op)
                op_waiting_for_response.complete(error=error)

            self.send_op_down(
                pipeline_ops_http.HTTPRequestAndResponseOperation(
                    method="POST",
                    path=path,
                    headers=headers,
                    body=body,
                    query_params=query_params,
                    callback=on_request_response,
                )
            )

        else:
            # All other operations get passed down
            self.send_op_down(op)
    def _run_op(self, op):
        if isinstance(op, pipeline_ops_iothub_http.MethodInvokeOperation):
            logger.debug(
                "{}({}): Translating Method Invoke Operation for HTTP.".format(
                    self.name, op.name))
            query_params = "api-version={apiVersion}".format(
                apiVersion=pkg_constant.IOTHUB_API_VERSION)
            #  if the target is a module.

            body = json.dumps(op.method_params)
            path = http_path_iothub.get_method_invoke_path(
                op.target_device_id, op.target_module_id)
            # NOTE: we do not add the sas Authorization header here. Instead we add it later on in
            # the HTTPTransportStage
            x_ms_edge_string = "{deviceId}/{moduleId}".format(
                deviceId=self.pipeline_root.pipeline_configuration.device_id,
                moduleId=self.pipeline_root.pipeline_configuration.module_id,
            )  # these are the identifiers of the current module
            user_agent_string = urllib.parse.quote_plus(
                user_agent.get_iothub_user_agent() +
                str(self.pipeline_root.pipeline_configuration.product_info))
            # Method Invoke must be addressed to the gateway hostname because it is an Edge op
            headers = {
                "Host":
                self.pipeline_root.pipeline_configuration.gateway_hostname,
                "Content-Type": "application/json",
                "Content-Length": str(len(str(body))),
                "x-ms-edge-moduleId": x_ms_edge_string,
                "User-Agent": user_agent_string,
            }
            op_waiting_for_response = op

            def on_request_response(op, error):
                logger.debug(
                    "{}({}): Got response for MethodInvokeOperation".format(
                        self.name, op.name))
                error = map_http_error(error=error, http_op=op)
                if not error:
                    op_waiting_for_response.method_response = json.loads(
                        op.response_body)
                op_waiting_for_response.complete(error=error)

            self.send_op_down(
                pipeline_ops_http.HTTPRequestAndResponseOperation(
                    method="POST",
                    path=path,
                    headers=headers,
                    body=body,
                    query_params=query_params,
                    callback=on_request_response,
                ))

        elif isinstance(op, pipeline_ops_iothub_http.GetStorageInfoOperation):
            logger.debug(
                "{}({}): Translating Get Storage Info Operation to HTTP.".
                format(self.name, op.name))
            query_params = "api-version={apiVersion}".format(
                apiVersion=pkg_constant.IOTHUB_API_VERSION)
            path = http_path_iothub.get_storage_info_for_blob_path(
                self.pipeline_root.pipeline_configuration.device_id)
            body = json.dumps({"blobName": op.blob_name})
            user_agent_string = urllib.parse.quote_plus(
                user_agent.get_iothub_user_agent() +
                str(self.pipeline_root.pipeline_configuration.product_info))
            headers = {
                "Host": self.pipeline_root.pipeline_configuration.hostname,
                "Accept": "application/json",
                "Content-Type": "application/json",
                "Content-Length": str(len(str(body))),
                "User-Agent": user_agent_string,
            }

            op_waiting_for_response = op

            def on_request_response(op, error):
                logger.debug(
                    "{}({}): Got response for GetStorageInfoOperation".format(
                        self.name, op.name))
                error = map_http_error(error=error, http_op=op)
                if not error:
                    op_waiting_for_response.storage_info = json.loads(
                        op.response_body)
                op_waiting_for_response.complete(error=error)

            self.send_op_down(
                pipeline_ops_http.HTTPRequestAndResponseOperation(
                    method="POST",
                    path=path,
                    headers=headers,
                    body=body,
                    query_params=query_params,
                    callback=on_request_response,
                ))

        elif isinstance(
                op, pipeline_ops_iothub_http.NotifyBlobUploadStatusOperation):
            logger.debug(
                "{}({}): Translating Get Storage Info Operation to HTTP.".
                format(self.name, op.name))
            query_params = "api-version={apiVersion}".format(
                apiVersion=pkg_constant.IOTHUB_API_VERSION)
            path = http_path_iothub.get_notify_blob_upload_status_path(
                self.pipeline_root.pipeline_configuration.device_id)
            body = json.dumps({
                "correlationId": op.correlation_id,
                "isSuccess": op.is_success,
                "statusCode": op.request_status_code,
                "statusDescription": op.status_description,
            })
            user_agent_string = urllib.parse.quote_plus(
                user_agent.get_iothub_user_agent() +
                str(self.pipeline_root.pipeline_configuration.product_info))

            # NOTE we do not add the sas Authorization header here. Instead we add it later on in
            # the HTTPTransportStage
            headers = {
                "Host": self.pipeline_root.pipeline_configuration.hostname,
                "Content-Type": "application/json; charset=utf-8",
                "Content-Length": str(len(str(body))),
                "User-Agent": user_agent_string,
            }
            op_waiting_for_response = op

            def on_request_response(op, error):
                logger.debug(
                    "{}({}): Got response for GetStorageInfoOperation".format(
                        self.name, op.name))
                error = map_http_error(error=error, http_op=op)
                op_waiting_for_response.complete(error=error)

            self.send_op_down(
                pipeline_ops_http.HTTPRequestAndResponseOperation(
                    method="POST",
                    path=path,
                    headers=headers,
                    body=body,
                    query_params=query_params,
                    callback=on_request_response,
                ))

        else:
            # All other operations get passed down
            self.send_op_down(op)