| 1 | package Perinci::Use; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | our $DATE = '2017-07-10'; # DATE | |||||||||||||||||||||||||||
| 4 | 1 | our $VERSION = '0.07'; # VERSION | |||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use experimental 'smartmatch'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Perinci::Access; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Perinci::Sub::Util qw(err); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 1 | our %SPEC; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | $SPEC{use_riap_package} = { | ||||||||||||||||||||||||||||
| 18 | 1 | v => 1.1, | |||||||||||||||||||||||||||
| 19 | summary => 'Use a Riap package as if it was a local Perl module', | ||||||||||||||||||||||||||||
| 20 | result => { | ||||||||||||||||||||||||||||
| 21 | schema => 'undef', | ||||||||||||||||||||||||||||
| 22 | }, | ||||||||||||||||||||||||||||
| 23 | description => <<'_', | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | "Use" a remote code package over Riap protocol as if it was a local Perl module. | ||||||||||||||||||||||||||||
| 26 | Actually, what is being done is query the remote URL for available functions, | ||||||||||||||||||||||||||||
| 27 | and for each remote function, create a proxy function. The proxy function will | ||||||||||||||||||||||||||||
| 28 | then call the remote function. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Currently only functions are imported. Variables and other entities are ignored. | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | _ | ||||||||||||||||||||||||||||
| 33 | args => { | ||||||||||||||||||||||||||||
| 34 | url => { | ||||||||||||||||||||||||||||
| 35 | summary => 'Location of Riap package entity', | ||||||||||||||||||||||||||||
| 36 | description => <<'_', | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | Example: '/Foo/Bar/' (local Perl module), 'http://example.com/api/Module/'. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | _ | ||||||||||||||||||||||||||||
| 41 | schema => 'str*', | ||||||||||||||||||||||||||||
| 42 | req => 1, | ||||||||||||||||||||||||||||
| 43 | pos => 0, | ||||||||||||||||||||||||||||
| 44 | }, | ||||||||||||||||||||||||||||
| 45 | into => { | ||||||||||||||||||||||||||||
| 46 | summary => 'Perl package name to put proxy functions into', | ||||||||||||||||||||||||||||
| 47 | description => <<'_', | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Example: 'Foo::Bar', 'Example::Module'. | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | _ | ||||||||||||||||||||||||||||
| 52 | schema => 'str*', | ||||||||||||||||||||||||||||
| 53 | req => 1, | ||||||||||||||||||||||||||||
| 54 | pos => 1, | ||||||||||||||||||||||||||||
| 55 | }, | ||||||||||||||||||||||||||||
| 56 | include => { | ||||||||||||||||||||||||||||
| 57 | summary => 'Do not load all children, only load specified ones', | ||||||||||||||||||||||||||||
| 58 | schema => ['array*' => of=>'str*'], | ||||||||||||||||||||||||||||
| 59 | }, | ||||||||||||||||||||||||||||
| 60 | }, | ||||||||||||||||||||||||||||
| 61 | }; | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | sub use_riap_package { | ||||||||||||||||||||||||||
|
Subroutine
use_riap_package: not called CC=20 SCAR=58.2
Pod
documented
| |||||||||||||||||||||||||||||
| 63 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 64 | 0 | log_trace("-> use_riap_package(%s)", \%args); | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | my $url = $args{url} or return [400, "Please specify url"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $into = $args{into} or return [400, "Please specify into"]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return [500, "Invalid module name `$into`"] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | unless $into =~ /\A\w+(::\w+)*\z/; | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | my $inc = $args{include} // []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args{"include"} // []
MC/DC0%0/1
$args{"include"} // []
$args{"include"}
Truth table0%0/2
A
A= $args{"include"}
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $pa = Perinci::Access->new; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # try child_metas first | ||||||||||||||||||||||||||||
| 74 | 0 | my $res = $pa->request(child_metas => $url); | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | return err(500, "Can't request action 'child_metas' on URL $url", $res) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$res->[0] == 200 or $res->[0] == 501
MC/DC0%0/2
$res->[0] == 200 or $res->[0] == 501
$res->[0] == 200
$res->[0] == 501
Truth table0%0/3
A or B
A= $res->[0] == 200B= $res->[0] == 501
| |||||||||||||||||||||||||||||
| 76 | unless $res->[0] == 200 || $res->[0] == 501; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | my @e; | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if ($res->[0] == 200) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | my $metas = $res->[2]; | |||||||||||||||||||||||||||
| 81 | 0 | for my $u (keys %$metas) { | |||||||||||||||||||||||||||
| 82 | 0 | my $meta = $metas->{$u}; | |||||||||||||||||||||||||||
| 83 | 0 | ▶ | next unless $meta->{args}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | my $sub = $u; $sub =~ s!.+/!!; | |||||||||||||||||||||||||||
| 85 | 0 | push @e, [$sub, $u, $meta]; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } else { | ||||||||||||||||||||||||||||
| 88 | # try 'list' + later 'meta' for each child | ||||||||||||||||||||||||||||
| 89 | 0 | $res = $pa->request(list => $url, {detail=>1}); | |||||||||||||||||||||||||||
| 90 | 0 | ▶ | return err(500, "Can't request action 'list' on URL $url", $res) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | unless $res->[0] == 200; | ||||||||||||||||||||||||||||
| 92 | 0 | for my $r (@{$res->[2]}) { | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | next unless $r->{type} eq 'function'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | my $sub = $r->{uri}; $sub =~ s!.+/!!; | |||||||||||||||||||||||||||
| 95 | 0 | push @e, [$sub, $r->{uri}]; | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # check all specified entries 'include' must exist | ||||||||||||||||||||||||||||
| 100 | 0 | for my $s (@$inc) { | |||||||||||||||||||||||||||
| 101 | return [400, "'$s' does not exist under $url"] | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless grep {$s eq $_->[0]} @e; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # create proxy functions | ||||||||||||||||||||||||||||
| 106 | 0 | for my $e (@e) { | |||||||||||||||||||||||||||
| 107 | 0 | ▶ | next if @$inc && !($e->[0] ~~ @$inc); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$inc and not $e->[0] ~~ \@$inc
MC/DC0%0/2
@$inc and not $e->[0] ~~ \@$inc
@$inc
not $e->[0] ~~ \@$inc
Truth table0%0/3
A and not(B)
A= @$incB= not $e->[0] ~~ \@$inc
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # get metadata if not yet retrieved | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | unless ($e->[2]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 0 | $res = $pa->request(meta => $e->[1]); | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | return err(500, "Can't request action 'meta' on URL $e->[1]", $res) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | unless $res->[0] == 200; | ||||||||||||||||||||||||||||
| 114 | 0 | $e->[2] = $res->[2]; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # mark metadata | ||||||||||||||||||||||||||||
| 118 | 0 | $e->[2]{_note} = "Imported by ".__PACKAGE__." on ".scalar(localtime); | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # create proxy | ||||||||||||||||||||||||||||
| 121 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 122 | 1 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | *{"$into\::$e->[0]"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 124 | 0 | my %args = @_; | |||||||||||||||||||||||||||
| 125 | 0 | $pa->request(call => $e->[1], {args=>\%args}); | |||||||||||||||||||||||||||
| 126 | 0 | }; | |||||||||||||||||||||||||||
| 127 | 0 | ${"$into\::SPEC"}{$e->[0]} = $e->[2]; | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | log_trace("<- use_riap_package()"); | |||||||||||||||||||||||||||
| 131 | 0 | [200, "OK"]; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 135 | 0 | my ($module, $url, @args) = @_; | |||||||||||||||||||||||||||
| 136 | 0 | my $into = caller; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | ▶ | die "import: Please specify URL as first argument" unless $url; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | my $into_pm = $into; $into_pm =~ s!::!/!g; $into_pm .= ".pm"; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | return if $INC{$into_pm}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $res = use_riap_package(url=>$url, into=>$into, include=>\@args); | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | die "import: Can't use_riap_package $url: $res->[0] - $res->[1]" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | unless $res->[0] == 200; | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 0 | $INC{$into_pm} = $url; | |||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 0 | 1; | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 1 | 1; | |||||||||||||||||||||||||||
| 153 | # ABSTRACT: Use a Riap package as if it was a local Perl module | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | __END__ | ||||||||||||||||||||||||||||