/
Bullhorn.pm
3082 lines (2313 loc) · 72.9 KB
/
Bullhorn.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#======================================================================
#
# NAME: Bullhorn.pm
#
# DESC: Bullhorn REST API
#
# ARGS:
#
# RET:
#
# HIST:
#
#======================================================================
# Copyright 2016 - Technology Resource Group LLC as an unpublished work
#======================================================================
package Bullhorn;
use strict;
use Data::Dumper;
use Encode;
use File::Basename;
use HTTP::Request::Common qw(GET POST PUT);
use JSON;
use LWP::UserAgent;
use MIME::Base64;
use Time::Local;
use URI::Escape;
#----------------------------------------------------------------------
#
# NAME: new
#
# DESC: Create a new Bullhorn object
#
# ARGS: $h1 - 'Bullhorn'
# $h2 - undef|HASH
#
# RETN: Bullhorn object
#
# HIST:
#
#----------------------------------------------------------------------
sub new {
my ($h1, $h2) = @_;
my $h = undef;
#------------------------------------------------------------
# class name - Bullhorn->new();
#------------------------------------------------------------
if($h1 eq 'Bullhorn') {
$h = $h2 if ref $h2 eq 'HASH';
}
else {
print STDERR
"Bullhorn::new ",
"ERROR: Invalid argument passed\n";
return undef;
}
#------------------------------------------------------------
# Define Bullhorn Object structure
#------------------------------------------------------------
my $d = { user_id => '',
user_pw => '',
client_id => '',
client_secret => '',
http_timeout => 30,
http_proxy => '',
https_proxy => '',
oauth_url => '',
rest_url => '',
auth_code => '',
access_href => { },
access_ts => 0,
login_href => { },
retry_wait => 20,
retry_max => 10,
debug => 0,
debug_http => 0,
debug_log => [ ],
error_log => [ ] };
#------------------------------------------------------------
# Enforce Bullhorn structure
# Copy each field if it exists with same ref type
#------------------------------------------------------------
foreach (keys %$d) {
if( exists $h->{$_} && (ref $h->{$_} eq ref $d->{$_}) ) {
$d->{$_} = $h->{$_};
}
}
#------------------------------------------------------------
# JSON object
#------------------------------------------------------------
my $json = JSON->new->allow_nonref;
$d->{json} = $json;
#------------------------------------------------------------
# LWP Object
#------------------------------------------------------------
my $ua = new LWP::UserAgent;
$ua->timeout( $d->{http_timeout} );
# Proxy Setup
#$ua->proxy( 'http', $d->{http_proxy} ) if $d->{http_proxy} ne "";
#$ua->proxy( 'https', $d->{https_proxy} ) if $d->{https_proxy} ne "";
#$req->proxy_authorization_basic("", "");
#$ENV{HTTP_PROXY} = $d->{http_proxy};
#$ENV{HTTP_PROXY_USERNAME} = '';
#$ENV{HTTP_PROXY_PASSWORD} = '';
#$ENV{HTTPS_PROXY} = $d->{https_proxy};
#$ENV{HTTPS_PROXY_USERNAME} = '';
#$ENV{HTTPS_PROXY_PASSWORD} = '';
$d->{ua} = $ua;
return bless $d, 'Bullhorn';
}
#----------------------------------------------------------------------
#
# NAME: error log
#
# DESC:
#
# ARGS:
#
# RETN:
#
# HIST:
#
#----------------------------------------------------------------------
sub add_error {
my ($self, $msg) = @_;
push @{$self->{error_log}}, $msg;
}
sub dump_error {
my ($self) = @_;
my $lastmeth;
foreach ( @{$self->{error_log}} ) {
my ($thismeth) = split /\:/, $_;
print "\n\n" if $thismeth ne $lastmeth;
$lastmeth = $thismeth;
print "$_\n";
}
}
#----------------------------------------------------------------------
#
# NAME: debug log
#
# DESC:
#
# ARGS:
#
# RETN:
#
# HIST:
#
#----------------------------------------------------------------------
sub add_debug {
my ($self, $msg) = @_;
push @{$self->{debug_log}}, $msg;
}
sub dump_debug {
my ($self) = @_;
my $lastmeth;
foreach ( @{$self->{debug_log}} ) {
my ($thismeth) = split /\:/, $_;
print "\n\n" if $thismeth ne $lastmeth;
$lastmeth = $thismeth;
print "$_\n";
}
}
#----------------------------------------------------------------------
#
# NAME: get_auth_code
#
# DESC: Get Authorization Code for BH REST API
#
#
# The first call is a GET to the URL below which nicludes the USER ID
# assigned by Bullhorn for the REST API. This returns the JSON below
# which contains various URLs - we are interested in oauthUrl and restUrl
# These are used to make calls to get/refresh the access token and login.
# If these URLs are not used then Bullhorn servers may redirect HTTP
# calls by returning a 307 code with a Location which this lib would
# have to handle.
#
# Once logged in another restUrl will be returned and set in
# login_href and it will be used for all other REST API calls.
#
#
# https://rest.bullhornstaffing.com/rest-services/loginInfo?username=trc.restapi
#
# {
# "atsUrl":"https://cls33.bullhornstaffing.com",
# "billingSyncUrl":"https://wfr-west.bullhornstaffing.com/billing-sync-services",
# "coreUrl":"https://cls33.bullhornstaffing.com/core",
# "documentEditorUrl":"https://docs-east.bullhornstaffing.com/document/",
# "mobileUrl":"https://m-west.bullhorn.com",
# "oauthUrl":"https://auth-west.bullhornstaffing.com/oauth",
# "restUrl":"https://rest-west.bullhornstaffing.com/rest-services",
# "samlUrl":"http://cls33.bullhornstaffing.com/BullhornStaffing/SAML/Login.cfm",
# "novoUrl":"https://app.bullhornstaffing.com",
# "pulseInboxUrl":"https://pulse-inbox.bullhornstaffing.com",
# "canvasUrl":"https://lasbigateway.bullhorn.com/canvas/cgi-bin/cognosisapi.dll",
# "npsSurveyUrl":"https://surveys-west.bullhorn.com{{path}}?sl=33&{{params}}",
# "ulUrl":"https://lasuniversal.bullhornstaffing.com/universal-login",
# "dataCenterId":3,
# "superClusterId":33
# }
#
#
#
# The second call should redirect back to the provided URL since that
# is the address we gave BH during setup of the API user.
# So we need to check the previous header to get 'Location'
# and get the auth code from the query string where param = 'code'
#
# https://auth.bullhornstaffing.com/oauth/authorize?
# client_id=<CLIENT ID>
# response_type=code
# username=<USER ID>
# password=<PASSWORD>
# action=Login
#
#
# ARGS:
#
# RETN: 0 - Success
# 1 - Error Check $self->{error_log}
#
# HIST:
#
#----------------------------------------------------------------------
sub get_auth_code {
my ($self) = @_;
my $func = "get_auth_code";
if( $self->{client_id} eq "") {
$self->add_error("$func: client_id not defined");
return 1;
}
if( $self->{user_id} eq "") {
$self->add_error("$func: user_id not defined");
return 1;
}
if( $self->{user_pw} eq "") {
$self->add_error("$func: user_pw not defined");
return 1;
}
#------------------------------------------------------------
# Call Bullhorn loginInfo to get URLs to use
#------------------------------------------------------------
my $rest_url = "https://rest.bullhornstaffing.com/rest-services";
$rest_url = $self->{rest_url} if $self->{rest_url} ne "";
my $url_info =
$rest_url . "/loginInfo?username=" . $self->{user_id};
$self->add_debug("$func: URL INFO = $url_info" . "\n")
if $self->{debug_http};
my $req_info = HTTP::Request->new(GET => $url_info);
$self->add_debug("$func: HTTP REQUEST:\n" .
$req_info->as_string . "\n") if $self->{debug_http};
my $info_href = { };
my $success = 0;
my $tries = 0;
# Allow multiple chances for success
while(! $success && $tries < $self->{retry_max}) {
$tries++;
my $res_info = $self->{ua}->request($req_info);
$self->add_debug(
"$func: HTTP RESPONSE:\n" .
$res_info->as_string . "\n") if $self->{debug_http};
my $info_href_json;
if($res_info->is_success) {
$info_href_json = $res_info->content;
$self->add_debug("$func: $info_href_json")
if $self->{debug_http};
$info_href = $self->{json}->decode($info_href_json);
$success = 1;
# If we had to retry, write a success msg to the error log
$self->add_error("$func: success on try $tries") if $tries > 1;
}
else {
$self->process_http_error($func, $req_info, $res_info, $tries);
}
}
# Assign OAuth and REST URLs
if( $info_href ) {
if( exists $info_href->{oauthUrl} &&
($info_href->{oauthUrl} ne "") ) {
$self->{oauth_url} = $info_href->{oauthUrl};
}
if( exists $info_href->{restUrl} &&
($info_href->{restUrl} ne "") ) {
$self->{rest_url} = $info_href->{restUrl};
}
}
else {
$self->add_error("$func: ERROR: " .
"Problem getting OAuth and REST URLs");
}
if( $self->{oauth_url} eq "" ) {
$self->add_error("$func: ERROR: OAuth URL is not set\n");
return 1;
}
if( $self->{rest_url} eq "" ) {
$self->add_error("$func: ERROR: REST URL is not set\n");
return 1;
}
#------------------------------------------------------------
# Make the call to Login
#------------------------------------------------------------
my $url =
$self->{oauth_url} . "/authorize?" .
qq(client_id=) . $self->{client_id} . "&" .
qq(response_type=code) . "&" .
qq(username=) . $self->{user_id} . "&" .
qq(password=) . $self->{user_pw} . "&" .
qq(action=Login);
$self->add_debug("$func: URL = $url" . "\n") if $self->{debug_http};
my $req = HTTP::Request->new(GET => $url);
$self->add_debug("$func: HTTP REQUEST:\n" .
$req->as_string . "\n") if $self->{debug_http};
my $success = 0;
my $tries = 0;
# Allow multiple chances for success
while(! $success && $tries < $self->{retry_max}) {
$tries++;
my $res = $self->{ua}->request($req);
$self->add_debug("$func: HTTP RESPONSE:\n" .
$res->as_string . "\n") if $self->{debug_http};
if($res->is_success) {
my $redirect_url = $res->previous->header('Location');
$self->add_debug("$func: LOC = $redirect_url")
if $self->{debug_http};
if( $redirect_url =~ /code=(.*)\&/ ) {
$self->{auth_code} = $1;
}
$success = 1;
# If we had to retry, write a success msg to the error log
$self->add_error("$func: success on try $tries") if $tries > 1;
}
else {
$self->process_http_error($func, $req, $res, $tries);
}
}
if( $self->{auth_code} eq "" ) {
$self->add_error("$func: ERROR: Did not get auth code");
return 1;
}
return 0;
}
#----------------------------------------------------------------------
#
# NAME: get_access_token
#
# DESC: Get Access Token for BH REST API
#
# https://auth.bullhornstaffing.com/oauth/token?
# grant_type=authorization_code
# code=<AUTH CODE>
# client_id=<CLIENT ID>
# client_secret=<CLIENT SECRET>
#
#
# Calling this returns JSON like this
#
# {
# "access_token" : "5:a6477004-44ee-4d22-a7e8-58105a6a4bef",
# "token_type" : "Bearer",
# "expires_in" : 600,
# "refresh_token" : "5:3daca6ef-6d12-4db4-b6d3-8da8a7a27b11"
# }
#
#
# ARGS:
#
# RETN: 0 - Success
# 1 - Error Check $self->{error_log}
#
# HIST:
#
#----------------------------------------------------------------------
sub get_access_token {
my ($self) = @_;
my $func = "get_access_token";
if( $self->{client_id} eq "") {
$self->add_error("$func: client_id not defined");
return 1;
}
if( $self->{client_secret} eq "") {
$self->add_error("$func: client_secret not defined");
return 1;
}
if( $self->{auth_code} eq "") {
$self->add_error("$func: auth_code not defined");
return 1;
}
# Initialize the timestamp for getting the access token
$self->{access_ts} = 0;
my $url =
$self->{oauth_url} . "/token?" .
qq(grant_type=authorization_code) . "&" .
qq(code=) . $self->{auth_code} . "&" .
qq(client_id=) . $self->{client_id} . "&" .
qq(client_secret=) . $self->{client_secret};
$self->add_debug("$func: URL = $url" . "\n")
if $self->{debug_http};
my $req = HTTP::Request->new(POST => $url);
$self->add_debug("$func: HTTP REQUEST:\n" .
$req->as_string . "\n") if $self->{debug_http};
my $success = 0;
my $tries = 0;
# Allow multiple chances for success
while(! $success && $tries < $self->{retry_max}) {
$tries++;
my $res = $self->{ua}->request($req);
$self->add_debug("$func: HTTP RESPONSE:\n" .
$res->as_string . "\n") if $self->{debug_http};
my $access_href_json;
if($res->is_success) {
$access_href_json = $res->content;
$self->add_debug("$func: $access_href_json")
if $self->{debug_http};
$self->{access_href} = $self->{json}->decode($access_href_json);
$success = 1;
# If we had to retry, write a success msg to the error log
$self->add_error("$func: success on try $tries") if $tries > 1;
}
else {
$self->process_http_error($func, $req, $res, $tries);
}
}
if( (! exists $self->{access_href}->{access_token} ) ||
($self->{access_href}->{access_token} eq "") ) {
$self->add_error("$func: ERROR: Did not get access_token");
return 1;
}
# Set the time when we got the access token so we
# know when to refresh later
$self->{access_ts} = time;
return 0;
}
#----------------------------------------------------------------------
#
# NAME: refresh_access_token
#
# DESC: Refresh Access Token for BH REST API
#
# https://auth.bullhornstaffing.com/oauth/token?
# grant_type=refresh_token
# refresh_token=<REFRESH TOKEN>
# client_id=<CLIENT ID>
# client_secret=<CLIENT SECRET>
#
#
# Calling this returns JSON like this
#
# {
# "access_token" : "5:a6472004-74ff-4d33-a7e8-91105a6a4bed",
# "token_type" : "Bearer",
# "expires_in" : 600,
# "refresh_token" : "5:3daca6de-6d12-4dc7-b6d3-3da8a7a27b08"
# }
#
#
# ARGS:
#
# RETN: 0 - Success
# 1 - Error Check $self->{error_log}
#
# HIST:
#
#----------------------------------------------------------------------
sub refresh_access_token {
my ($self) = @_;
my $func = "refresh_access_token";
if( $self->{client_id} eq "") {
$self->add_error("$func: client_id not defined");
return 1;
}
if( $self->{client_secret} eq "") {
$self->add_error("$func: client_secret not defined");
return 1;
}
if( $self->{access_href}->{refresh_token} eq "") {
$self->add_error("$func: refresh_token not defined");
return 1;
}
# Initialize the timestamp for getting the access token
$self->{access_ts} = 0;
my $url =
$self->{oauth_url} . "/token?" .
qq(grant_type=refresh_token) . "&" .
qq(refresh_token=) . $self->{access_href}->{refresh_token} . "&" .
qq(client_id=) . $self->{client_id} . "&" .
qq(client_secret=) . $self->{client_secret};
$self->add_debug("$func: URL = $url" . "\n")
if $self->{debug_http};
my $req = HTTP::Request->new(POST => $url);
$self->add_debug("$func: HTTP REQUEST:\n" .
$req->as_string . "\n") if $self->{debug_http};
my $success = 0;
my $tries = 0;
# Allow multiple chances for success
while(! $success && $tries < $self->{retry_max}) {
$tries++;
my $res = $self->{ua}->request($req);
if($res->is_success) {
my $access_href_json = $res->content;
$self->add_debug("$func: $access_href_json")
if $self->{debug_http};
$self->{access_href} = $self->{json}->decode($access_href_json);
$success = 1;
# If we had to retry, write a success msg to the error log
$self->add_error("$func: success on try $tries") if $tries > 1;
}
else {
$self->process_http_error($func, $req, $res, $tries);
}
}
if( (! exists $self->{access_href}->{access_token} ) ||
($self->{access_href}->{access_token} eq "") ) {
$self->add_error("$func: ERROR: Did not refresh access_token");
return 1;
}
# Set the time when we got the access token so we
# know when to refresh later
$self->{access_ts} = time;
return 0;
}
#----------------------------------------------------------------------
#
# NAME: check_access_token
#
# DESC: Check the access token to see if it needs a refresh
#
# ARGS:
#
# RETN: 0 - Success
# 1 - Error Check $self->{error_log}
#
# HIST:
#
#----------------------------------------------------------------------
sub check_access_token {
my ($self) = @_;
my $func = "check_access_token";
# Determine how long we have been using the current token
# Add 20 seconds
my $d = time - $self->{access_ts};
$self->add_debug("$func: time diff = $d")
if $self->{debug_http};
my $rc = 0;
if( $d > ($self->{access_href}->{expires_in} - 20) ) {
$rc = $self->refresh_access_token;
$self->add_debug("$func: refresh token - RET = $rc");
}
return $rc;
}
#----------------------------------------------------------------------
#
# NAME: login
#
# DESC: Login to the BH REST API
#
# https://rest.bullhornstaffing.com/rest-services/login?
# version=*
# access_token=<ACCESS TOKEN>
#
#
# Calling this returns JSON like this
#
# {
# "BhRestToken":"8b92f340-5edf-5bfe-1c52-479f2cf3ea47",
# "restUrl":"https://rest5.bullhornstaffing.com/rest-services/abc123/"
# }
#
#
# ARGS:
#
# RETN: 0 - Success
# 1 - Error Check $self->{error_log}
#
# HIST:
#
#----------------------------------------------------------------------
sub login {
my ($self) = @_;
my $func = "login";
if( $self->{access_href}->{access_token} eq "") {
$self->add_error("$func: access_token not defined");
return 1;
}
my $url =
$self->{rest_url} . "/login?" .
qq(version=*) . "&" .
qq(access_token=) . $self->{access_href}->{access_token};
$self->add_debug("$func: URL = $url" . "\n");
my $req = HTTP::Request->new(GET => $url);
$self->add_debug("$func: HTTP REQUEST:\n" .
$req->as_string . "\n") if $self->{debug_http};
my $success = 0;
my $tries = 0;
# Allow multiple chances for success
while(! $success && $tries < $self->{retry_max}) {
$tries++;
my $res = $self->{ua}->request($req);
$self->add_debug("$func: HTTP RESPONSE:\n" .
$res->as_string . "\n") if $self->{debug_http};
if($res->is_success) {
my $login_json = $res->content;
$self->add_debug("$func: $login_json");
$self->{login_href} = $self->{json}->decode($login_json);
$success = 1;
# If we had to retry, write a success msg to the error log
$self->add_error("$func: success on try $tries") if $tries > 1;
}
else {
$self->process_http_error($func, $req, $res, $tries);
}
}
if( (! exists $self->{login_href}->{restUrl} ) ||
($self->{login_href}->{restUrl} eq "") ) {
$self->add_error("$func: ERROR: Did not get REST URL");
return 1;
}
elsif( (! exists $self->{login_href}->{BhRestToken} ) ||
($self->{login_href}->{BhRestToken} eq "") ) {
$self->add_error("$func: ERROR: Did not get REST TOKEN");
return 1;
}
return 0;
}
#----------------------------------------------------------------------
#
# NAME: connect
#
# DESC: Function to call the 3 main functions to
# initiate the REST API for Bullhorn
#
# ARGS:
#
# RETN: 0 - Success
# 1 - Error Check $self->{error_log}
#
# HIST:
#
#----------------------------------------------------------------------
sub connect {
my ($self) = @_;
my $rc;
$rc = $self->get_auth_code;
return $rc if $rc;
$rc = $self->get_access_token;
return $rc if $rc;
$rc = $self->login;
return $rc if $rc;
return 0;
}
#----------------------------------------------------------------------
#
# NAME: meta_entity
#
# DESC: Get the meta data for an Entity
#
# https://rest5.bullhornstaffing.com/rest-services/abc123/
# meta/Candidate?
# BhRestToken=74e6ae9b-b5c2-41d0-867f-f6b7a36f16db
#
#
# ARGS: entity - BH entity name
# fields - comma delim list of fields to return
#
# RETN:
#
# HIST:
#
#----------------------------------------------------------------------
sub meta_entity {
my ($self, $entity, $fields) = @_;
my $func = "meta_entity";
if( $self->{login_href}->{restUrl} eq "") {
$self->add_error("$func: restUrl not defined");
return undef;
}
if( $self->{login_href}->{BhRestToken} eq "") {
$self->add_error("$func: BhRestToken not defined");
return undef;
}
if( $entity eq "") {
$self->add_error("$func: entity not defined");
return undef;
}
# Check if the access token needs to be refreshed
if($self->check_access_token) {
$self->add_error("$func: Problem checking access token");
return undef;
}
my $get = undef;
my $url =
$self->{login_href}->{restUrl} . qq(meta/$entity) . "?" .
qq(BhRestToken=) . $self->{login_href}->{BhRestToken};
$url .= "&" . qq(fields=$fields) if $fields ne "";
$self->add_debug("$func: URL = $url" . "\n");
my $req = HTTP::Request->new(GET => $url);
$self->add_debug("$func: HTTP REQUEST:\n" .
$req->as_string . "\n") if $self->{debug_http};
my $success = 0;
my $tries = 0;
# Allow multiple chances for success
while(! $success && $tries < $self->{retry_max}) {
$tries++;
my $res = $self->{ua}->request($req);
$self->add_debug("$func: HTTP RESPONSE:\n" .
$res->as_string . "\n") if $self->{debug_http};
if($res->is_success) {
my $get_json = $res->content;
$self->add_debug("$func: $get_json") if $self->{debug_http};
$get = $self->{json}->decode($get_json);
$success = 1;
# If we had to retry, write a success msg to the error log
$self->add_error("$func: success on try $tries") if $tries > 1;
}
else {
$self->process_http_error($func, $req, $res, $tries);
}
}
if( ! exists $get->{data} ) {
$self->add_error("$func: Did not get metadata for $entity");
}
return $get;
}
#----------------------------------------------------------------------
#
# NAME: get_entity
#
# DESC: Get a single Entity
#
# https://rest5.bullhornstaffing.com/rest-services/abc123/
# entity/123456/Candidate?
# BhRestToken=74e6ae9b-b5c2-41d0-867f-f6b7a36f16db
# fields=firstName,lastName,address,email
#
# JSON Returned
#
# {
# "data" : {
# "firstName" : "Karen",
# "lastName" : "Bullhorn",
# "address" : {
# "address1" : "1234, West Nowhere Street",
# "address2" : "",
# "city" : "Carlisle",
# "state" : "Massachusetts",
# "zip" : "01741",
# "countryID" : 1,
# "countryName" : "United States",
# "countryCode" : "US"
# },
# "email" : "joe@gmail.com",
# "_score" : 7.84939
# }
# }
#
#
# ARGS: entity - BH entity name
# id - BH Entity ID
# fields - comma delim list of fields to return
#
#
# RETN: Search Object from JSON
#
# HIST: