package FERWeb;
use warnings;
use strict;

# sudo apt-get install librpc-xml-perl libio-socket-ssl-perl

#use IO::Socket::SSL qw(debug4);
#use LWP::Debug qw(+);

require RPC::XML;
require RPC::XML::Client;
use YAML;

use Data::Dump qw(dump);

sub new {
	my ($class) = @_;

	my $auth = RPC::XML::Client->new('https://www.fer.hr/xmlrpc/xr_auth.php');

	my $login_id = $auth->send_request('auth.rlogin', '_koha', $ENV{PASSWD} )->value;
	die dump $login_id if ref $login_id eq 'HASH';

	warn "# login_id $login_id\n";

	my $self = { login_id => $login_id };
	bless $self, $class;
	return $self;
}

our $users = RPC::XML::Client->new('https://www.fer.hr/xmlrpc/xr_users.php');
sub getinfobycode {
	my ($self,$code) = @_;

	my $path = "ferweb/$code";
	return YAML::LoadFile $path if -e $path;

	my $res = $users->send_request(
		'users.getinfobycode', $self->{login_id}, RPC::XML::string->new($code)
	)->value;

	print "[$code] = ", dump($res), $/;

	YAML::DumpFile $path, $res;

	return $res;
}

1;
