stdweird / p5-net-freeipa

Perl5 NET::FreeIPA (FreeIPA 4.2+ JSON API)
Apache License 2.0
4 stars 3 forks source link

Add Request / Response types #18

Open NickCis opened 8 years ago

NickCis commented 8 years ago

As discussed in 13 PR. The idea is to add a Response class:

I think that storing the response in the Net::FreeIPA is something unnecesary, and the LWP::UserAgent aproach will be better. i.e.: the LWP::UserAgent performs requests that return a HTTP::Response object.

Code example:

 my $ua = LWP::UserAgent->new;

 my $response = $ua->get('http://search.cpan.org/');

 if ($response->is_success) {
     print $response->decoded_content;  # or whatever
 } else {
     die $response->status_line;
 }

I think that all the api calls would return a Net::FreeIPA::Response object (the get or post method and the HTTP::Response class would be an analogy of this). Net::FreeIPA::Error will be a subclass of Net::FreeIPA::Response.

The client would use the api in the following way:

my $fi = Net::FreeIPA->new("host.example.com");
die("Failed to initialise the rest client") if ! $fi->{rc};
my $response = $fi->api_user_find("");

# If the `Net::FreeIPA::Response overloads the bool operator
if($reponse){
# if it doesnot overload it
# if($response->is_success){ # or if(not($response->is_error){
    # More helper methods could be implemented, like $response->result_len or things that
    # will make easier for the client to work with the response
    print "Found ", scalar @{$response->result}, " users\n";
} else{
    print "Error , $response;
}

The Request class is done to provide batch support (#7)

stdweird commented 8 years ago

@NickCis this is now implemented, rpc takes request instance and returns response instance. the last rpc response is still store in an attribute, but that is purely for convenience, it's not used anymore.