Skip to content

Instantly share code, notes, and snippets.

@zoffixznet

zoffixznet/p6.p6 Secret

Created March 15, 2017 17:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoffixznet/e565ee330e249112ef93c9047461b1e2 to your computer and use it in GitHub Desktop.
Save zoffixznet/e565ee330e249112ef93c9047461b1e2 to your computer and use it in GitHub Desktop.
sub EXPORT (*@subs-to-export where *.all ∈ <jget jpost get post>) {
# note Map.new: |%(
Map.new:
'&jget' => &WWW::jget,
'&jpost' => &WWW::jpost,
'&get' => &WWW::get,
'&post' => &WWW::post,
# ){ |@subs-to-export.map: '&'~* }
}
module WWW {
use JSON::Fast;
use HTTP::UserAgent;
sub jget (|c) { from-json get |c }
sub jpost (|c) { from-json post |c }
sub get ($url) is export {
with HTTP::UserAgent.new.get: $url {
.is-success or fail .&err;
.decoded-content
}
}
multi sub post($, %?, *%) {*}
multi sub post ($url, *%form) is export {
post: $url, %, |%form;
}
multi sub post ($url, %headers, *%form) is export {
with HTTP::UserAgent.new.post($url, %form, |%headers) {
.is-success or fail .&err;
.decoded-content
}
}
sub err ($_) { "Error {.code}: {.status-line}" }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment