| 1 | package XTP::Client 0.001; | ||
| 2 | our $VERSION; | ||
| 3 | |||
| 4 | use 5.016; | ||
| 5 | use warnings; | ||
| 6 | use Carp qw(croak carp); | ||
| 7 | use Date::Parse; | ||
| 8 | use Extism; | ||
| 9 | use JSON::PP qw(decode_json encode_json); | ||
| 10 | use LWP::UserAgent; | ||
| 11 | |||
| 12 | sub __request { | ||
| 13 | my ($self, $path, $opt) = @_; | ||
| 14 | $opt //= {}; | ||
| 15 | $opt->{method} = 'POST' if exists $opt->{body} && !$opt->{method}; | ||
| 16 | $opt->{method} //= 'GET'; | ||
| 17 | $opt->{headers}{Authorization} = "Bearer $self->{token}"; | ||
| 18 | my $ua = LWP::UserAgent->new; | ||
| 19 | $ua->agent("Perl-XTP-Client/$VERSION"); | ||
| 20 | my $req = HTTP::Request->new($opt->{method} => "$self->{baseUrl}$path", HTTP::Headers->new(%{$opt->{headers}}), $opt->{body}); | ||
| 21 | $ua->request($req) | ||
| 22 | } | ||
| 23 | |||
| 24 | sub _request { | ||
| 25 | my $res = __request(@_); | ||
| 26 | $res->is_success or die "request failed " . $res->content; | ||
| 27 | $res->content | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _json_request { | ||
| 31 | my ($self, $path, $opt) = @_; | ||
| 32 | $opt //= {}; | ||
| 33 | if (exists $opt->{body}) { | ||
| 34 | $opt->{body} = eval { | ||
| 35 | encode_json($opt->{body}) | ||
| 36 | }; | ||
| 37 | if ($@) { | ||
| 38 | die "Body could not be encoded to json\n"; | ||
| 39 | } | ||
| 40 | $opt->{headers}{'Content-Type'} //= 'application/json; charset=utf-8'; | ||
| 41 | } | ||
| 42 | my $res = $self->__request($path, $opt); | ||
| 43 | my $content = eval { | ||
| 44 | decode_json($res->content) | ||
| 45 | }; | ||
| 46 | if ($@) { | ||
| 47 | if ($res->is_success) { | ||
| 48 | die "Invalid JSON received\n"; | ||
| 49 | } | ||
| 50 | die "Request failed\n"; | ||
| 51 | } elsif (!$res->is_success) { | ||
| 52 | $content->{message} //= "Code: ".$res->code; | ||
| 53 | die "$content->{message}\n"; | ||
| 54 | } | ||
| 55 | $content | ||
| 56 | } | ||
| 57 | |||
| 58 | sub new { | ||
| 59 | my ($name, $opt) = @_; | ||
| 60 | my %opt = %$opt; | ||
| 61 | $opt{token} or croak 'token is required to instantiate an XTP client'; | ||
| 62 | $opt{appId} or croak 'appid is required to instantiate an XTP client'; | ||
| 63 | $opt{baseUrl} //= 'https://xtp.dylibso.com'; | ||
| 64 | my $self = bless \%opt, $name; | ||
| 65 | my $content = eval { | ||
| 66 | $self->_json_request("/api/v1/apps/$self->{appId}/extension-points") | ||
| 67 | }; | ||
| 68 | if ($@) { | ||
| 69 | chomp $@; | ||
| 70 | croak "Loading extension-points failed: $@"; | ||
| 71 | } | ||
| 72 | #foreach my $extpoint (@{$content->{objects}}) { | ||
| 73 | # print "extname $extpoint->{name} id $extpoint->{id}\n"; | ||
| 74 | #} | ||
| 75 | $self->{extpoints} = {map {("$_->{name}" => $_)} @{$content->{objects}}}; | ||
| 76 | return $self; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _getBindings { | ||
| 80 | my ($self, $extid, $guestkey) = @_; | ||
| 81 | my $content = eval { | ||
| 82 | $self->_json_request("/api/v1/extension-points/$extid/bindings/$guestkey") | ||
| 83 | }; | ||
| 84 | if ($@) { | ||
| 85 | chomp $@; | ||
| 86 | croak "Loading bindings failed: $@"; | ||
| 87 | } | ||
| 88 | $content | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _getLatestBinding { | ||
| 92 | my ($bindings) = @_; | ||
| 93 | my $latestbinding; | ||
| 94 | my $latestdate = 0; | ||
| 95 | while(my ($binding, $value) = each %$bindings) { | ||
| 96 | my $updatedAt = str2time($value->{updatedAt}); | ||
| 97 | if ($updatedAt > $latestdate) { | ||
| 98 | $latestdate = $updatedAt; | ||
| 99 | $latestbinding = $binding; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | $latestbinding | ||
| 103 | } | ||
| 104 | |||
| 105 | sub _getPluginSource { | ||
| 106 | my ($self, $extid, $guestkey, $bindingname) = @_; | ||
| 107 | my $bindings = $self->_getBindings($extid, $guestkey); | ||
| 108 | my $resolvedBindingName = $bindingname || _getLatestBinding($bindings); | ||
| 109 | if (!$resolvedBindingName || ! exists $bindings->{$resolvedBindingName}) { | ||
| 110 | return undef; | ||
| 111 | } | ||
| 112 | my $contentAddress = $bindings->{$resolvedBindingName}{contentAddress}; | ||
| 113 | $contentAddress or do { | ||
| 114 | return undef; | ||
| 115 | }; | ||
| 116 | my $path = "/tmp/xtp/$contentAddress"; | ||
| 117 | if (-f $path) { | ||
| 118 | my $artifactContent = do { local(@ARGV, $/) = $path; <> }; | ||
| 119 | return $artifactContent if ($artifactContent); | ||
| 120 | } | ||
| 121 | my $artifactContent = eval { | ||
| 122 | $self->_request("/api/v1/c/$contentAddress") | ||
| 123 | }; | ||
| 124 | if ($@) { | ||
| 125 | croak "Downloading Wasm failed"; | ||
| 126 | } | ||
| 127 | if (-d "/tmp/xtp" || mkdir("/tmp/xtp", 0755)) { | ||
| 128 | open(my $fh, '>', $path); | ||
| 129 | print $fh $artifactContent; | ||
| 130 | } | ||
| 131 | $artifactContent | ||
| 132 | } | ||
| 133 | |||
| 134 | sub getPlugin { | ||
| 135 | my ($self, $extname, $guestkey, %options) = @_; | ||
| 136 | my $extid = $self->{extpoints}{$extname}{id}; | ||
| 137 | my $bindingname //= $options{bindingName}; | ||
| 138 | my $pluginSource = $self->_getPluginSource($extid, $guestkey, $bindingname); | ||
| 139 | $pluginSource or croak 'Failed to find plugin'; | ||
| 140 | my $extismoptions = $options{extism} // $self->{extism}; | ||
| 141 | my $plugin = eval { | ||
| 142 | Extism::Plugin->new($pluginSource, $extismoptions) | ||
| 143 | }; | ||
| 144 | if ($@) { | ||
| 145 | my ($reason) = $@ =~ /^(.+)\sat.+line\s\d+/; | ||
| 146 | croak $reason; | ||
| 147 | } | ||
| 148 | $plugin | ||
| 149 | } | ||
| 150 | |||
| 151 | sub inviteGuest { | ||
| 152 | my ($self, $opt) = @_; | ||
| 153 | my %opt = %$opt; | ||
| 154 | $opt{guestKey} or croak 'guestkey is required to invite a guest'; | ||
| 155 | ($opt{deliveryMethod} // '') =~ /^(?:email|link)$/ or croak 'deliveryMethod must be email or link'; | ||
| 156 | if ($opt{deliveryMethod} eq 'email') { | ||
| 157 | $opt{name} // croak "name is required when deliveryMethod is email"; | ||
| 158 | } | ||
| 159 | my $response = eval { | ||
| 160 | $self->_json_request("/api/v1/apps/$self->{appId}/guests", { | ||
| 161 | body => \%opt | ||
| 162 | }) | ||
| 163 | }; | ||
| 164 | if ($@) { | ||
| 165 | chomp $@; | ||
| 166 | croak "Failed to invite guest: $@"; | ||
| 167 | } | ||
| 168 | $response | ||
| 169 | } | ||
| 170 | |||
| 171 | 1; |