| 1 | #! perl | ||
| 2 | |||
| 3 | use v5.36; | ||
| 4 | use Object::Pad; | ||
| 5 | |||
| 6 | class Weenect::Connect; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Weenect::Connect - Low level server communications | ||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | my $api = Weenect::Connect->new; | ||
| 15 | $api->request( ... ) | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This modules handles low level communication (REST API) with the | ||
| 20 | Weenect server. | ||
| 21 | |||
| 22 | At the application level, use Weenect::API. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | use LWP::UserAgent; | ||
| 27 | use HTTP::Request::Common; | ||
| 28 | use JSON::PP; | ||
| 29 | use DDP; | ||
| 30 | |||
| 31 | field $json; | ||
| 32 | field $ua; | ||
| 33 | |||
| 34 | field $auth :mutator; | ||
| 35 | field $debug :mutator; | ||
| 36 | field $cache :mutator; | ||
| 37 | |||
| 38 | ADJUST { | ||
| 39 | $json = JSON::PP->new; | ||
| 40 | $ua = LWP::UserAgent->new; | ||
| 41 | $ua->agent("Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0"); | ||
| 42 | # $ua->default_header( ); | ||
| 43 | }; | ||
| 44 | |||
| 45 | method get_endpoint( $u = undef ) { | ||
| 46 | my $url = "https://apiv4.weenect.com/v4"; | ||
| 47 | $url .= "/" . $u if defined $u; | ||
| 48 | $url; | ||
| 49 | } | ||
| 50 | |||
| 51 | =head1 METHODS | ||
| 52 | |||
| 53 | Supported methods include: | ||
| 54 | |||
| 55 | =head2 request( $path, [ %keys ] ) | ||
| 56 | |||
| 57 | Performs a REST API request. | ||
| 58 | |||
| 59 | If %keys contains C<Content>, then a POST is made, otherwise it will GET. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | method request( $path, %keys ) { | ||
| 64 | my $u = $self->get_endpoint($path); | ||
| 65 | $u = "https://my.weenect.com/en/$path" if $path eq "logout"; | ||
| 66 | |||
| 67 | my $req; | ||
| 68 | my @common = | ||
| 69 | ( Accept => "application/json, text/plain, */*", | ||
| 70 | Origin => "https://my.weenect.com", | ||
| 71 | Referer => "https://my.weenect.com", | ||
| 72 | "x-app-version" => "0.1.0", | ||
| 73 | "x-app-user-id" => "", | ||
| 74 | "x-app-type" => "userspace", | ||
| 75 | DNT => 1, | ||
| 76 | ); | ||
| 77 | push( @common, Authorization => "JWT ".$auth->access_token ) if $auth; | ||
| 78 | |||
| 79 | my $op = delete $keys{OP} || (defined($keys{Content}) ? 'POST' : 'GET'); | ||
| 80 | |||
| 81 | if ( $op eq 'PUT' ) { | ||
| 82 | my $content = delete $keys{Content} // {}; | ||
| 83 | $content = $json->encode($content) if ref($content); | ||
| 84 | $req = HTTP::Request::Common::PUT | ||
| 85 | ( $u, | ||
| 86 | @common, | ||
| 87 | Content_Type => "application/json", | ||
| 88 | Content => $content, | ||
| 89 | %keys | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | elsif ( $op eq 'POST' ) { | ||
| 93 | my $content = delete $keys{Content} // {}; | ||
| 94 | $content = $json->encode($content) if ref($content); | ||
| 95 | $req = HTTP::Request::Common::POST | ||
| 96 | ( $u, | ||
| 97 | @common, | ||
| 98 | Content_Type => "application/json", | ||
| 99 | Content => $content, | ||
| 100 | %keys | ||
| 101 | ); | ||
| 102 | } | ||
| 103 | elsif ( $op eq 'GET' ) { | ||
| 104 | $req = HTTP::Request::Common::GET | ||
| 105 | ( $u, | ||
| 106 | @common, | ||
| 107 | %keys | ||
| 108 | ); | ||
| 109 | } | ||
| 110 | elsif ( $op eq 'DELETE' ) { | ||
| 111 | $req = HTTP::Request::Common::DELETE | ||
| 112 | ( $u, | ||
| 113 | @common, | ||
| 114 | %keys | ||
| 115 | ); | ||
| 116 | } | ||
| 117 | |||
| 118 | p($req) if $debug; | ||
| 119 | my $res = $ua->request($req); # res1.json.raw | ||
| 120 | unless ( $res->is_success ) { | ||
| 121 | p($res) if $debug; | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | |||
| 125 | return 1 if $path eq "logout"; # res is HTML login page | ||
| 126 | return $json->decode( $res->decoded_content || "{}" ); | ||
| 127 | } | ||
| 128 | |||
| 129 | 1; |