package Dokuwiki::RPC::XML::Client;
# ABSTRACT: Dokuwiki::RPC::XML::Client - A RPC::XML::Client for dokuwiki (
use strict;
use warnings;
use parent 'RPC::XML::Client';
our $VERSION = '0.2';
our %API = qw<
getPagelist dokuwiki.getPagelist
getVersion dokuwiki.getVersion
getTime dokuwiki.getTime
getXMLRPCAPIVersion dokuwiki.getXMLRPCAPIVersion
login dokuwiki.login
getTitle dokuwiki.getTitle
appendPage dokuwiki.appendPage
setLocks dokuwiki.setLocks
getRPCVersionSupported wiki.getRPCVersionSupported
aclCheck wiki.aclCheck
getPage wiki.getPage
getPageVersion wiki.getPageVersion
getPageVersions wiki.getPageVersions
getPageInfo wiki.getPageInfo
getPageInfoVersion wiki.getPageInfoVersion
getPageHTML wiki.getPageHTML
getPageHTMLVersion wiki.getPageHTMLVersion
putPage wiki.putPage
listLinks wiki.listLinks
getAllPages wiki.getAllPages
getBackLinks wiki.getBackLinks
getRecentChanges wiki.getRecentChanges
getRecentMediaChanges wiki.getRecentMediaChanges
getAttachments wiki.getAttachments
getAttachment wiki.getAttachment
getAttachmentInfo wiki.getAttachmentInfo
putAttachment wiki.putAttachment
deleteAttachment wiki.deleteAttachment
addAcl plugin.acl.addAcl
delAcl plugin.acl.delAcl >; 

sub _reach_arguments {
    if    ( $_[1] eq 'url' )  { splice @_, 1, 1; return }
    elsif ( $_[1] eq 'base' ) { splice @_, 1, 1 }

sub reach {
    my ( $class, $url, %options ) = @_;
    $options{file} //= "$ENV{HOME}/.dokuwiki.cookies.txt";
    ( $url 
    , useragent => [ cookie_jar => { file => $options{file} } ]
    , error_handler => sub { die $_[0] }
    , fault_handler => sub { die $_[0]->string } );

{ no strict 'refs';
    while ( my($method, $call) = each %API ) {
        *{__PACKAGE__."::$method"} = sub {
            my $client = shift;
            $client->send_request( $call, @_ )->value


=head1 NAME

Dokuwiki::RPC::XML::Client - A RPC::XML::Client for L<dokuwiki|>.


L<Dokuwiki::RPC::XML::Client> extends the L<RPC::XML::Client> with the Dokuwiki
XML-RPC methods (without namespace) described in the
L<dokuwiki xml-rpc page|>.

As example, a call to 
(which also require a call to
L<dokuwiki.login|>) is: 

    use Dokuwiki::RPC::XML::Client;
    use Modern::Perl;

    my $wiki =
        -> reach('');

    $wiki->login(qw( editor s3cr3t ))->value or die;
    say $wiki->getVersion->value;

=head1 the C<reach> constructor

to use the C<RPC::XML::Client::new> constructor directly, you have to remember the url of the RPC server 
and how to setup a cookie file so you stay connected after the 
L<dokuwiki.login|>) was called. 

So, as explained in
calling the constructor would be: 

    my $client =
        , useragent => [ cookie_jar => { file => "$ENV{HOME}/.cookies.txt" }] );

But you don't want to write it down? don't you? L<Dokuwiki::RPC::XML::Client> comes with a
wrapper called C<reach $class $url %options>; possible calls are

        ('' );

        ( base => '' );

        ( url => '' );

        , file => '/tmp/dokukookies.txt' );


C<%Dokuwiki::RPC::XML::Client::API> is a hash where keys are the
C<Dokuwiki::RPC::XML::Client> methods and values are the Dokuwiki XML-RPC
methods. So you can have the list of the mapped functions with:

    perl -MDokuwiki::RPC::XML::Client -E'
        say for keys %Dokuwiki::RPC::XML::Client::API 

but please refer to the 
L<dokuwiki xml-rpc page|> for more details.


getting the login and password from C<STDIN>, C<@ARGV> or hardcoded in your
source file is B<always> a bad idea. so this is an example to get things done
using the god damn old and good C<~/.netrc> file.

    use Dokuwiki::RPC::XML::Client;
    use Modern::Perl;
    use Net::Netrc;
    my $base = '';
    my $host = 'company';

    my $wiki =
        -> reach('');

    my $credentials = Net::Netrc->lookup($host)
        or die "please add a fake $host machine in your ~/.netrc";

    my ( $l, $p ) = $credentials->lpa;

    $wiki->login( $l, $p )->value 
        or die "can't authenticate with $l";

    say $wiki->getVersion->value;

=head1 FUTURE

i'm still experimenting to make my mind up about the right way to do it. it would
be nice to have both a raw C<RPC::XML::Client> as a singleton. then we could have
something usable in both way

    use aliased qw<Dokuwiki::RPC::XML::Client::Singleton D>;
    D::netrc 'unistra';
    D::login '';
    say "connected to dokuwiki version ". D::getVersion;
    say for grep /^metiers:/, ids_of D::getAllPages; 


    use Dokuwiki::RPC::XML::Client::Singleton ':all';
    netrc 'unistra';
    login '';
    say "connected to dokuwiki version ". getVersion;
    say for grep /^metiers:/, ids_of getAllPages;