File: XML.pm

package info (click to toggle)
libwebservice-ils-perl 0.18-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: perl: 2,645; makefile: 2; sh: 2
file content (184 lines) | stat: -rw-r--r-- 5,161 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package WebService::ILS::XML;

use Modern::Perl;

=encoding utf-8

=head1 NAME

WebService::ILS::JSON - WebService::ILS module for services with XML API

=head1 DESCRIPTION

To be subclassed

See L<WebService::ILS>

=cut

use Carp;
use HTTP::Request::Common;
use URI;
use XML::LibXML;

use parent qw(WebService::ILS);

sub with_get_request {
    my $self = shift;
    my $callback = shift or croak "No callback";
    my $url = shift or croak "No url";
    my $get_params = shift; # hash ref

    my $uri = URI->new($url);
    $uri->query_form($get_params) if $get_params;
    my $request = HTTP::Request::Common::GET( $uri );
    my $response = $self->_request_with_auth($request);
    return $self->process_xml_response($response, $callback);
}

sub with_delete_request {
    my $self = shift;
    my $callback = shift or croak "No callback";
    my $error_callback = shift;
    my $url = shift or croak "No url";

    my $request = HTTP::Request::Common::DELETE( $url );
    my $response = $self->_request_with_auth($request);
    return 1 if $response->is_success;

    return $self->_error_result(
        sub { $self->process_invalid_xml_response($response, $error_callback); },
        $request,
        $response
    );
}

sub with_post_request {
    my $self = shift;
    my $callback = shift or croak "No callback";
    my $url = shift or croak "No url";
    my $post_params = shift || {}; # hash ref

    my $request = HTTP::Request::Common::POST( $url, $post_params );
    my $response = $self->_request_with_auth($request);
    return $self->process_xml_response($response, $callback);
}

sub with_xml_request {
    my $self = shift;
    my $callback = shift or croak "No callback";
    my $error_callback = shift;
    my $url = shift or croak "No url";
    my $dom = shift or croak "No XML document";
    my $method = shift || 'post';

    my $req_builder = "HTTP::Request::Common::".uc( $method );
    no strict 'refs';
    my $request = $req_builder->( $url );
    $request->header( 'Content-Type' => 'application/xml; charset=utf-8' );
    $request->content( $dom->toeString );
    $request->header( 'Content-Length' => bytes::length($request->content));
    my $response = $self->_request_with_auth($request);
    return $self->process_xml_response($response, $callback, $error_callback);
}

sub process_xml_response {
    my $self = shift;
    my $response = shift or croak "No response";
    my $success_callback = shift;
    my $error_callback = shift;

    unless ($response->is_success) {
        return $self->process_xml_error_response($response, $error_callback);
    }

    my $content_type = $response->header('Content-Type');
    die $response->as_string
        unless $content_type && $content_type =~ m!application/xml!;
    my $content = $response->decoded_content
        or die $self->invalid_response_exception_string($response);

    local $@;

    my $doc = eval { XML::LibXML->load_xml( string => $content )->documentElement() };
    #XXX check XML::LibXML::Error
    die "$@\nResponse:\n".$response->as_string if $@;

    return $doc unless $success_callback;

    my $res = eval {
        $success_callback->($doc);
    };
    die "$@\nResponse:\n$content" if $@;
    return $res;
}

sub process_xml_error_response {
    my $self = shift;
    my $response = shift or croak "No response";
    my $error_callback = shift;

    my $content_type = $response->header('Content-Type');
    if ($content_type && $content_type =~ m!application/xml!) {
        my $content = $response->decoded_content
            or die $self->invalid_response_exception_string($response);

        my $doc = eval { XML::LibXML->load_xml( string => $content )->documentElement() };
        #XXX check XML::LibXML::Error
        die "$@\nResponse:\n$content" if $@;

        if ($error_callback) {
            return $error_callback->($doc);
        }

        die $self->_error_from_xml($doc) || "Invalid response:\n$content";
    }
    die $self->invalid_response_exception_string($response);
}

sub _error_from_xml {};

sub _first_child_content {
    my $self = shift;
    my $parent_elt = shift or croak "No parent element";
    my $tag = shift or croak "No child tag name";

    my $child_elts = $parent_elt->getElementsByTagName($tag) or return;
    my $child_elt = $child_elts->shift or return;
    return $child_elt->textContent;
}

sub _children_content {
    my $self = shift;
    my $parent_elt = shift or croak "No parent element";
    my $tag = shift or croak "No child tag name";

    my $child_elts = $parent_elt->getElementsByTagName($tag) or return;
    return [ $child_elts->map( sub { $_[0]->textContent } ) ];
}

sub _xml_to_hash {
    my $self = shift;
    my $parent_elt = shift or croak "No parent element";
    my $tags = shift or croak "No children tag names";

    return { map { $_ => $self->_first_child_content($parent_elt, $_) } @$tags };
}

1;

__END__

=head1 LICENSE

Copyright (C) Catalyst IT NZ Ltd
Copyright (C) Bywater Solutions

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=head1 AUTHOR

Srdjan Janković E<lt>srdjan@catalyst.net.nzE<gt>

=cut