-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.psgi
executable file
·134 lines (112 loc) · 4.29 KB
/
app.psgi
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
#!/usr/bin/perl
use lib 'lib';
use CGI::PSGI;
use Data::Dump qw( dump );
use Encode qw( decode_utf8 );
use JSON qw( encode_json );
use ProfilesEasyJSON::MegaUCSF;
use 5.8.8;
use utf8;
use strict;
use warnings;
my %valid_types = ( FNO => 'FNO',
Person => 'Person',
EmployeeID => 'EmployeeID',
EPPN => 'UserName',
ProfilesURLName => 'PrettyURL',
ProfilesNodeID => 'ProfilesNodeID',
URL => 'URL'
);
my $api = ProfilesEasyJSON::MegaUCSF->new;
my $app = sub {
my $env = shift;
my $q = CGI::PSGI->new($env);
my $params = $q->Vars;
my ( $identifier_type, $identifier, $error, $json );
my $http_status = 200;
# Did the user access /url_endpoint/something_random_and_unwanted ?
unless ( $q->path_info() =~ m{^/?$} ) {
my $suggested_api_url = $q->url() . '/';
$error
= { error =>
"This is an invalid API endpoint. Did you want to access $suggested_api_url ?"
};
$http_status = "404 Not found";
}
foreach my $key ( sort keys %valid_types ) {
if ( exists $params->{$key} ) {
$identifier_type = $valid_types{$key};
if ( $params->{$key} =~ m/^\s*(\S+)\s*$/ ) {
$identifier = $1;
} else {
my $identifier_printable = dump($identifier);
$error
||= { error =>
"Invalid argument '$identifier_printable' for identifier type $key"
};
$http_status = "400 Invalid argument sent";
}
last;
}
}
unless ($identifier_type) {
$error
||= { error =>
"You didn't specify an identifier type to look up! We were expecting to see one of the following: "
. join( ' / ', map {"?$_=..."} sort keys %valid_types ) };
$http_status = "400 Invalid argument sent";
}
unless ( ( $params->{source} and $params->{source} =~ m/\w\w\w/ )
or $q->referer() ) {
$error
||= { error =>
q{Missing source! Please send a source= parameter to let us know who's sending the request. For example, ?source=example.ucsf.edu if the data's being used on that website, or ?source=Foobar+University+XYZ+Tool for a script -- it doesn't have to be fancy, just some way to help us understand usage, and so we can get a hold of you in case of an emergency.}
};
$http_status = "400 Invalid argument sent";
}
if ( $identifier_type and $identifier and !$error ) {
my $options = {};
if ( $params->{cache} and $params->{cache} =~ m/^(always|never)$/ ) {
$options->{cache} = lc $1;
}
if ( $params->{timeout}
and $params->{timeout} =~ m/^\d+$/
and $params->{timeout} > 0 ) {
$options->{finish_by_time_in_epoch_seconds}
= time + $params->{timeout};
}
my $error_string = '';
$SIG{__WARN__} = sub { $error_string = $_[0]; chomp $error_string; };
$json = $api->identifier_to_json( $identifier_type, $identifier,
$options );
delete $SIG{__WARN__};
unless ($json) {
$error = { error => $error_string || 'Unknown error' };
if ( $error_string
=~ m/Tried to look up user.*but got no results|Could not look up identifier/i
) {
$http_status = "404 Could not find user";
}
}
}
# prepare output
my %header_options = ( -type => 'application/json',
-charset => 'utf-8',
-status => $http_status,
-access_control_allow_origin => '*',
);
my $output;
if ($error) {
$output = encode_json($error);
} else {
$output = $json;
}
if ( $params->{callback} ) {
$output = "$params->{callback}($output)";
$header_options{'-type'} = 'text/javascript';
}
return [ $q->psgi_header(%header_options), [$output] ];
};
# Local Variables:
# mode: perltidy
# End: