App-wp-xmlrpc

 view release on metacpan or  search on metacpan

lib/App/wp/xmlrpc.pm  view on Meta::CPAN

package App::wp::xmlrpc;

our $DATE = '2017-04-24'; # DATE
our $VERSION = '0.003'; # VERSION

use 5.010001;
use strict;
use warnings;

our %SPEC;

my %args_common = (
    proxy => {
        schema => 'str*',
        req => 1,
        tags => ['common'],
    },
    blog_id => {
        schema => 'posint*',
        default => 1,
        tags => ['common'],
    },
    username => {
        schema => 'str*',
        req => 1,
        cmdline_aliases => {u=>{}},
        tags => ['common'],
    },
    password => {
        schema => 'str*',
        req => 1,
        cmdline_aliases => {p=>{}},
        tags => ['common'],
    },
);

# for each non-common arg, if the arg's value starts with '[' or '{' then it
# will be assumed to be JSON and will be JSON-decoded.
sub _convert_args_to_struct {
    require JSON::MaybeXS;

    my $args = shift;
    for my $k (keys %$args) {
        next if $args_common{$k};
        next unless $args->{$k} =~ /\A(?:\[|\{)/;
        eval { $args->{$k} = JSON::MaybeXS::decode_json($args->{$k}) };
        die "Invalid JSON in '$k' argument: $@\n" if $@;
    }
}

sub _api {
    require XMLRPC::Lite;

    my ($args, $method, $argnames) = @_;

    my @xmlrpc_args = (
        $method,
        $args->{blog_id},
        $args->{username},
        $args->{password},
        grep {defined} map { $args->{$_} } @$argnames,
    );

    my $call = XMLRPC::Lite->proxy($args->{proxy})->call(@xmlrpc_args);
    my $fault = $call->fault;
    if ($fault && $fault->{faultCode}) {
        return [$fault->{faultCode}, $fault->{faultString}];
    }
    [200, "OK", $call->result, {'cmdline.default_format'=>'json-pretty'}];
}

our %API_Methods = (
    # Posts
    'wp.getPost' => {
        args => [
            ['post_id*', {schema=>'posint*'}],
            ['fields',   {schema=>'str*'}],
        ],
    },
    'wp.getPosts' => {
        args => [
            ['filter',   {schema=>'str*'}],
        ],
    },
    'wp.newPost' => {
        args => [
            ['content*', {schema=>'str*'}],
        ],
    },
    'wp.editPost' => {
        args => [
            ['content*', {schema=>'str*'}],
        ],
    },
    'wp.deletePost' => {
        args => [
            ['post_id*', {schema=>'posint*'}],
        ],



( run in 1.406 second using v1.01-cache-2.11-cpan-7fcb06a456a )