...
Code Block | ||||
---|---|---|---|---|
| ||||
#!/usr/bin/env perl use strict; use warnings; use feature 'say'; use JSON; use Data::Dumper; use LWP::UserAgent; use HTTP::CookieJar::LWP; my $ua = LWP::UserAgent->new( cookie_jar => HTTP::CookieJar::LWP->new, ); my $host = 'folio-snapshot-okapi.dev.folio.org'; my $x_okapi_token = ''; my $res = $ua->post( 'https://folio-snapshot-okapi.dev.folio.org' . $host . '/authn/login-with-expiry', 'Accept' => 'text/plain', 'content-type' => 'application/json', 'x-okapi-tenant' => 'diku', 'x-okapi-token' => '', 'Content' => '{ "username": "diku_admin", "password": "admin" }', ); if ( $res->is_success ) { if ( $res->status_line =~ /^201 / ) { my $headers$ref = $res->headers()->as_string; my @lines = split /\n/, $headers; foreach my $line ( @lines ) { say $line; if ( $line =~ /^X-Okapi-Token: (.+)$/ ) { $ua->cookie_jar; my %cookies = %$ref; $x_okapi_token = $1; last; } }$cookies{store}{$host}{'/'}{folioAccessToken}{value}; } } else { say STDERR 'failed:'; say STDERR $res->status_line; say STDERR $res->decoded_content; } die 'No okapi token retrieved' unless $x_okapi_token; my @folioUsers; my $totalRecords = 0; my $offset = 0; { do { ( $offset, $totalRecords, @folioUsers ) = getUsers( $offset, @folioUsers ); say $offset; } while ( $offset < $totalRecords ); } say 'TotalRecords: ', $totalRecords; say 'Records retrieved: ', scalar @folioUsers; say Dumper @folioUsers; exit 0; # -------------- subroutines ----------------------# sub getUsers { my ( $offset, @folioUsers ) = @_; say 'offset: ', $offset; my $jsonString; $res = $ua->get( 'https://folio-snapshot-okapi.dev.folio.org/users?limit=10&query=(active=="true")&offset=' . $offset, 'Accept' => 'text/plain', 'content-type' => 'application/json', 'x-okapi-tenant' => 'diku', 'x-okapi-token' => "$x_okapi_token" ); if ( $res->is_success ) { if ( $res->status_line ne '200 OK' ) { say STDERR 'not ok:'; say STDERR $res->status_line; say STDERR $res->decoded_content; return; } $jsonString = $res->decoded_content; } else { say STDERR 'failed:'; say STDERR $res->status_line; say STDERR $res->decoded_content; return; } my $ref = JSON->new->decode( $jsonString ); my %hash = %$ref; my $totalRecords = $hash{'totalRecords'}; my @users = $hash{users}; foreach ( @users ) { my @records = @$_; foreach my $ref ( @records ) { push @folioUsers, JSON->new->encode( $ref ); $offset++; } } return $offset, $totalRecords, @folioUsers; } __END__ |
...