#!/usr/bin/perl

# Copyright (C) 2014 SUSE Linux Products GmbH
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

=head1 boilerplate

client - test openqa json interface

=head1 SYNOPSIS

client [OPTIONS] PATH

=head1 OPTIONS

=over 4

=item B<--host> HOST

connect to specified host, defaults to localhost

=item B<--params> FILE

load get/post parameters from a json file. For example

{
   "FLAVOR" : "DVD",
   "BUILD" : "42",
   "ARCH" : "i586",
   "DISTRI" : "opensuse",
   "VERSION" : "26",
}


=item B<--help, -h>

print help

=back

=head1 SYNOPSIS

top level entry points: jobs, workers, isos.

=item client --host openqa.example.com jobs

=item client --host openqa.example.com jobs/1

=item client --host openqa.example.com jobs/1 delete

=item client --host openqa.example.com isos post iso=bar.iso tests=blah

=head1 DESCRIPTION

lorem ipsum ...

=cut

use strict;
use FindBin;
BEGIN { unshift @INC, "$FindBin::RealBin/../lib" }

use Data::Dump;
use Mojo::URL;
use OpenQA::Client;
use Getopt::Long;
Getopt::Long::Configure("no_ignore_case");

my %options;

sub usage($) {
    my $r = shift;
    eval "use Pod::Usage; pod2usage(1);";
    if ($@) {
        die "cannot display help, install perl(Pod::Usage)\n";
    }
}

GetOptions(\%options, "host=s", "apibase=s", "verbose|v", "apikey:s", "apisecret:s", "params=s", "help|h|?",) or usage(1);

usage(1) unless @ARGV;
$options{'host'}    ||= 'localhost';
$options{'apibase'} ||= '/api/v1';

my $path = shift @ARGV;
# Relative paths are routed to v1
if ($path !~ m/^\//) {
    $path = join('/', $options{apibase}, $path);
}

my $method = 'get';
my %params;

if ($options{params}) {
    use JSON;
    local $/;
    open(my $fh, '<', $options{params});
    my $info = JSON->new->relaxed->decode(<$fh>);
    close $fh;
    %params = %{$info};
}

for my $arg (@ARGV) {
    if ($arg =~ /^(?:get|post|delete|put)$/i) {
        $method = lc $arg;
    }
    elsif ($arg =~ /^([[:alnum:]_\[\]]+)=(.+)/) {
        my $key   = $1;
        my $value = $2;
        # Mojo::URL no longer escapes / (https://github.com/kraih/mojo/commit/aab2f1f)
        $value =~ s@/@%2F@g;
        $params{$key} = $value;
    }
}

my $url;
if ($options{'host'} !~ '/') {
    $url = Mojo::URL->new();
    $url->host($options{'host'});
    $url->scheme('http');
}
else {
    $url = Mojo::URL->new($options{'host'});
}

$url->path($path);
$url->query([%params]) if %params;

my $client = OpenQA::Client->new(apikey => $options{'apikey'}, apisecret => $options{'apisecret'}, api => $url->host);

my $res = $client->$method($url)->res;
if ($res->code == 200) {
    dd($res->json || $res->body);
}
else {
    printf STDERR "ERROR: %s - %s\n", $res->code, $res->message;
    if ($res->body) {
        dd($res->json || $res->body);
    }
    exit(1);
}

1;
# vim: set sw=4 sts=4 et:
