| 1 | package WebService::ILS::XML; | ||
| 2 | |||
| 3 | use Modern::Perl; | ||
| 4 | |||
| 5 | =encoding utf-8 | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | WebService::ILS::JSON - WebService::ILS module for services with XML API | ||
| 10 | |||
| 11 | =head1 DESCRIPTION | ||
| 12 | |||
| 13 | To be subclassed | ||
| 14 | |||
| 15 | See L<WebService::ILS> | ||
| 16 | |||
| 17 | =cut | ||
| 18 | |||
| 19 | use Carp; | ||
| 20 | use HTTP::Request::Common; | ||
| 21 | use URI; | ||
| 22 | use XML::LibXML; | ||
| 23 | |||
| 24 | use parent qw(WebService::ILS); | ||
| 25 | |||
| 26 | sub with_get_request { | ||
| 27 | my $self = shift; | ||
| 28 | my $callback = shift or croak "No callback"; | ||
| 29 | my $url = shift or croak "No url"; | ||
| 30 | my $get_params = shift; # hash ref | ||
| 31 | |||
| 32 | my $uri = URI->new($url); | ||
| 33 | $uri->query_form($get_params) if $get_params; | ||
| 34 | my $request = HTTP::Request::Common::GET( $uri ); | ||
| 35 | my $response = $self->_request_with_auth($request); | ||
| 36 | return $self->process_xml_response($response, $callback); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub with_delete_request { | ||
| 40 | my $self = shift; | ||
| 41 | my $callback = shift or croak "No callback"; | ||
| 42 | my $error_callback = shift; | ||
| 43 | my $url = shift or croak "No url"; | ||
| 44 | |||
| 45 | my $request = HTTP::Request::Common::DELETE( $url ); | ||
| 46 | my $response = $self->_request_with_auth($request); | ||
| 47 | return 1 if $response->is_success; | ||
| 48 | |||
| 49 | return $self->_error_result( | ||
| 50 | sub { $self->process_invalid_xml_response($response, $error_callback); }, | ||
| 51 | $request, | ||
| 52 | $response | ||
| 53 | ); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub with_post_request { | ||
| 57 | my $self = shift; | ||
| 58 | my $callback = shift or croak "No callback"; | ||
| 59 | my $url = shift or croak "No url"; | ||
| 60 | my $post_params = shift || {}; # hash ref | ||
| 61 | |||
| 62 | my $request = HTTP::Request::Common::POST( $url, $post_params ); | ||
| 63 | my $response = $self->_request_with_auth($request); | ||
| 64 | return $self->process_xml_response($response, $callback); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub with_xml_request { | ||
| 68 | my $self = shift; | ||
| 69 | my $callback = shift or croak "No callback"; | ||
| 70 | my $error_callback = shift; | ||
| 71 | my $url = shift or croak "No url"; | ||
| 72 | my $dom = shift or croak "No XML document"; | ||
| 73 | my $method = shift || 'post'; | ||
| 74 | |||
| 75 | my $req_builder = "HTTP::Request::Common::".uc( $method ); | ||
| 76 | no strict 'refs'; | ||
| 77 | my $request = $req_builder->( $url ); | ||
| 78 | $request->header( 'Content-Type' => 'application/xml; charset=utf-8' ); | ||
| 79 | $request->content( $dom->toeString ); | ||
| 80 | $request->header( 'Content-Length' => bytes::length($request->content)); | ||
| 81 | my $response = $self->_request_with_auth($request); | ||
| 82 | return $self->process_xml_response($response, $callback, $error_callback); | ||
| 83 | } | ||
| 84 | |||
| 85 | sub process_xml_response { | ||
| 86 | my $self = shift; | ||
| 87 | my $response = shift or croak "No response"; | ||
| 88 | my $success_callback = shift; | ||
| 89 | my $error_callback = shift; | ||
| 90 | |||
| 91 | unless ($response->is_success) { | ||
| 92 | return $self->process_xml_error_response($response, $error_callback); | ||
| 93 | } | ||
| 94 | |||
| 95 | my $content_type = $response->header('Content-Type'); | ||
| 96 | die $response->as_string | ||
| 97 | unless $content_type && $content_type =~ m!application/xml!; | ||
| 98 | my $content = $response->decoded_content | ||
| 99 | or die $self->invalid_response_exception_string($response); | ||
| 100 | |||
| 101 | local $@; | ||
| 102 | |||
| 103 | my $doc = eval { XML::LibXML->load_xml( string => $content )->documentElement() }; | ||
| 104 | #XXX check XML::LibXML::Error | ||
| 105 | die "$@\nResponse:\n".$response->as_string if $@; | ||
| 106 | |||
| 107 | return $doc unless $success_callback; | ||
| 108 | |||
| 109 | my $res = eval { | ||
| 110 | $success_callback->($doc); | ||
| 111 | }; | ||
| 112 | die "$@\nResponse:\n$content" if $@; | ||
| 113 | return $res; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub process_xml_error_response { | ||
| 117 | my $self = shift; | ||
| 118 | my $response = shift or croak "No response"; | ||
| 119 | my $error_callback = shift; | ||
| 120 | |||
| 121 | my $content_type = $response->header('Content-Type'); | ||
| 122 | if ($content_type && $content_type =~ m!application/xml!) { | ||
| 123 | my $content = $response->decoded_content | ||
| 124 | or die $self->invalid_response_exception_string($response); | ||
| 125 | |||
| 126 | my $doc = eval { XML::LibXML->load_xml( string => $content )->documentElement() }; | ||
| 127 | #XXX check XML::LibXML::Error | ||
| 128 | die "$@\nResponse:\n$content" if $@; | ||
| 129 | |||
| 130 | if ($error_callback) { | ||
| 131 | return $error_callback->($doc); | ||
| 132 | } | ||
| 133 | |||
| 134 | die $self->_error_from_xml($doc) || "Invalid response:\n$content"; | ||
| 135 | } | ||
| 136 | die $self->invalid_response_exception_string($response); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _error_from_xml {}; | ||
| 140 | |||
| 141 | sub _first_child_content { | ||
| 142 | my $self = shift; | ||
| 143 | my $parent_elt = shift or croak "No parent element"; | ||
| 144 | my $tag = shift or croak "No child tag name"; | ||
| 145 | |||
| 146 | my $child_elts = $parent_elt->getElementsByTagName($tag) or return; | ||
| 147 | my $child_elt = $child_elts->shift or return; | ||
| 148 | return $child_elt->textContent; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _children_content { | ||
| 152 | my $self = shift; | ||
| 153 | my $parent_elt = shift or croak "No parent element"; | ||
| 154 | my $tag = shift or croak "No child tag name"; | ||
| 155 | |||
| 156 | my $child_elts = $parent_elt->getElementsByTagName($tag) or return; | ||
| 157 | return [ $child_elts->map( sub { $_[0]->textContent } ) ]; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub _xml_to_hash { | ||
| 161 | my $self = shift; | ||
| 162 | my $parent_elt = shift or croak "No parent element"; | ||
| 163 | my $tags = shift or croak "No children tag names"; | ||
| 164 | |||
| 165 | return { map { $_ => $self->_first_child_content($parent_elt, $_) } @$tags }; | ||
| 166 | } | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | __END__ |