summaryrefslogtreecommitdiff
path: root/lib/WebService/ForecastIo.pm
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2023-12-09 14:07:20 +0000
committerdakkar <dakkar@thenautilus.net>2023-12-09 14:07:20 +0000
commit1a37c7e52d5433e8182d444d12a55fdd9b8a2770 (patch)
tree2f0ed93dd07e6c078a5fb03cb9539ec93772fe23 /lib/WebService/ForecastIo.pm
parentbump perl (diff)
parentsort buses (diff)
downloadHomePanel-1a37c7e52d5433e8182d444d12a55fdd9b8a2770.tar.gz
HomePanel-1a37c7e52d5433e8182d444d12a55fdd9b8a2770.tar.bz2
HomePanel-1a37c7e52d5433e8182d444d12a55fdd9b8a2770.zip
Merge branch 'new-api'
Diffstat (limited to 'lib/WebService/ForecastIo.pm')
-rw-r--r--lib/WebService/ForecastIo.pm42
1 files changed, 15 insertions, 27 deletions
diff --git a/lib/WebService/ForecastIo.pm b/lib/WebService/ForecastIo.pm
index c329ad0..f3bf109 100644
--- a/lib/WebService/ForecastIo.pm
+++ b/lib/WebService/ForecastIo.pm
@@ -2,18 +2,18 @@ package WebService::ForecastIo;
use Moo;
use 5.10.0;
use Types::Standard -types,'slurpy';
-use Type::Utils qw(duck_type enum);
use Types::URI 'Uri';
-use Types::DateTime 'DateTimeT';
+use Types::DateTime -all;
use Type::Params;
+use Future::AsyncAwait;
use WebService::ForecastIo::Response;
use DateTime::TimeZone;
-use namespace::autoclean;
+use namespace::clean;
has base_uri => (
is => 'ro',
isa => Uri,
- default => 'https://api.forecast.io/forecast',
+ default => 'https://api.pirateweather.net/forecast',
coerce => Uri->coercion,
);
@@ -24,19 +24,10 @@ has api_key => (
);
has user_agent => (
- is => 'lazy',
- isa => duck_type(['get']),
+ is => 'ro',
+ isa => HasMethods['do_request'],
+ required => 1,
);
-sub _build_user_agent {
- require LWP::UserAgent;
- my $agent = LWP::UserAgent->new(
- agent => __PACKAGE__ . ' version ' . ($WebService::ForecastIo::VERSION // 'devel' ),
- env_proxy => 1,
- keep_alive => 1,
- );
- $agent->default_header( 'Accept-Encoding' => 'gzip' );
- return $agent;
-}
sub _make_request_uri {
my ($self,$opts) = @_;
@@ -74,36 +65,33 @@ sub _make_request_uri {
return $req_uri;
}
-my $units_type = enum [qw(us si ca uk auto)];
-my $block_type = enum [qw(currently minutely hourly daily alerts flags sources)];
+my $units_type = Enum [qw(us si ca uk auto)];
+my $block_type = Enum [qw(currently minutely hourly daily alerts flags sources)];
-sub request {
+async sub request {
state $argcheck = compile(
Object, Dict[
latitude => Num,
longitude => Num,
- time => Optional[DateTimeT],
+ time => Optional[DateTimeUTC],
units => Optional[$units_type],
exclude => Optional[ArrayRef[$block_type]],
- raw => Optional[Bool],
],
);
my ($self,$opts) = $argcheck->(@_);
$opts->{units} //= 'si';
$opts->{exclude} //= [];
- $opts->{raw} //= 0;
my $uri = $self->_make_request_uri({%$opts});
- my $response = $self->user_agent->get($uri);
+ my $response = await $self->user_agent->do_request(uri => $uri);
+
if ($response->is_success) {
my $json = $response->decoded_content;
- return $json if $opts->{raw};
- my $res = WebService::ForecastIo::Response->new($json);
- return wantarray ? ($res,$json) : $res
+ return WebService::ForecastIo::Response->new($json);
}
else {
- die $response->status_line
+ die $response->status_line;
}
}