| 1 | package Net::OBS::Client::Roles::Client; | ||
| 2 | |||
| 3 | 1 | ▶ | use Moose::Role; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | 1 | ▶ | use LWP::UserAgent; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use XML::Structured; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Config::Tiny; | ||
| 8 | use HTTP::Request; | ||
| 9 | use URI::URL; | ||
| 10 | use Path::Class qw/file/; | ||
| 11 | use HTTP::Cookies; | ||
| 12 | |||
| 13 | has use_oscrc => ( | ||
| 14 | is => 'rw', | ||
| 15 | isa => 'Bool', | ||
| 16 | ); | ||
| 17 | |||
| 18 | has apiurl => ( | ||
| 19 | is => 'rw', | ||
| 20 | isa => 'Str', | ||
| 21 | lazy => 1, | ||
| 22 | default => sub { | ||
| 23 | return ($_[0]->use_oscrc) | ||
| 24 | ? $_[0]->oscrc->{general}->{apiurl} | ||
| 25 | : 'https://api.opensuse.org/public'; | ||
| 26 | }, | ||
| 27 | ); | ||
| 28 | |||
| 29 | has user => ( | ||
| 30 | is => 'rw', | ||
| 31 | isa => 'Str|Undef', | ||
| 32 | lazy => 1, | ||
| 33 | default => sub { | ||
| 34 | my $self = shift; | ||
| 35 | return $self->oscrc->{$self->apiurl}->{user} if ($self->use_oscrc); | ||
| 36 | return q{}; | ||
| 37 | }, | ||
| 38 | ); | ||
| 39 | |||
| 40 | has pass => ( | ||
| 41 | is => 'rw', | ||
| 42 | isa => 'Str|Undef', | ||
| 43 | lazy => 1, | ||
| 44 | default => sub { | ||
| 45 | my $self = shift; | ||
| 46 | return $self->oscrc->{$self->apiurl}->{pass} if ($self->use_oscrc); | ||
| 47 | return q{}; | ||
| 48 | }, | ||
| 49 | ); | ||
| 50 | |||
| 51 | |||
| 52 | has user_agent => ( | ||
| 53 | is => 'rw', | ||
| 54 | isa => 'Object', | ||
| 55 | lazy => 1, | ||
| 56 | default => sub { | ||
| 57 | my $self = shift; | ||
| 58 | my $ua = LWP::UserAgent->new; | ||
| 59 | if ($ENV{NET_OBS_CLIENT_DEBUG}) { | ||
| 60 | eval { | ||
| 61 | require LWP::ConsoleLogger::Easy; | ||
| 62 | LWP::ConsoleLogger::Easy->import('debug_ua'); | ||
| 63 | debug_ua($ua); | ||
| 64 | }; | ||
| 65 | } | ||
| 66 | $ua->timeout(10); | ||
| 67 | $ua->env_proxy; | ||
| 68 | |||
| 69 | return $ua | ||
| 70 | }, | ||
| 71 | ); | ||
| 72 | |||
| 73 | has oscrc => ( | ||
| 74 | is => 'rw', | ||
| 75 | isa => 'Object', | ||
| 76 | lazy => 1, | ||
| 77 | default => sub { | ||
| 78 | my $rc; | ||
| 79 | if ( -f "$ENV{HOME}/.oscrc" ) { | ||
| 80 | $rc = "$ENV{HOME}/.oscrc"; | ||
| 81 | } elsif (-f "$ENV{HOME}/.config/osc/oscrc") { | ||
| 82 | $rc = "$ENV{HOME}/.config/osc/oscrc"; | ||
| 83 | } else { | ||
| 84 | die "No oscrc found\n"; | ||
| 85 | } | ||
| 86 | my $cf = Config::Tiny->read($rc); | ||
| 87 | die "Cannot open .oscrc\n" if ! $cf; | ||
| 88 | return $cf; | ||
| 89 | }, | ||
| 90 | ); | ||
| 91 | |||
| 92 | has repository => ( | ||
| 93 | is => 'rw', | ||
| 94 | isa => 'Str', | ||
| 95 | ); | ||
| 96 | |||
| 97 | has arch => ( | ||
| 98 | is => 'rw', | ||
| 99 | isa => 'Str', | ||
| 100 | ); | ||
| 101 | |||
| 102 | sub debug { | ||
| 103 | my @lines = @_; | ||
| 104 | return if (! $ENV{NET_OBS_CLIENT_DEBUG} ); | ||
| 105 | for (@lines) {print "$_\n"}; | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub request { | ||
| 110 | my $self = shift; | ||
| 111 | my $method = shift; | ||
| 112 | my $api_path = shift; | ||
| 113 | |||
| 114 | my $ua = $self->user_agent(); | ||
| 115 | my $url = $self->apiurl . $api_path; | ||
| 116 | |||
| 117 | debug(" $method: $url"); | ||
| 118 | |||
| 119 | my $req = HTTP::Request->new($method => $url); | ||
| 120 | if ( $self->user ) { | ||
| 121 | $req->authorization_basic($self->user,$self->pass); | ||
| 122 | } | ||
| 123 | |||
| 124 | my $response = $ua->request($req); | ||
| 125 | |||
| 126 | if (!$response->is_success) { | ||
| 127 | die $response->status_line . " while $method Request on $url\n"; | ||
| 128 | } | ||
| 129 | return $response->decoded_content; # or whatever | ||
| 130 | } | ||
| 131 | |||
| 132 | 1; # End of Net::OBS::Client | ||