Python twisted.web.client.FileBodyProducer() Examples
The following are 30
code examples of twisted.web.client.FileBodyProducer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
twisted.web.client
, or try the search function
.
Example #1
Source File: test_cgi.py From learn_python3_spider with MIT License | 6 votes |
def test_ReadInput(self): cgiFilename = os.path.abspath(self.mktemp()) with open(cgiFilename, 'wt') as cgiFile: cgiFile.write(READINPUT_CGI) portnum = self.startServer(cgiFilename) agent = client.Agent(reactor) url = "http://localhost:%d/cgi" % (portnum,) url = url.encode("ascii") d = agent.request( uri=url, method=b"POST", bodyProducer=client.FileBodyProducer( BytesIO(b"Here is your stdin")), ) d.addCallback(client.readBody) d.addCallback(self._test_ReadInput_1) return d
Example #2
Source File: test_openbmc.py From maas with GNU Affero General Public License v3.0 | 6 votes |
def test_power_off(self): driver = OpenBMCPowerDriver() system_id = factory.make_name("system_id") context = make_context() url = driver.get_uri(context, HOST_STATE + "RequestedHostTransition") mock_file_body_producer = self.patch( openbmc_module, "FileBodyProducer" ) dataoff = FileBodyProducer( BytesIO(json.dumps(HOST_OFF).encode("utf-8")) ) mock_openbmc_request = self.patch(driver, "openbmc_request") mock_openbmc_request.return_value = dataoff mock_file_body_producer.return_value = dataoff mock_command = self.patch(driver, "command") mock_command.return_value = SAMPLE_JSON_HOSTOFF mock_set_pxe_boot = self.patch(driver, "set_pxe_boot") yield driver.power_off(system_id, context) self.assertThat(mock_set_pxe_boot, MockCalledOnceWith(context)) self.assertThat( mock_command, MockCalledOnceWith(context, b"PUT", url, dataoff) )
Example #3
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def test_hpe_create_volume_invalid_provisioning_option(self): name = 'test-create-volume-fake' path = b"/VolumeDriver.Create" body = {u"Name": name, u"Opts": {u"provisioning": u"fake"}} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({ u"Err": "Invalid input received: Must specify a valid " + "provisioning type ['thin', 'full', " + "'dedup'], value 'fake' is invalid."})) d.addCallback(self._remove_volume_callback, name) d.addErrback(self.cbFailed) return d
Example #4
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def test_hpe_create_volume_invalid_option(self): name = 'test-create-volume-fake' path = b"/VolumeDriver.Create" body = {u"Name": name, u"Opts": {u"fake": u"fake"}} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({ u"Err": "create volume failed, error is: fake is not a valid " "option. Valid options are: ['size', 'provisioning', " "'flash-cache']"})) d.addCallback(self._remove_volume_callback, name) d.addErrback(self.cbFailed) return d
Example #5
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def _get_volume_mount_path(self, body, name): # NOTE: body arg is the result from last deferred call. # Python complains about parameter mis-match if you don't include it # In this test, we need it to compare expected results with Path # request # Compare path returned by mount (body) with Get Path request path = b"/VolumeDriver.Path" newbody = {u"Name": name} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(newbody))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, body) d.addErrback(self.cbFailed) return d
Example #6
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def _mount_the_volume(self, body, name): # NOTE: body arg is the result from last deferred call. # Python complains about parameter mis-match if you don't include it # Mount the previously created volume path = b"/VolumeDriver.Mount" newbody = {u"Name": name} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(newbody))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.getResponse) # If we get a valid response from Path request then we assume # the mount passed. # TODO: Add additonal logic to verify the mountpath d.addCallback(self._get_volume_mount_path, name) return d
Example #7
Source File: test_cgi.py From learn_python3_spider with MIT License | 6 votes |
def test_ReadAllInput(self): cgiFilename = os.path.abspath(self.mktemp()) with open(cgiFilename, 'wt') as cgiFile: cgiFile.write(READALLINPUT_CGI) portnum = self.startServer(cgiFilename) url = "http://localhost:%d/cgi" % (portnum,) url = url.encode("ascii") d = client.Agent(reactor).request( uri=url, method=b"POST", bodyProducer=client.FileBodyProducer( BytesIO(b"Here is your stdin")), ) d.addCallback(client.readBody) d.addCallback(self._test_ReadAllInput_1) return d
Example #8
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def broken_test_hpe_mount_umount_volume(self): name = 'test-mount-volume' path = b"/VolumeDriver.Create" body = {u"Name": name} # Create a volume to be mounted headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addErrback(self.cbFailed) # Mount the previously created volume d.addCallback(self._mount_the_volume, name) # UMount the previously created volume d.addCallback(self._unmount_the_volume, name) # Remove the previously created volume d.addCallback(self._remove_volume_callback, name) return d
Example #9
Source File: webpush.py From autopush with Mozilla Public License 2.0 | 6 votes |
def _send_notification(self, uaid, node_id, notification): """Send a notification to a specific node_id This version of the overriden method includes the necessary crypto headers for the notification. :type notification: autopush.utils.WebPushNotification """ payload = notification.serialize() payload["timestamp"] = int(time.time()) url = node_id + "/push/" + uaid request = self.agent.request( "PUT", url.encode("utf8"), bodyProducer=FileBodyProducer(StringIO(json.dumps(payload))), ) request.addCallback(IgnoreBody.ignore) return request
Example #10
Source File: test_integration.py From autopush with Mozilla Public License 2.0 | 6 votes |
def _agent(method, url, contextFactory=None, headers=None, body=None): kwargs = {} if contextFactory: kwargs['contextFactory'] = contextFactory agent = Agent(reactor, **kwargs) rbody = None if body: rbody = FileBodyProducer(StringIO(body)) response = yield agent.request(method, url, headers=headers, bodyProducer=rbody) proto = AccumulatingProtocol() proto.closedDeferred = Deferred() response.deliverBody(proto) yield proto.closedDeferred returnValue((response, proto.data))
Example #11
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def test_hpe_get_volume(self): name = 'test-get-volume' path = b"/VolumeDriver.Create" body = {u"Name": name} # Create a volume to be mounted headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addErrback(self.cbFailed) # Get the previously created volume expected = {u"Volume": {u"Status": {}, u"Mountpoint": '', u"Name": name}, u"Err": ''} d.addCallback(self._get_volume, name, expected) # Remove the previously created volume d.addCallback(self._remove_volume_callback, name) return d
Example #12
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 6 votes |
def broken_test_hpe_list_volume(self): name = 'test-list-volume' path = b"/VolumeDriver.Create" body = {u"Name": name} # Create a volume to be mounted headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addErrback(self.cbFailed) # List volumes expected = {u"Err": '', u"Volumes": [{u"Mountpoint": '', u"Name": name}]} d.addCallback(self._list_volumes, name, expected) # Remove the previously created volume d.addCallback(self._remove_volume_callback, name) return d
Example #13
Source File: test_cgi.py From Safejumper-for-Desktop with GNU General Public License v2.0 | 6 votes |
def testReadInput(self): cgiFilename = os.path.abspath(self.mktemp()) with open(cgiFilename, 'wt') as cgiFile: cgiFile.write(READINPUT_CGI) portnum = self.startServer(cgiFilename) agent = client.Agent(reactor) d = agent.request( uri="http://localhost:%d/cgi" % (portnum,), method=b"POST", bodyProducer=client.FileBodyProducer( BytesIO(b"Here is your stdin")), ) d.addCallback(client.readBody) d.addCallback(self._testReadInput_1) return d
Example #14
Source File: test_api.py From worker with GNU General Public License v3.0 | 6 votes |
def request(self, method, url, content=None, state=http.OK, add_headers=None): body = None if content is None else client.FileBodyProducer( StringIO(content)) headers = {'Content-Type': ['application/json'], 'X-WebAuth-User': ['tester']} if add_headers: headers.update(add_headers) response = yield self.client.request(method, self.url_prefix + url, Headers(headers), body) self.assertEqual(state, response.code) body_receiver = BodyReceiver() response.deliverBody(body_receiver) body = yield body_receiver.finished if response.headers.getRawHeaders('content-type') == ['application/json']: body = anyjson.loads(body) returnValue((response, body))
Example #15
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 5 votes |
def _list_volumes(self, body, name, expected): path = b"/VolumeDriver.List" body = {u"Name": name} # Get a volume headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps(expected)) d.addErrback(self.cbFailed) return d
Example #16
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 5 votes |
def _unmount_the_volume(self, body, name): # NOTE: body arg is the result from last deferred call. # Python complains about parameter mis-match if you don't include it path = b"/VolumeDriver.Unmount" newbody = {u"Name": name} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(newbody))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addErrback(self.cbFailed) return d
Example #17
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 5 votes |
def broken_test_hpe_list_volume_no_volumes(self): path = b"/VolumeDriver.List" # Create a volume to be mounted headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps({}))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": '', u"Volumes": []})) d.addErrback(self.cbFailed) return d
Example #18
Source File: http.py From Paradrop with Apache License 2.0 | 5 votes |
def request(self, method, url, body=None): def makeRequest(ignored): bodyProducer = None if body is not None: bodyProducer = FileBodyProducer(six.StringIO(body)) headers = {} for key, value in six.iteritems(self.headers): headers[key] = [value] agent = Agent(reactor, pool=TwistedRequestDriver.pool) d = agent.request(method, url, Headers(headers), bodyProducer) d.addCallback(self.receive) return d def releaseSemaphore(result): TwistedRequestDriver.sem.release() # Forward the result to the next handler. return result d = TwistedRequestDriver.sem.acquire() # Make the request once we acquire the semaphore. d.addCallback(makeRequest) # Release the semaphore regardless of how the request goes. d.addBoth(releaseSemaphore) return d
Example #19
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 5 votes |
def test_hpe_create_volume_provisioning_option(self): name = 'test-create-volume' path = b"/VolumeDriver.Create" body = {u"Name": name, u"Opts": {u"provisioning": u"full"}} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addCallback(self._remove_volume_callback, name) d.addErrback(self.cbFailed) return d
Example #20
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 5 votes |
def test_hpe_create_volume_size_option(self): name = 'test-create-volume' path = b"/VolumeDriver.Create" body = {u"Name": name, u"Opts": {u"size": u"50"}} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addCallback(self._remove_volume_callback, name) d.addErrback(self.cbFailed) return d
Example #21
Source File: test_hpe_plugin.py From python-hpedockerplugin with Apache License 2.0 | 5 votes |
def test_hpe_create_volume(self): name = 'test-create-volume' path = b"/VolumeDriver.Create" body = {u"Name": name, u"Opts": None} headers = Headers({b"content-type": [b"application/json"]}) body_producer = FileBodyProducer(BytesIO(dumps(body))) agent = Agent.usingEndpointFactory(reactor, HPEEndpointFactory()) d = agent.request(b'POST', b"UNIX://localhost" + path, headers, body_producer) d.addCallback(self.checkResponse, json.dumps({u"Err": ''})) d.addErrback(self.cbFailed) return d
Example #22
Source File: test_openbmc.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_power_on(self): driver = OpenBMCPowerDriver() system_id = factory.make_name("system_id") context = make_context() url = driver.get_uri(context, HOST_STATE + "RequestedHostTransition") mock_file_body_producer = self.patch( openbmc_module, "FileBodyProducer" ) dataon = FileBodyProducer(BytesIO(json.dumps(HOST_ON).encode("utf-8"))) mock_openbmc_request = self.patch(driver, "openbmc_request") mock_openbmc_request.return_value = dataon mock_file_body_producer.return_value = dataon mock_power_query = self.patch(driver, "power_query") mock_power_query.return_value = "on" mock_command = self.patch(driver, "command") mock_command.return_value = SAMPLE_JSON_HOSTOFF mock_set_pxe_boot = self.patch(driver, "set_pxe_boot") yield driver.power_on(system_id, context) self.assertThat( mock_power_query, MockCalledOnceWith(system_id, context) ) self.assertThat(mock_set_pxe_boot, MockCalledWith(context)) self.assertThat( mock_command, MockCalledWith(context, b"PUT", url, dataon) )
Example #23
Source File: test_redfish.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_set_pxe_boot(self): driver = RedfishPowerDriver() context = make_context() url = driver.get_url(context) node_id = b"1" headers = driver.make_auth_headers(**context) mock_file_body_producer = self.patch( redfish_module, "FileBodyProducer" ) payload = FileBodyProducer( BytesIO( json.dumps( { "Boot": { "BootSourceOverrideEnabled": "Once", "BootSourceOverrideTarget": "Pxe", } } ).encode("utf-8") ) ) mock_file_body_producer.return_value = payload mock_redfish_request = self.patch(driver, "redfish_request") yield driver.set_pxe_boot(url, node_id, headers) self.assertThat( mock_redfish_request, MockCalledOnceWith( b"PATCH", join(url, b"redfish/v1/Systems/%s" % node_id), headers, payload, ), )
Example #24
Source File: openbmc.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def power_off(self, system_id, context): """Power off host.""" uri = self.get_uri(context, HOST_STATE + "RequestedHostTransition") data = FileBodyProducer(BytesIO(json.dumps(HOST_OFF).encode("utf-8"))) # set next one-time boot to PXE boot. yield self.set_pxe_boot(context) # power off host. power_state = yield self.command(context, b"PUT", uri, data) status = power_state.get("status") if status.lower() != "ok": raise PowerFatalError( "OpenBMC power driver received unexpected response" " to power off command" )
Example #25
Source File: openbmc.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def power_on(self, system_id, context): """Power on host.""" cur_state = yield self.power_query(system_id, context) uri = self.get_uri(context, HOST_STATE + "RequestedHostTransition") # power off host if it is currently on. if cur_state == "on": data = FileBodyProducer( BytesIO(json.dumps(HOST_OFF).encode("utf-8")) ) off_state = yield self.command(context, b"PUT", uri, data) status = off_state.get("status") if status.lower() != "ok": raise PowerFatalError( "OpenBMC power driver received unexpected response" " to power off command" ) # set one-time boot to PXE boot. yield self.set_pxe_boot(context) # power on host. data = FileBodyProducer(BytesIO(json.dumps(HOST_ON).encode("utf-8"))) on_state = yield self.command(context, b"PUT", uri, data) status = on_state.get("status") if status.lower() != "ok": raise PowerFatalError( "OpenBMC power driver received unexpected response" " to power on command" )
Example #26
Source File: openbmc.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def set_pxe_boot(self, context): """Set the host to PXE boot.""" # set boot mode to one-time boot. uri = self.get_uri(context, HOST_CONTROL + "one_time/attr/BootMode") data = FileBodyProducer(BytesIO(json.dumps(REG_MODE).encode("utf-8"))) yield self.command(context, b"PUT", uri, data) # set one-time boot source to network. uri = self.get_uri(context, HOST_CONTROL + "one_time/attr/BootSource") data = FileBodyProducer(BytesIO(json.dumps(SRC_NET).encode("utf-8"))) yield self.command(context, b"PUT", uri, data)
Example #27
Source File: openbmc.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def command(self, context, method, uri, data=None): """Current deployments of OpenBMC in the field do not support header based authentication. To issue RESTful commands, we need to login, issue RESTful command and logout. """ # login to BMC login_uri = self.get_uri(context, "/login") login_creds = { "data": [context.get("power_user"), context.get("power_pass")] } login_data = FileBodyProducer( BytesIO(json.dumps(login_creds).encode("utf-8")) ) login = yield self.openbmc_request(b"POST", login_uri, login_data) login_status = login.get("status") if login_status.lower() != "ok": raise PowerFatalError( "OpenBMC power driver received unexpected response" " to login command" ) # issue command cmd_out = yield self.openbmc_request(method, uri, data) # logout of BMC logout_uri = self.get_uri(context, "/logout") logout_creds = {"data": []} logout_data = FileBodyProducer( BytesIO(json.dumps(logout_creds).encode("utf-8")) ) logout = yield self.openbmc_request(b"POST", logout_uri, logout_data) logout_status = logout.get("status") if logout_status.lower() != "ok": raise PowerFatalError( "OpenBMC power driver received unexpected response" " to logout command" ) return cmd_out
Example #28
Source File: test_rsd.py From maas with GNU Affero General Public License v3.0 | 5 votes |
def test_set_pxe_boot(self): driver = RSDPodDriver() context = make_context() url = driver.get_url(context) node_id = context.get("node_id").encode("utf-8") headers = driver.make_auth_headers(**context) mock_file_body_producer = self.patch(rsd_module, "FileBodyProducer") payload = FileBodyProducer( BytesIO( json.dumps( { "Boot": { "BootSourceOverrideEnabled": "Once", "BootSourceOverrideTarget": "Pxe", } } ).encode("utf-8") ) ) mock_file_body_producer.return_value = payload mock_redfish_request = self.patch(driver, "redfish_request") yield driver.set_pxe_boot(url, node_id, headers) self.assertThat( mock_redfish_request, MockCalledOnceWith( b"PATCH", join(url, b"redfish/v1/Nodes/%s" % node_id), headers, payload, ), )
Example #29
Source File: http_client.py From calvin-base with Apache License 2.0 | 5 votes |
def encode_body(data): if not data: return None if not isinstance(data, str): return None return FileBodyProducer(StringIO(data))
Example #30
Source File: gcmpushkin.py From sygnal with Apache License 2.0 | 5 votes |
def _perform_http_request(self, body, headers): """ Perform an HTTP request to the FCM server with the body and headers specified. Args: body (nested dict): Body. Will be JSON-encoded. headers (Headers): HTTP Headers. Returns: """ body_producer = FileBodyProducer(BytesIO(json.dumps(body).encode())) # we use the semaphore to actually limit the number of concurrent # requests, since the HTTPConnectionPool will actually just lead to more # requests being created but not pooled – it does not perform limiting. with QUEUE_TIME_HISTOGRAM.time(): with PENDING_REQUESTS_GAUGE.track_inprogress(): await self.connection_semaphore.acquire() try: with SEND_TIME_HISTOGRAM.time(): with ACTIVE_REQUESTS_GAUGE.track_inprogress(): response = await self.http_agent.request( b"POST", GCM_URL, headers=Headers(headers), bodyProducer=body_producer, ) response_text = (await readBody(response)).decode() except Exception as exception: raise TemporaryNotificationDispatchException( "GCM request failure" ) from exception finally: self.connection_semaphore.release() return response, response_text