| 1 | package Net::OBS::Client::Roles::Client; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 0 | ▶ | use XML::Structured; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Config::Tiny; | ||
| 7 | use HTTP::Request; | ||
| 8 | use HTTP::Cookies; | ||
| 9 | use Carp qw/croak/; | ||
| 10 | use File::Path qw/make_path/; | ||
| 11 | use Net::OBS::LWP::UserAgent; | ||
| 12 | |||
| 13 | use Net::OBS::SigAuth; | ||
| 14 | |||
| 15 | has use_oscrc => ( | ||
| 16 | is => 'rw', | ||
| 17 | isa => 'Bool', | ||
| 18 | ); | ||
| 19 | |||
| 20 | has apiurl => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Str', | ||
| 23 | lazy => 1, | ||
| 24 | default => sub { | ||
| 25 | return ($_[0]->use_oscrc) | ||
| 26 | ? $_[0]->oscrc->{general}->{apiurl} | ||
| 27 | : 'https://api.opensuse.org/public'; | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | has user => ( | ||
| 32 | is => 'rw', | ||
| 33 | isa => 'Str|Undef', | ||
| 34 | lazy => 1, | ||
| 35 | default => sub { | ||
| 36 | my $self = shift; | ||
| 37 | return $self->oscrc->{$self->apiurl}->{user} if ($self->use_oscrc); | ||
| 38 | return q{}; | ||
| 39 | }, | ||
| 40 | ); | ||
| 41 | |||
| 42 | has pass => ( | ||
| 43 | is => 'rw', | ||
| 44 | isa => 'Str|Undef', | ||
| 45 | lazy => 1, | ||
| 46 | default => sub { | ||
| 47 | my $self = shift; | ||
| 48 | return $self->oscrc->{$self->apiurl}->{pass} if ($self->use_oscrc); | ||
| 49 | return q{}; | ||
| 50 | }, | ||
| 51 | ); | ||
| 52 | |||
| 53 | has user_agent => ( | ||
| 54 | is => 'rw', | ||
| 55 | isa => 'Object', | ||
| 56 | lazy => 1, | ||
| 57 | default => sub { | ||
| 58 | my $ua = Net::OBS::LWP::UserAgent->new( | ||
| 59 | cookie_jar => $_[0]->cookie_jar | ||
| 60 | ); | ||
| 61 | if ($ENV{NET_OBS_CLIENT_DEBUG}) { | ||
| 62 | eval { | ||
| 63 | require LWP::ConsoleLogger::Easy; | ||
| 64 | LWP::ConsoleLogger::Easy->import('debug_ua'); | ||
| 65 | debug_ua($ua, $ENV{NET_OBS_CLIENT_DEBUG}); | ||
| 66 | }; | ||
| 67 | warn "$@" if $@; | ||
| 68 | } | ||
| 69 | $ua->timeout(10); | ||
| 70 | $ua->env_proxy; | ||
| 71 | $ua->sigauth_credentials($_[0]->sigauth_credentials) if %{$_[0]->sigauth_credentials}; | ||
| 72 | return $ua | ||
| 73 | }, | ||
| 74 | ); | ||
| 75 | |||
| 76 | has oscrc => ( | ||
| 77 | is => 'rw', | ||
| 78 | isa => 'Object', | ||
| 79 | lazy => 1, | ||
| 80 | default => sub { | ||
| 81 | my $rc; | ||
| 82 | if ( -f "$ENV{HOME}/.oscrc" ) { | ||
| 83 | $rc = "$ENV{HOME}/.oscrc"; | ||
| 84 | } elsif (-f "$ENV{HOME}/.config/osc/oscrc") { | ||
| 85 | $rc = "$ENV{HOME}/.config/osc/oscrc"; | ||
| 86 | } else { | ||
| 87 | die "No oscrc found\n"; | ||
| 88 | } | ||
| 89 | my $cf = Config::Tiny->read($rc); | ||
| 90 | die "Cannot open .oscrc\n" if ! $cf; | ||
| 91 | return $cf; | ||
| 92 | }, | ||
| 93 | ); | ||
| 94 | |||
| 95 | has repository => ( | ||
| 96 | is => 'rw', | ||
| 97 | isa => 'Str', | ||
| 98 | ); | ||
| 99 | |||
| 100 | has arch => ( | ||
| 101 | is => 'rw', | ||
| 102 | isa => 'Str', | ||
| 103 | ); | ||
| 104 | |||
| 105 | has api_path => ( | ||
| 106 | is => 'rw', | ||
| 107 | isa => 'Str', | ||
| 108 | ); | ||
| 109 | |||
| 110 | has cookie_jar => ( | ||
| 111 | is => 'rw', | ||
| 112 | isa => 'Object', | ||
| 113 | lazy => 1, | ||
| 114 | default => sub { | ||
| 115 | HTTP::Cookies->new( | ||
| 116 | file => $_[0]->cookie_jar_file, | ||
| 117 | autosave => 1, | ||
| 118 | ) | ||
| 119 | }, | ||
| 120 | ); | ||
| 121 | |||
| 122 | has sigauth_credentials => ( | ||
| 123 | is => 'rw', | ||
| 124 | isa => 'HashRef', | ||
| 125 | lazy => 1, | ||
| 126 | default => sub { {} }, | ||
| 127 | ); | ||
| 128 | |||
| 129 | has cookie_jar_file => ( | ||
| 130 | is => 'rw', | ||
| 131 | isa => 'Str', | ||
| 132 | lazy => 1, | ||
| 133 | default => sub { | ||
| 134 | if ($_[0]->use_oscrc) { | ||
| 135 | for my $i ("$::ENV{HOME}/.local/state/osc/cookiejar", "$::ENV{HOME}/.osc_cookiejar") { | ||
| 136 | return $i if (-f $i); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | my $state_dir = "$::ENV{HOME}/.local/state/Net_OBS_Client"; | ||
| 140 | -d $state_dir || make_path($state_dir); | ||
| 141 | return "$state_dir/cookie_jar"; | ||
| 142 | }, | ||
| 143 | ); | ||
| 144 | |||
| 145 | sub debug { | ||
| 146 | my @lines = @_; | ||
| 147 | return if (! $::ENV{NET_OBS_CLIENT_DEBUG} ); | ||
| 148 | for (@lines) {print "$_\n"}; | ||
| 149 | return; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub request { | ||
| 153 | my $self = shift; | ||
| 154 | my $method = shift; | ||
| 155 | my $api_path = shift; | ||
| 156 | $self->api_path($api_path) if $api_path; | ||
| 157 | |||
| 158 | my $ua = $self->user_agent(); | ||
| 159 | $ua->sigauth_credentials($self->sigauth_credentials) if $self->sigauth_credentials; | ||
| 160 | my $url = $self->apiurl . $self->api_path; | ||
| 161 | |||
| 162 | debug(" $method: $url"); | ||
| 163 | |||
| 164 | my $req = HTTP::Request->new($method => $url); | ||
| 165 | $req->uri->authority($self->user.'@'.$req->uri->authority) if ($self->user && !$self->pass && $req->uri->authority !~ /\@/); | ||
| 166 | |||
| 167 | $req->authorization_basic($self->user, $self->pass) if ($self->user && $self->pass); | ||
| 168 | my $response = $ua->request($req); | ||
| 169 | |||
| 170 | if (!$response->is_success) { | ||
| 171 | die $response->status_line . " while $method Request on ".$req->uri->canonical."\n"; | ||
| 172 | } | ||
| 173 | |||
| 174 | return $response->decoded_content; # or whatever | ||
| 175 | } | ||
| 176 | |||
| 177 | 1; # End of Net::OBS::Client | ||