diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index af05d52d2e..9b7c07e28d 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -3854,7 +3854,6 @@ dependencies = [ "parity-scale-codec 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "sr-std 2.0.0", - "substrate-offchain 2.0.0", "substrate-primitives 2.0.0", "substrate-state-machine 2.0.0", "substrate-trie 2.0.0", @@ -3878,6 +3877,7 @@ dependencies = [ "sr-io 2.0.0", "sr-std 2.0.0", "substrate-application-crypto 2.0.0", + "substrate-offchain 2.0.0", "substrate-primitives 2.0.0", ] diff --git a/substrate/core/sr-io/Cargo.toml b/substrate/core/sr-io/Cargo.toml index 08a526f4e6..2200014e9c 100644 --- a/substrate/core/sr-io/Cargo.toml +++ b/substrate/core/sr-io/Cargo.toml @@ -19,9 +19,6 @@ environmental = { version = "1.0.1", optional = true } substrate-state-machine = { path = "../state-machine", optional = true } trie = { package = "substrate-trie", path = "../trie", optional = true } -[dev-dependencies] -substrate-offchain = { path = "../offchain" } - [features] default = ["std"] std = [ diff --git a/substrate/core/sr-io/src/lib.rs b/substrate/core/sr-io/src/lib.rs index 06114c02a3..4b00a84231 100644 --- a/substrate/core/sr-io/src/lib.rs +++ b/substrate/core/sr-io/src/lib.rs @@ -45,8 +45,6 @@ pub enum EcdsaVerifyError { BadSignature, } -pub mod offchain; - /// Converts a public trait definition into a private trait and set of public functions /// that assume the trait is implemented for `()` for ease of calling. macro_rules! export_api { diff --git a/substrate/core/sr-primitives/Cargo.toml b/substrate/core/sr-primitives/Cargo.toml index 271ed2dccc..ff12fa6143 100644 --- a/substrate/core/sr-primitives/Cargo.toml +++ b/substrate/core/sr-primitives/Cargo.toml @@ -19,9 +19,10 @@ rand = { version = "0.7.0", optional = true } impl-trait-for-tuples = "0.1.1" [dev-dependencies] -serde_json = "1.0" primitive-types = "0.5.1" rand = "0.7.2" +serde_json = "1.0" +substrate-offchain = { path = "../offchain" } [features] default = ["std"] diff --git a/substrate/core/sr-primitives/src/lib.rs b/substrate/core/sr-primitives/src/lib.rs index ddb935867e..1e26a3d423 100644 --- a/substrate/core/sr-primitives/src/lib.rs +++ b/substrate/core/sr-primitives/src/lib.rs @@ -44,13 +44,13 @@ use codec::{Encode, Decode}; #[cfg(feature = "std")] pub mod testing; -pub mod weights; -pub mod traits; pub mod curve; - pub mod generic; -pub mod transaction_validity; +pub mod offchain; pub mod sr_arithmetic; +pub mod traits; +pub mod transaction_validity; +pub mod weights; /// Re-export these since they're only "kind of" generic. pub use generic::{DigestItem, Digest}; diff --git a/substrate/core/sr-io/src/offchain/http.rs b/substrate/core/sr-primitives/src/offchain/http.rs similarity index 89% rename from substrate/core/sr-io/src/offchain/http.rs rename to substrate/core/sr-primitives/src/offchain/http.rs index 7aab309f13..6df18da83e 100644 --- a/substrate/core/sr-io/src/offchain/http.rs +++ b/substrate/core/sr-primitives/src/offchain/http.rs @@ -14,7 +14,38 @@ // You should have received a copy of the GNU General Public License // along with Substrate. If not, see . -//! A non-std set of HTTP types. +//! A high-level helpers for making HTTP requests from Offchain Workers. +//! +//! `sr-io` crate exposes a low level methods to make and control HTTP requests +//! available only for Offchain Workers. Those might be hard to use +//! and usually that level of control is not really necessary. +//! This module aims to provide high-level wrappers for those APIs +//! to simplify making HTTP requests. +//! +//! +//! Example: +//! ```rust,no_run +//! use sr_primitives::offchain::http::Request; +//! +//! // initiate a GET request to localhost:1234 +//! let request: Request = Request::get("http://localhost:1234"); +//! let pending = request +//! .add_header("X-Auth", "hunter2") +//! .send() +//! .unwrap(); +//! +//! // wait for the response indefinitely +//! let mut response = pending.wait().unwrap(); +//! +//! // then check the headers +//! let mut headers = response.headers().into_iter(); +//! assert_eq!(headers.current(), None); +//! +//! // and collect the body +//! let body = response.body(); +//! assert_eq!(body.clone().collect::>(), b"1234".to_vec()); +//! assert_eq!(body.error(), &None); +//! ``` use rstd::str; use rstd::prelude::Vec; @@ -192,11 +223,11 @@ impl<'a, I: AsRef<[u8]>, T: IntoIterator> Request<'a, T> { let meta = &[]; // start an http request. - let id = crate::http_request_start(self.method.as_ref(), self.url, meta).map_err(|_| HttpError::IoError)?; + let id = runtime_io::http_request_start(self.method.as_ref(), self.url, meta).map_err(|_| HttpError::IoError)?; // add custom headers for header in &self.headers { - crate::http_request_add_header( + runtime_io::http_request_add_header( id, header.name(), header.value(), @@ -205,11 +236,11 @@ impl<'a, I: AsRef<[u8]>, T: IntoIterator> Request<'a, T> { // write body for chunk in self.body { - crate::http_request_write_body(id, chunk.as_ref(), self.deadline)?; + runtime_io::http_request_write_body(id, chunk.as_ref(), self.deadline)?; } // finalise the request - crate::http_request_write_body(id, &[], self.deadline)?; + runtime_io::http_request_write_body(id, &[], self.deadline)?; Ok(PendingRequest { id, @@ -276,7 +307,7 @@ impl PendingRequest { deadline: impl Into> ) -> Vec> { let ids = requests.iter().map(|r| r.id).collect::>(); - let statuses = crate::http_response_wait(&ids, deadline.into()); + let statuses = runtime_io::http_response_wait(&ids, deadline.into()); statuses .into_iter() @@ -314,7 +345,7 @@ impl Response { /// Retrieve the headers for this response. pub fn headers(&mut self) -> &Headers { if self.headers.is_none() { - self.headers = Some(Headers { raw: crate::http_response_headers(self.id) }); + self.headers = Some(Headers { raw: runtime_io::http_response_headers(self.id) }); } self.headers.as_ref().expect("Headers were just set; qed") } @@ -393,7 +424,7 @@ impl Iterator for ResponseBody { } if self.filled_up_to.is_none() { - let result = crate::http_response_read_body(self.id, &mut self.buffer, self.deadline); + let result = runtime_io::http_response_read_body(self.id, &mut self.buffer, self.deadline); match result { Err(e) => { self.error = Some(e); @@ -481,7 +512,7 @@ impl<'a> HeadersIterator<'a> { #[cfg(test)] mod tests { use super::*; - use crate::{TestExternalities, with_externalities}; + use runtime_io::{TestExternalities, with_externalities}; use substrate_offchain::testing; #[test] diff --git a/substrate/core/sr-io/src/offchain/mod.rs b/substrate/core/sr-primitives/src/offchain/mod.rs similarity index 100% rename from substrate/core/sr-io/src/offchain/mod.rs rename to substrate/core/sr-primitives/src/offchain/mod.rs