Debian Codegraph @

README

raw
1 NAME
2 Catmandu::Importer::getJSON - load JSON-encoded data from a server using
3 a GET HTTP request
4
5 SYNOPSIS
6 The following three examples are equivalent:
7
8 Catmandu::Importer::getJSON->new(
9 file => \"http://example.org/alice.json\nhttp://example.org/bob.json"
10 )->each(sub { my ($record) = @_; ... );
11
12 Catmandu::Importer::getJSON->new(
13 url => "http://example.org",
14 file => \"/alice.json\n/bob.json"
15 )->each(sub { my ($record) = @_; ... );
16
17 Catmandu::Importer::getJSON->new(
18 url => "http://example.org/{name}.json",
19 file => \"{\"name\":\"alice\"}\n{\"name\":\"bob\"}"
20 )->each(sub { my ($record) = @_; ... );
21
22 For more convenience the catmandu command line client can be used:
23
24 echo http://example.org/alice.json | catmandu convert getJSON to YAML
25 catmandu convert getJSON --from http://example.org/alice.json to YAML
26 catmandu convert getJSON --dry 1 --url http://{domain}/robots.txt < domains
27
28 DESCRIPTION
29 This Catmandu::Importer performs a HTTP GET request to load JSON-encoded
30 data from a server. The importer expects a line-separated input. Each
31 line corresponds to a HTTP request that is mapped to a JSON-record on
32 success. The following input formats are accepted:
33
34 plain URL
35 A line that starts with ""http://"" or ""https://"" is used as plain
36 URL.
37
38 URL path
39 A line that starts with ""/"" is appended to the configured url
40 parameter.
41
42 variables
43 A JSON object with variables to be used with an URL template or as
44 HTTP query parameters. For instance the input line "{"name":"Karl
45 Marx"}" with URL "http://api.lobid.org/person" or the input line
46 "{"entity":"person","name":"Karl Marx"}" with URL template
47 "http://api.lobid.org/{entity}{?id}{?name}{?q}" are both expanded to
48 <http://api.lobid.org/person?name=Karl+Marx>.
49
50 If the JSON data returned in a HTTP response is a JSON array, its
51 elements are imported as multiple items. If a JSON object is returned,
52 it is imported as one item.
53
54 CONFIGURATION
55 url An URI or an URI templates (URI::Template) as defined by RFC 6570
56 <http://tools.ietf.org/html/rfc6570> to load JSON from. If no url is
57 configured, plain URLs must be provided as input or option "from"
58 must be used instead.
59
60 from
61 A plain URL to load JSON without reading any input lines.
62
63 timeout / agent / proxy / headers
64 Optional HTTP client settings.
65
66 client
67 Instance of a Furl HTTP client to perform requests with.
68
69 dry Don't do any HTTP requests but return URLs that data would be
70 queried from.
71
72 file / fh
73 Input to read lines from (see Catmandu::Importer). Defaults to
74 STDIN.
75
76 fix An optional fix to be applied on every item (see Catmandu::Fix).
77
78 wait
79 Number of seconds to wait between requests.
80
81 cache
82 Cache JSON response of URLs to not request the same URL twice. HTTP
83 error codes in the 4xx range (e.g. 404) are also cached but 5xx
84 errors are not.
85
86 The value of this option can be any objects that implements method
87 "get" and "set" (e.g. "CHI"), an existing directory for file
88 caching, a true value to enable global in-memory-caching, or a false
89 value to disable caching (default).
90
91 File caching uses file names based on MD5 of an URL so for instance
92 "http://example.org/" is cached as
93 "4389382917e51695b759543fdfd5f690.json".
94
95 warn
96 Show error messages on the standard error.
97
98 METHODS
99 time
100 Returns the UNIX timestamp right before the last request. This can be
101 used for instance to add timestamps or the measure how fast requests
102 were responded.
103
104 construct_url( [ $base_url, ] $vars_url_or_path )
105 Returns an URL given a hash reference with variables, a plain URL or an
106 URL path. The optional first argument can be used to override option
107 "url".
108
109 $importer->construct_url( %query_vars )
110 $importer->construct_url( $importer->url, %query_vars ) # equivalent
111
112 request($url)
113 Perform a HTTP GET request of a given URL including logging, caching,
114 request hook etc. Returns a hash/array reference or "undef".
115
116 EXTENDING
117 This importer provides two methods to filter requests and responses,
118 respectively. See Catmandu::Importer::Wikidata for an example.
119
120 request_hook
121 Gets a whitespace-trimmed input line and is expected to return an
122 unblessed hash reference, an URL, or undef. Errors are catched and
123 treated equal to undef.
124
125 response_hook
126 Gets the queried response object and is expected to return an object.
127
128 LOGGING
129 URLs are emitted before each request on DEBUG log level.
130
131 LIMITATIONS
132 Future versions of this module may also support asynchronous HTTP
133 fetching modules such as HTTP::Async, for retrieving multiple URLs at
134 the same time.
135
136 SEE ALSO
137 Catmandu::Fix::get_json provides this importer as fix function.
138
139 COPYRIGHT AND LICENSE
140 Copyright Jakob Voß, 2014-
141
142 This library is free software; you can redistribute it and/or modify it
143 under the same terms as Perl itself.
144