| 1 | |||
| 2 | package Net::DigitalNZ; | ||
| 3 | #Based heavily on Net::Twitter | ||
| 4 | |||
| 5 | $VERSION = "0.15"; | ||
| 6 | use 5.005; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | use URI::Escape; | ||
| 10 | use JSON::Any 1.19; | ||
| 11 | use LWP::UserAgent 2.032; | ||
| 12 | use Carp; | ||
| 13 | |||
| 14 | sub new { | ||
| 15 | my $class = shift; | ||
| 16 | |||
| 17 | my %conf; | ||
| 18 | |||
| 19 | if ( scalar @_ == 1 ) { | ||
| 20 | if ( ref $_[0] ) { | ||
| 21 | %conf = %{ $_[0] }; | ||
| 22 | } else { | ||
| 23 | croak "Bad argument \"" . $_[0] . "\" passed, please pass a hashref containing config values."; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | else { | ||
| 27 | %conf = @_; | ||
| 28 | } | ||
| 29 | $conf{apiurl} = 'http://api.digitalnz.org/' unless defined $conf{apiurl}; | ||
| 30 | |||
| 31 | ### Set useragents, HTTP Headers, source codes. | ||
| 32 | $conf{useragent} = "Net::DigitalNZ/$Net::DigitalNZ::VERSION (PERL)" | ||
| 33 | unless defined $conf{useragent}; | ||
| 34 | ### Allow specifying a class other than LWP::UA | ||
| 35 | |||
| 36 | $conf{no_fallback} = 0 unless defined $conf{no_fallback}; | ||
| 37 | |||
| 38 | ### Create an LWP Object to work with | ||
| 39 | $conf{ua} = LWP::UserAgent->new(); | ||
| 40 | |||
| 41 | |||
| 42 | $conf{ua}->env_proxy(); | ||
| 43 | |||
| 44 | $conf{response_error} = undef; | ||
| 45 | $conf{response_code} = undef; | ||
| 46 | $conf{response_method} = undef; | ||
| 47 | |||
| 48 | return bless {%conf}, $class; | ||
| 49 | } | ||
| 50 | |||
| 51 | ### Return a shallow copy of the object to allow error handling when used in | ||
| 52 | ### Parallel/Async setups like POE. Set response_error to undef to prevent | ||
| 53 | ### spillover, just in case. | ||
| 54 | |||
| 55 | sub clone { | ||
| 56 | my $self = shift; | ||
| 57 | bless { %{$self}, response_error => $self->{error_return_val} }; | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | sub get_error { | ||
| 63 | my $self = shift; | ||
| 64 | my $response = eval { JSON::Any->jsonToObj( $self->{response_error} ) }; | ||
| 65 | |||
| 66 | if ( !defined $response ) { | ||
| 67 | $response = { | ||
| 68 | request => undef, | ||
| 69 | error => "DIGITAL NZ RETURNED ERROR MESSAGE BUT PARSING OF THE JSON RESPONSE FAILED - " | ||
| 70 | . $self->{response_error} | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $response; | ||
| 75 | |||
| 76 | } | ||
| 77 | |||
| 78 | sub http_code { | ||
| 79 | my $self = shift; | ||
| 80 | return $self->{response_code}; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub http_message { | ||
| 84 | my $self = shift; | ||
| 85 | return $self->{response_message}; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub search { | ||
| 89 | my $self = shift; | ||
| 90 | my $query = shift; | ||
| 91 | my $params = shift; | ||
| 92 | |||
| 93 | my $url = $self->{apiurl} . "records/v1.json/?"; | ||
| 94 | $url .= 'api_key='. $self->{api_key}; | ||
| 95 | $url .= '&search_text='. uri_escape($query); | ||
| 96 | |||
| 97 | ### Make a string out of the args to append to the URL. | ||
| 98 | |||
| 99 | foreach my $name ( sort keys %{$params} ) { | ||
| 100 | # drop arguments with undefined values | ||
| 101 | next unless defined $params->{$name}; | ||
| 102 | $url .= "&" unless substr( $url, -1 ) eq "?"; | ||
| 103 | $url .= $name . "=" . uri_escape( $params->{$name} ); | ||
| 104 | |||
| 105 | } | ||
| 106 | my $retval; | ||
| 107 | ### Make the request, store the results. | ||
| 108 | my $req = $self->{ua}->get($url); | ||
| 109 | |||
| 110 | $self->{response_code} = $req->code; | ||
| 111 | $self->{response_message} = $req->message; | ||
| 112 | $self->{response_error} = $req->content; | ||
| 113 | |||
| 114 | undef $retval; | ||
| 115 | |||
| 116 | ### Trap a case where digitalnz could return a 200 success but give up badly formed JSON | ||
| 117 | ### which would cause it to die. This way it simply assigns undef to $retval | ||
| 118 | ### If this happens, response_code, response_message and response_error aren't going to | ||
| 119 | ### have any indication what's wrong, so we prepend a statement to request_error. | ||
| 120 | |||
| 121 | if ( $req->is_success ) { | ||
| 122 | $retval = eval { JSON::Any->jsonToObj( $req->content ) }; | ||
| 123 | |||
| 124 | if ( !defined $retval ) { | ||
| 125 | $self->{response_error} = | ||
| 126 | "DIGITALNZ RETURNED SUCCESS BUT PARSING OF THE RESPONSE FAILED - " . $req->content; | ||
| 127 | return $self->{error_return_val}; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | return $retval; | ||
| 131 | } | ||
| 132 | |||
| 133 | 1; | ||
| 134 | __END__ |