-
Notifications
You must be signed in to change notification settings - Fork 117
review structure #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: solutions
Are you sure you want to change the base?
review structure #24
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
[package] | ||
name = "pallet-http-call" | ||
version = "2.5.0" | ||
edition = "2021" | ||
authors = ["Mrisho Lukamba"] | ||
description = "https://github.com/MrishoLukamba/MrishoLukamba" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ | ||
"derive", | ||
] } | ||
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } | ||
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-std = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27"} | ||
|
||
log = "0.4.17" | ||
lite-json = "0.2.0" | ||
|
||
[dev-dependencies] | ||
sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-io = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"frame-benchmarking/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"scale-info/std", | ||
] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#![cfg_attr(not(feature = "std"),no_std)] | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
pub use pallet::*; | ||
#[frame_support::pallet] | ||
pub mod pallet{ | ||
use frame_system::pallet_prelude::*; | ||
use frame_support::pallet_prelude::*; | ||
use sp_runtime::offchain::{http}; | ||
use log; | ||
use lite_json; | ||
use lite_json::JsonValue; | ||
|
||
#[pallet::config] | ||
pub trait Config: frame_system::Config {} | ||
|
||
#[pallet::pallet] | ||
pub struct Pallet<T>(_); | ||
|
||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn get_external_data() -> Result<u32, http::Error> { | ||
|
||
let request = http::Request::get("https://api.fda.gov/food/enforcement.json?limit=0"); | ||
let pending = request.send().map_err(|_|http::Error::Unknown)?; | ||
let result = pending.wait(); | ||
|
||
let response = result.map_err(|_| http::Error::IoError)?; | ||
|
||
if response.code != 200 { | ||
log::info!("Failed Response Code {}",response.code); | ||
return Err(http::Error::IoError) | ||
} | ||
log::info!("fetched success"); | ||
|
||
let body = response.body().collect::<Vec<u8>>(); | ||
let body_str = sp_std::str::from_utf8(&body[..]) | ||
.map_err(|_| http::Error::Unknown)?; | ||
let body_json = lite_json::json_parser::parse_json(body_str) | ||
.map_err(|_|http::Error::Unknown)?; | ||
let result_total = Self::parse_to_int(body_json).unwrap(); | ||
Ok(result_total) | ||
|
||
} | ||
|
||
pub fn parse_to_int(body: JsonValue) -> Option<u32>{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function does not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this function does not need to be in |
||
let result = match body { | ||
JsonValue::Object(obj) => { | ||
let (_,val) = obj.into_iter().find(|(k,_)| k.iter().copied().eq("meta".chars()))?; | ||
match val { | ||
JsonValue::Object(obj) => { | ||
let (_,val) = obj.into_iter().find(|(k,_)|k.iter().copied().eq("results".chars()))?; | ||
match val { | ||
JsonValue::Object(obj) => { | ||
let (_,val) = obj.into_iter().find(|(k,_)|k.iter().copied().eq("total".chars()))?; | ||
match val { | ||
JsonValue::Number(num) => Some(num), | ||
_=> None | ||
} | ||
}, | ||
_=> None | ||
} | ||
}, | ||
_=> None | ||
} | ||
}, | ||
_=> None | ||
}; | ||
Some(result.unwrap().integer as u32) | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use crate as pallet_http_call; | ||
use frame_support::traits::{ConstU16, ConstU32, ConstU64}; | ||
use sp_runtime::{ | ||
testing::{Header}, | ||
traits::{Extrinsic, IdentifyAccount, Verify} | ||
}; | ||
use sp_core::H256; | ||
use sp_runtime::{ | ||
traits::{BlakeTwo256, IdentityLookup}, | ||
}; | ||
use sp_core::{ | ||
offchain::{testing, OffchainWorkerExt, TransactionPoolExt}, | ||
sr25519::Signature, | ||
}; | ||
type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<TestRuntime>; | ||
type Block = frame_system::mocking::MockBlock<TestRuntime>; | ||
|
||
// Configure a mock runtime to test the pallet. | ||
frame_support::construct_runtime!( | ||
pub enum TestRuntime where | ||
Block = Block, | ||
NodeBlock = Block, | ||
UncheckedExtrinsic = UncheckedExtrinsic, | ||
{ | ||
System: frame_system::{Pallet, Call, Config, Storage, Event<T>}, | ||
HttpCall: pallet_http_call, | ||
} | ||
); | ||
|
||
|
||
impl frame_system::Config for TestRuntime { | ||
type BaseCallFilter = frame_support::traits::Everything; | ||
type BlockWeights = (); | ||
type BlockLength = (); | ||
type DbWeight = (); | ||
type Origin = Origin; | ||
type Call = Call; | ||
type Index = u64; | ||
type BlockNumber = u64; | ||
type Hash = H256; | ||
type Hashing = BlakeTwo256; | ||
type AccountId = u64; | ||
type Lookup = IdentityLookup<Self::AccountId>; | ||
type Header = Header; | ||
type Event = Event; | ||
type BlockHashCount = ConstU64<250>; | ||
type Version = (); | ||
type PalletInfo = PalletInfo; | ||
type AccountData = (); | ||
type OnNewAccount = (); | ||
type OnKilledAccount = (); | ||
type SystemWeightInfo = (); | ||
type SS58Prefix = ConstU16<42>; | ||
type OnSetCode = (); | ||
type MaxConsumers = ConstU32<16>; | ||
} | ||
|
||
impl pallet_http_call::Config for TestRuntime {} | ||
|
||
#[test] | ||
fn test_htt_call(){ | ||
let mut test_ext = sp_io::TestExternalities::default(); | ||
let (ocw, ocw_state) = testing::TestOffchainExt::new(); | ||
let ocw_ext = OffchainWorkerExt::new(ocw); | ||
test_ext.register_extension(ocw_ext); | ||
|
||
ocw_state.write().expect_request(testing::PendingRequest { | ||
method: "GET".into(), | ||
uri: "https://api.fda.gov/food/enforcement.json?limit=0".into(), | ||
response: Some(br#" | ||
{ | ||
"meta": { | ||
"disclaimer": "Do not rely on openFDA to make decisions regarding medical care. While we make every effort to ensure that data is accurate, you should assume all results are unvalidated. We may limit or otherwise restrict your access to the API in line with our Terms of Service.", | ||
"terms": "https://open.fda.gov/terms/", | ||
"license": "https://open.fda.gov/license/", | ||
"last_updated": "2022-08-31", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test will fail as soon as they update their database again. |
||
"results": { | ||
"skip": 0, | ||
"limit": 0, | ||
"total": 22840 | ||
} | ||
}, | ||
"results": [] | ||
} | ||
"#.to_vec()), | ||
sent: true, | ||
..Default::default() | ||
}); | ||
|
||
|
||
test_ext.execute_with(||{ | ||
let total_result = HttpCall::get_external_data().unwrap(); | ||
assert_eq!(total_result,22840); | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
[package] | ||
name = "pallet-offchain-call" | ||
version = "2.5.0" | ||
edition = "2021" | ||
authors = ["Mrisho Lukamba"] | ||
description = "https://github.com/MrishoLukamba/MrishoLukamba" | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [ | ||
"derive", | ||
] } | ||
scale-info = { version = "2.1.1", default-features = false, features = ["derive"] } | ||
frame-benchmarking = { version = "4.0.0-dev", default-features = false, optional = true, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In our tutorials we are using substrate on branch |
||
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
log = "0.4.17" | ||
sp-std = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.27"} | ||
lite-json = "0.2.0" | ||
|
||
[dev-dependencies] | ||
sp-core = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-io = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-runtime = { version = "6.0.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28" } | ||
sp-keystore = { version="0.12.0", default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.28"} | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"frame-benchmarking/std", | ||
"frame-support/std", | ||
"frame-system/std", | ||
"scale-info/std", | ||
"sp-runtime/std", | ||
"sp-core/std", | ||
"sp-std/std" | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use more experessive function name.
This one get the total number of FDA enforcement. Call in so it express this