-
Notifications
You must be signed in to change notification settings - Fork 1
/
perl_client_sample.pl
executable file
·183 lines (143 loc) · 4.47 KB
/
perl_client_sample.pl
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
#!/usr/bin/perl
# Program: perl_client_sample.pl
# Usage: ./perl_client_sample.pl
# Purpose: perl language sample client program for Perl Petstore Enhanced API Server
# Copyright: James Briggs USA 2016
# Env: Perl5
# Returns: exit status is non-zero on failure
use strict;
use diagnostics;
use MIME::Base64 qw(encode_base64);
use LWP::UserAgent;
my $url = $ENV{'PETS_SCHEME'} . $ENV{'PETS_DOMAIN'} . $ENV{'PETS_BASE_URL'};
my $api_key = $ENV{'PETS_USER'};
my $api_password = $ENV{'PETS_PASSWORD'};
my $admin_api_key = $ENV{'PETS_ADMIN_USER'};
my $admin_api_password = $ENV{'PETS_ADMIN_PASSWORD'};
# Create a user agent object
my $ua = LWP::UserAgent->new;
$ua->agent("PetstorePerlBot/0.1");
$ua->from('perlbot@example.com');
$ua->timeout($ENV{'PETS_TIMEOUT'} || 10);
# Add auth token to user agent
$ua->default_header('Authorization' => 'Basic ' . encode_base64("$admin_api_key:$admin_api_password", ''));
{
# Create a request to do a simple health check
my $req = HTTP::Request->new(GET => $url . '/admin/ping');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
print "info: request #1 - simple health check\n";
# Check the outcome of the response
if ($res->is_success) {
print $res->content, "\n";
}
else {
print $res->status_line, "\n";
exit 6;
}
}
# Add auth token to user agent
$ua->default_header('Authorization' => 'Basic ' . encode_base64("$api_key:$api_password", ''));
{
# Create a request for the first page of pets
my $req = HTTP::Request->new(GET => $url . '/pets');
# Or add auth token to each request
$req->header('Authorization' => 'Basic ' . encode_base64("$api_key:$api_password", ''));
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
print "info: request #2 - show first page of pets\n";
# Check the outcome of the response
if ($res->is_success) {
print $res->content, "\n";
}
else {
print $res->status_line, "\n";
exit 1;
}
}
{
# Create a request for a pet
my $req = HTTP::Request->new(GET => $url . '/pets/1');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
print "info: request #3 - get one pet\n";
# Check the outcome of the response
if ($res->is_success) {
print $res->content, "\n";
}
else {
print $res->status_line, "\n";
exit 2;
}
}
{
# Create a request to add a pet with a POST override in the headers
my $req = HTTP::Request->new(POST => $url . '/pets');
$req->header('X-HTTP-Method-Override' => 'PUT');
$req->content_type('application/json');
$req->content('{"name": "zebra"}');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
print "info: request #4 - add a pet using POST override\n";
# Check the outcome of the response
if ($res->is_success) {
print $res->content, "\n";
}
else {
print $res->status_line, "\n";
exit 5;
}
}
{
# Create a request to add a duplicate pet
my $req = HTTP::Request->new(PUT => $url . '/pets');
$req->content_type('application/json');
$req->content('{"name": "alligator"}');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
print "info: request #5 - try to add a duplicate pet\n";
# Check the outcome of the response
if ($res->is_success) {
print $res->content, "\n";
}
else {
print $res->status_line, "\n";
# exit 4;
}
}
{
my $success = 0;
my $location = '';
# Create a request to add a pet
my $req = HTTP::Request->new(PUT => $url . '/pets');
$req->content_type('application/json');
$req->content('{"name": "zebra"}');
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
print "info: request #6 - add a pet\n";
# Check the outcome of the response
if ($res->is_success) {
print $res->content, "\n";
$location = $res->header('Location');
$success = 1;
}
else {
print $res->status_line, "\n";
exit 3;
}
if ($success and $location ne '') {
# Create a request to delete a pet
# Pass request to the user agent and get a response back
my $res = $ua->delete($location);
print "info: request #7 - delete a pet\n";
# Check the outcome of the response
if ($res->is_success) {
}
else {
print "location=$location\n";
print $res->status_line, "\n";
exit 3;
}
}
}
exit 0;