Backblaze-B2
view release on metacpan or search on metacpan
lib/Backblaze/B2/v1/AnyEvent.pm view on Meta::CPAN
# You might want to override this if you want to use HIJK or
# some other way. If your HTTP requestor is synchronous, just
# return a
# AnyEvent->condvar
# which performs the real task.
# Actually, this now returns just a Promise
sub request {
my( $self, %options) = @_;
$options{ method } ||= 'GET';
#my $completed = delete $options{ cb };
my $method = delete $options{ method };
my $endpoint = delete $options{ api_endpoint };
my $headers = delete $options{ headers } || {};
$headers = { $self->get_headers, %$headers };
my $body = delete $options{ _body };
my $url;
if( ! $options{url} ) {
croak "Don't know the api_endpoint for the request"
unless $endpoint;
$url = URI->new( join( "/b2api/v1/",
$self->apiUrl,
$endpoint)
);
} else {
$url = delete $options{ url };
$url = URI->new( $url )
if( ! ref $url );
};
for my $k ( keys %options ) {
my $v = $options{ $k };
$url->query_param_append($k, $v);
};
$self->log_message(1, sprintf "Sending %s request to %s", $method, $url);
my $res = deferred;
my $req;
$req = http_request $method => $url,
headers => $headers,
body => $body,
sub {
my( $data, $headers ) = @_;
undef $req;
$res->resolve($data, $headers);
#undef $res; # justin case
},
;
$res->promise
}
=head2 C<< ->json_request >>
my $res = $b2->json_request(...)->then(sub {
my( $ok, $message, @stuff ) = @_;
});
Helper routine that expects a JSON formatted response
and returns the decoded JSON structure.
=cut
sub json_request {
my( $self, %options ) = @_;
$self->request(
%options
)->then(sub {
my( $body, $headers ) = @_;
my $d = deferred;
my @decoded = $self->decode_json_response($body, $headers);
my $result = $d->promise;
$d->resolve( @decoded );
$result
});
}
sub authorize_account {
my( $self, %options ) = @_;
$options{ accountId }
or croak "Need an accountId";
$options{ applicationKey }
or croak "Need an applicationKey";
my $auth= encode_base64( "$options{accountId}:$options{ applicationKey }" );
my $url = $self->{api_base} . "b2_authorize_account";
$self->json_request(
url => $url,
headers => {
"Authorization" => "Basic $auth"
},
)->then( sub {
my( $ok, $msg, $cred ) = @_;
if( $ok ) {
$self->log_message(1, sprintf "Storing authorization token");
$self->{credentials} = $cred;
};
return ( $ok, $msg, $cred );
});
}
=head2 C<< $b2->create_bucket >>
$b2->create_bucket(
bucketName => 'my_files',
bucketType => 'allPrivate',
);
Bucket names can consist of: letters, digits, "-", and "_".
L<https://www.backblaze.com/b2/docs/b2_create_bucket.html>
The C<bucketName> has to be B<globally> unique, so expect
this request to fail, a lot.
=cut
sub create_bucket {
my( $self, %options ) = @_;
croak "Need a bucket name"
unless defined $options{ bucketName };
$options{ accountId } ||= $self->accountId;
$options{ bucketType } ||= 'allPrivate'; # let's be defensive here...
$self->json_request(api_endpoint => 'b2_create_bucket',
accountId => $options{ accountId },
bucketName => $options{ bucketName },
( run in 0.367 second using v1.01-cache-2.11-cpan-6aa56a78535 )