-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOsmApi.pm
142 lines (123 loc) · 3.35 KB
/
OsmApi.pm
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
#!/usr/bin/perl
# OsmApi.pm
# ---------
#
# Implements OSM API connectivity
#
# Part of the "osmtools" suite of programs
# Originally written by Frederik Ramm <[email protected]>; public domain
package OsmApi;
use strict;
use warnings;
use LWP::UserAgent;
our $prefs;
our $ua;
our $dummy;
sub setup {
$prefs = { "dryrun" => 1 };
open (PREFS, $ENV{HOME}."/.osmtoolsrc") or die "cannot open ". $ENV{HOME}."/.osmtoolsrc";
while(<PREFS>)
{
if (/^(\S+)\s*=\s*(\S*)/)
{
$prefs->{$1} = $2;
}
}
close (PREFS);
foreach my $required("username","password","apiurl")
{
die $ENV{HOME}."/.osmtoolsrc does not have $required" unless defined($prefs->{$required});
}
if (!defined($prefs->{instance}))
{
$prefs->{instance} = sprintf "%010x", $$ * rand(100000000);
open(PREFS, ">>".$ENV{HOME}."/.osmtoolsrc");
printf PREFS "instance=".$prefs->{instance};
close(PREFS);
}
$prefs->{apiurl} =~ m!https?://([^/]+)/!;
my $host = $1;
$host .= ":80" unless ($host =~ /:/);
$ua = LWP::UserAgent->new;
# warn "Creds" .join ",",($host, "", $prefs->{username}, $prefs->{password});
$ua->credentials($host, "FOSM", $prefs->{username}, $prefs->{password});
my $revision = '$Revision: 27365 $';
my $revno = 0;
$revno = $1 if ($revision =~ /:\s*(\d+)/);
$ua->agent("osmtools/$revno ($^O, ".$prefs->{instance}.")");
$ua->timeout(600);
$prefs->{debug} = $prefs->{dryrun} unless (defined($prefs->{debug}));
$dummy = HTTP::Response->new(200);
}
BEGIN
{
warn "Starting";
setup;
}
sub get
{
my $url = shift;
my $req = HTTP::Request->new(GET => $prefs->{apiurl}.$url);
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
return($resp);
}
sub put
{
my $url = shift;
my $body = shift;
dummylog("PUT", $url, $body);# if ($prefs->{dryrun});
my $req = HTTP::Request->new(PUT => $prefs->{apiurl}.$url);
$req->header("Content-type" => "text/xml");
$req->content($body) if defined($body);
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
return $resp;
}
sub post
{
my $url = shift;
my $body = shift;
return dummylog("POST", $url, $body) if ($prefs->{dryrun});
my $req = HTTP::Request->new(POST => $prefs->{apiurl}.$url);
$req->content($body) if defined($body);
$req->header("Content-type" => "text/xml");
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
return $resp;
}
sub delete
{
my $url = shift;
my $body = shift;
return dummylog("DELETE", $url, $body) if ($prefs->{dryrun});
my $req = HTTP::Request->new(DELETE => $prefs->{apiurl}.$url);
$req->header("Content-type" => "text/xml");
$req->content($body) if defined($body);
my $resp = $ua->request($req);
debuglog($req, $resp) if ($prefs->{"debug"});
return $resp;
}
sub debuglog
{
my ($request, $response) = @_;
printf STDERR "%s %s... %s %s (%db)\n",
$request->method(),
$request->uri(),
$response->code(),
$response->message(),
length($response->content());
}
sub dummylog
{
my ($method, $url, $body) = @_;
print STDERR "$method $url\n";
print STDERR "$body\n\n";
return $dummy;
}
sub set_timeout
{
my $to = shift;
$ua->timeout($to);
}
1;