forked from ciaranmahoney/php
-
Notifications
You must be signed in to change notification settings - Fork 6
/
insightly.php
1619 lines (1364 loc) · 42.7 KB
/
insightly.php
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
<?php
/**
Insightly PHP library for Insightly API
This library provides user friendly access to the version 2.1 REST API
for Insightly.
The library is built using PHP standard libraries.
(no third party tools required, so it will run out of the box
on most PHP environments).
The wrapper functions return native PHP objects (arrays and objects),
so working with them is easily done using built in functions.
USAGE:
Simply add insightly.php to your PHP include path,
then do the following to run a test suite:
require("insightly.php");
$i = new Insightly('your API key');
$i->test();
This will run an automatic test suite against your Insightly account.
If the methods you need all pass, you're good to go!
For convenience, you can also run test.php from the command line:
php test.php <your-api-key>
This will run the test suite as well.
If you are working with very large recordsets,
you should use ODATA filters to access data in smaller chunks.
This is a good idea in general to minimize server response times.
BASIC USE PATTERNS:
CREATE/UPDATE ACTIONS
These methods expect an object containing valid data fields for the object.
They will return a dictionary containing the object
as stored on the server (if successful)
or raise an exception if the create/update request fails.
You indicate whether you want to create a new item
by setting the record id to 0 or omitting it.
To obtain sample objects, you can do the following:
$contact = $i->addContact('sample');
$event = $i->addEvent('sample');
$organization = $i->addOrganization('sample');
$project = $i->addProject('sample');
This will return a random item from your account,
so you can see what fields are required,
along with representative field values.
SEARCH ACTIONS
These methods return a list of dictionaries containing the matching items.
For example to request a list of all contacts, you call:
$contacts = $i->getContacts()
SEARCH ACTIONS USING ODATA
Search methods recognize top, skip, orderby and filters parameters,
which you can use to page, order and filter recordsets.
These are passed via an associative array:
// get the first 200 contacts
$contacts = $i->getContacts(array("top" => 200));
// get the first 200 contacts, in first name descending order
$contacts = $i->getContacts(array("orderby" => 'FIRST_NAME desc', "top" => 200));
// get 200 records, after skipping the first 200 records
$contacts = $i->getContacts(array("top" => 200, "skip" => 200));
// get contacts where FIRST_NAME='Brian'
$contacts = $i->getContacts(array("filters" => array('FIRST_NAME=\'Brian\'')));
IMPORTANT NOTE: when using OData filters,
be sure to include escaped quotes around the search term.
Otherwise you will get a 400 (bad request) error.
These methods will raise an exception if the lookup fails,
or return a (possibly empty) list of objects if successful.
READ ACTIONS (SINGLE ITEM)
These methods will return a single object containing the requested item's properties.
$contact = $i->getContact(123456);
DELETE ACTIONS
These methods will return True if successful, or raise an exception.
e.g. $success = $i->deleteContact(123456)
IMAGE AND FILE ATTACHMENT MANAGEMENT
The API calls to manage images and file attachments have not yet been implemented in the PHP library. However you can access
these directly via our REST API
ISSUES TO BE AWARE OF
This library makes it easy to integrate with Insightly,
and by automating HTTPS requests for you,
eliminates the most common causes of user issues.
That said, the service is picky about rejecting requests
that do not have required fields, or have invalid field values
(such as an invalid USER_ID).
When this happens, you'll get a 400 (bad request) error.
Your best bet at this point is to consult the
API documentation and look at the required request data.
Write/update methods also have a dummy feature
that returns sample objects that you can use as a starting point.
For example, to obtain a sample task object, just call:
$task = $i->addTask('sample');
This will return one of the tasks from your Insightly account,
so you can get a sense of the fields and values used.
If you are working with large recordsets,
we strongly recommend that you use ODATA functions,
such as top and skip to page through recordsets
rather than trying to fetch entire recordsets in one go.
This both improves client/server communication,
but also minimizes memory requirements on your end.
TROUBLESHOOTING TIPS
One of the main issues API users run into during write/update operations
is a 400 error (bad request) due to missing required fields.
If you are unclear about what the server is expecting,
a good way to troubleshoot this is to do the following:
* Using the web interface, create the object in question
(contact, project, team, etc), and add sample data and child elements to it
* Use the corresponding getNNNN() method to retrieve this object via the web API
* Inspect the object's contents and structure
Read operations via the API are generally quite straightforward,
so if you get struck on a write operation, this is a good workaround,
as you are probably just missing a required field
or using an invalid element ID when referring
to something such as a link to a contact.
*/
class Insightly{
/**
* API key
*
* @var string
*/
private $apikey;
/**
* Class constructor accepting an API key
*
* @param string $apikey
*/
public function __construct($apikey){
$this->apikey = $apikey;
}
/**
* Gets a list of contacts
*
* @param array $options
* @return mixed
* @link https://api.insight.ly/v2.1/Help/Api/GET-Contacts_ids_email_tag
*/
public function getContacts($options = null){
$email = isset($options["email"]) ? $options["email"] : null;
$tag = isset($options["tag"]) ? $options["tag"] : null;
$ids = isset($options["ids"]) ? $options["ids"] : null;
$request = $this->GET("/v2.1/Contacts");
// handle standard OData options
$this->buildODataQuery($request, $options);
// handle other options
if($email != null){
$request->queryParam("email", $email);
}
if($tag != null){
$request->queryParam("tag", $tag);
}
if($ids != null){
$s = "";
foreach($ids as $key => $value){
if($key > 0){
$s = $s . ",";
}
$s = $s . $value;
}
$request->queryParam("ids", $s);
}
return $request->asJSON();
}
/**
* Gets a contact
*
* @param int $id
* @return mixed
* @link https://api.insight.ly/v2.1/Help/Api/GET-Contacts-id
*/
public function getContact($id){
return $this->GET("/v2.1/Contacts/" . $id)->asJSON();
}
/**
* Adds a contact
*
* @param stdClass $contact
* @return mixed
* @link https://api.insight.ly/v2.1/Help/Api/POST-Contacts
*/
public function addContact($contact){
$url_path = "/v2.1/Contacts";
$request = null;
if(isset($contact->CONTACT_ID) && $contact->CONTACT_ID > 0){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($contact)->asJSON();
}
public function deleteContact($id){
$this->DELETE("/v2.1/Contacts/$id")->asString();
return true;
}
public function getContactEmails($contact_id){
return $this->GET("/v2.1/Contacts/$contact_id/Emails")->asJSON();
}
public function getContactNotes($contact_id){
return $this->GET("/v2.1/Contacts/$contact_id/Notes")->asJSON();
}
public function getContactTasks($contact_id){
return $this->GET("/v2.1/Contacts/$contact_id/Tasks")->asJSON();
}
public function getCountries(){
return $this->GET("/v2.1/Countries")->asJSON();
}
public function getCurrencies(){
return $this->GET("/v2.1/Currencies")->asJSON();
}
public function getCustomFields(){
return $this->GET("/v2.1/CustomFields")->asJSON();
}
public function getCustomField($id){
return $this->GET("/v2.1/CustomFields/$id")->asJSON();
}
public function getEmails($options = null){
$request = $this->GET("/v2.1/Emails");
$this->buildODataQuery($request, $options);
return $request->asJSON();
}
public function getEmail($id){
return $this->GET("/v2.1/Emails/$id")->asJSON();
}
public function deleteEmail($id){
$this->DELETE("/v2.1/Emails/$id")->asString();
return true;
}
public function getEmailComments($email_id){
$this->GET("/v2.1/Emails/$email_id/Comments")->asJSON();
}
public function addCommentToEmail($email_id, $body, $owner_user_id){
$data = new stdClass();
$data->BODY = $body;
$data->OWNER_USER_ID = $owner_user_id;
return $this->POST("/v2.1/Emails/")->body($data)->asJSON();
}
public function getEvents($options = null){
$request = $this->GET("/v2.1/Events");
$this->buildODataQuery($request, $options);
return $request->asJSON();
}
public function getEvent($id){
return $this->GET("/v2.1/Events/$id")->asJSON();
}
public function addEvent($event){
if($event == "sample"){
$return = $this->getEvents(array("top" => 1));
return $return[0];
}
$url_path = "/v2.1/Events";
if(isset($event->EVENT_ID) && ($event->EVENT_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($event)->asJSON();
}
public function deleteEvent($id){
$this->DELETE("/v2.1/Events/$id")->asString();
return true;
}
public function getFileCategories(){
return $this->GET("/v2.1/FileCategories")->asJSON();
}
public function getFileCategory($id){
return $this->GET("/v2.1/FileCategories/$id")->asJSON();
}
public function addFileCategory($category){
if($category == "sample"){
$return = $this->getFileCategories();
return $return[0];
}
$url_path = "/v2.1/FileCategories";
if(isset($category->CATEGORY_ID)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($category)->asJSON();
}
public function deleteFileCategory($id){
$this->DELETE("/v2.1/FileCategories/$id")->asString();
return true;
}
public function getNotes($options = null){
$request = $this->GET("/v2.1/Notes");
$this->buildODataQuery($request, $options);
return $request->asJSON();
}
public function getNote($id){
return $request = $this->GET("/v2.1/Notes/$id")->asJSON();
}
public function addNote($note){
if($note == "sample"){
$return = $this->getNotes(array("top" => 1));
return $return[0];
}
$url_path = "/v2.1/Notes";
if(isset($note->NOTE_ID) && ($note->NOTE_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($note)->asJSON();
}
public function getNoteComments($note_id){
return $this->GET("/v2.1/Notes/$note_id/Comments")->asJSON();
}
public function addNoteComment($note_id, $comment){
if($comment == "sample"){
$comment = new stdClass();
$comment->COMMENT_ID = 0;
$comment->BODY = "This is a comment.";
$comment->OWNER_USER_ID = 1;
$comment->DATE_CREATED_UTC = "2014-07-15 16:40:00";
$comment->DATE_UPDATED_UTC = "2014-07-15 16:40:00";
return $comment;
}
return $this->POST("/v2.1/$note_id/Comments")->body($comment)->asJSON();
}
public function deleteNote($id){
$this->DELETE("/v2.1/Notes/$id")->asString();
return true;
}
public function getOpportunities($options = null){
$request = $this->GET("/v2.1/Opportunities");
$this->buildODataQuery($request, $options);
return $request->asJSON();
}
public function getOpportunity($id){
return $this->GET("/v2.1/Opportunities/" . $id)->asJSON();
}
public function addOpportunity($opportunity){
if($opportunity == "sample"){
$return = $this->getOpportunities(array("top" => 1));
return $return[0];
}
$url_path = "/v2.1/Opportunities";
if(isset($opportunity->OPPORTUNITY_ID) && ($opportunity->OPPORTUNITY_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($opportunity)->asJSON();
}
public function deleteOpportunity($id){
$this->DELETE("/v2.1/Opportunities/$id")->asString();
return true;
}
public function getOpportunityEmails($opportunity_id){
return $this->GET("/v2.1/Opportunities/$opportunity_id/Emails")->asJSON();
}
public function getOpportunityNotes($opportunity_id){
return $this->GET("/v2.1/Opportunities/$opportunity_id/Notes")->asJSON();
}
public function getOpportunityStateHistory($opportunity_id){
return $this->GET("/v2.1/Opportunities/$opportunity_id/StateHistory")->asJSON();
}
public function getOpportunityTasks($opportunity_id){
return $this->GET("/v2.1/Opportunities/$opportunity_id/Tasks")->asJSON();
}
public function getOpportunityCategories(){
return $this->GET("/v2.1/OpportunityCategories")->asJSON();
}
public function getOpportunityCategory($id){
return $this->GET("/v2.1/OpportunityCategories/$id")->asJSON();
}
public function addOpportunityCategory($category){
if($category == "sample"){
$return = $this->getOpportunityCategories();
return $return[0];
}
$url_path = "/v2.1/OpportunityCategories";
if(isset($category->CATEGORY_ID) && ($category->CATEGORY_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($category)->asJSON();
}
public function deleteOpportunityCategory($id){
$this->DELETE("/v2.1/OpportunityCategories/$id")->asString();
return true;
}
public function getOpportunityStateReasons(){
return $this->GET("/v2.1/OpportunityStateReasons")->asJSON();
}
public function getOrganizations($options = null){
$request = $this->GET("/v2.1/Organisations");
$this->buildODataQuery($request, $options);
return $request->asJSON();
}
public function getOrganization($id){
return $this->GET("/v2.1/Organisations/$id")->asJSON();
}
public function addOrganization($organization){
if($organization == "sample"){
$return = $this->getOrganizations(array("top" => 1));
return $return[0];
}
$url_path = "/v2.1/Organisations";
if(isset($organization->ORGANISATION_ID) && ($organization->ORGANISATION_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($organization)->asJSON();
}
public function deleteOrganization($id){
$this->DELETE("/v2.1/Organisations/$id")->asString();
return true;
}
public function getOrganizationEmails($organization_id){
return $this->GET("/v2.1/Organisations/$organization_id/Emails")->asJSON();
}
public function getOrganizationNotes($organization_id){
return $this->GET("/v2.1/Organisations/$organization_id/Notes")->asJSON();
}
public function getOrganizationTasks($organization_id){
return $this->GET("/v2.1/Organisations/$organization_id/Tasks")->asJSON();
}
public function getPipelines(){
return $this->GET("/v2.1/Pipelines")->asJSON();
}
public function getPipeline($id){
return $this->GET("/v2.1/Pipelines/$id")->asJSON();
}
public function getPipelineStages(){
return $this->GET("/v2.1/PipelineStages")->asJSON();
}
public function getPipelineStage($id){
return $this->GET("/v2.1/PipelineStages/$id")->asJSON();
}
public function getProjectCategories(){
return $this->GET("/v2.1/ProjectCategories")->asJSON();
}
public function getProjectCategory($id){
return $this->GET("/v2.1/ProjectCategories/$id")->asJSON();
}
public function addProjectCategory($category){
if($category == "sample"){
$return = $this->getProjectCategoriest();
return $return[0];
}
$url_path = "/v2.1ProjectCategories";
if(isset($category->CATEGORY_ID) && ($category->CATEGORY_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($category)->asJSON();
}
public function deleteProjectCategory($id){
$this->DELETE("/v2.1/ProjectCategories/$id")->asString();
return true;
}
public function getProjects($options = null){
$tag = isset($options["tag"]) ? $options["tag"] : null;
$ids = isset($options["ids"]) ? $options["ids"] : null;
$request = $this->GET("/v2.1/Projects");
// handle standard OData options
$this->buildODataQuery($request, $options);
// handle other options
if($tag != null){
$request->queryParam("tag", $tag);
}
if($ids != null){
$s = "";
foreach($ids as $key => $value){
if($key > 0){
$s = $s . ",";
}
$s = $s . $value;
}
$request->queryParam("ids", $s);
}
return $request->asJSON();
}
public function getProject($id){
return $this->GET("/v2.1/Projects/$id")->asJSON();
}
public function addProject($project){
if($project == "sample"){
$return = $this->getProjects();
return $return[0];
}
$url_path = "/v2.1/Projects";
if(isset($project->PROJECT_ID) && ($project->PROJECT_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($project)->asJSON();
}
public function deleteProject($id){
$this->DELETE("/v2.1/Projects/$id")->asString();
return true;
}
public function getProjectEmails($project_id){
return $this->GET("/v2.1/Projects/$project_id/Emails")->asJSON();
}
public function getProjectNotes($project_id){
return $this->GET("/v2.1/Projects/$project_id/Notes")->asJSON();
}
public function getProjectTasks($project_id){
return $this->GET("/v2.1/Projects/$project_id/Tasks")->asJSON();
}
public function getRelationships(){
return $this->GET("/v2.1/Relationships")->asJSON();
}
public function getTags($id){
return $this->GET("/v2.1/Tags/$id")->asJSON();
}
public function getTasks($options = null){
$request = $this->GET("/v2.1/Tasks");
$this->buildODataQuery($request, $options);
if(isset($options["ids"])){
$ids = "";
foreach($options["ids"] as $id){
$ids .= $id . ",";
}
$request.queryParam("ids", $ids);
}
return $request->asJSON();
}
public function getTask($id){
return $this->GET("/v2.1/Tasks/$id")->asJSON();
}
public function addTask($task){
if($task == "sample"){
$return = $this->getTasks(array("top" => 1));
return $return[0];
}
$url_path = "/v2.1/Tasks";
if(isset($task->TASK_ID) && ($task->TASK_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($task)->asJSON();
}
public function deleteTask($id){
$this->DELETE("/v2.1/Tasks/$id")->asString();
return true;
}
public function getTaskComments($task_id){
return $this->GET("/v2.1/Tasks/$task_id/Comments")->asJSON();
}
public function addTaskComment($task_id, $comment){
return $this->POST("/v2.1/Tasks/$task_id/Comments")->body($comment)->asJSON();
}
public function getTeams($options = null){
$request = $this->GET("/v2.1/Teams");
$this->buildODataQuery($request, $options);
return $request->asJSON();
}
public function getTeam($id){
return $this->GET("/v2.1/Teams/$id")->asJSON();
}
public function addTeam($team){
if($team == "sample"){
$return = $this->getTeams(array("top" => 1));
return $return[0];
}
$url_path = "/v2.1/Teams";
if(isset($team->TEAM_ID) && ($team->TEAM_ID > 0)){
$request = $this->PUT($url_path);
}
else{
$request = $this->POST($url_path);
}
return $request->body($team)->asJSON();
}
public function deleteTeam($id){
$this->DELETE("/v2.1/Teams/$id")->asString();
return true;
}
public function getTeamMembers($team_id){
return $this->POST("/v2.1/TeamMembers/teamid=$team_id")->asJSON();
}
public function getTeamMember($id){
return $this->POST("/v2.1/TeamMembers/$id")->asJSON();
}
public function addTeamMember($team_member){
if($team_member == "sample"){
$team_member = new stdClass();
$team_member->PERMISSION_ID = 1;
$team_member->TEAM_ID = 1;
$team_member->MEMBER_USER_ID = 1;
$team_member->MEMBER_TEAM_ID = 1;
return $team_member;
}
return $this->POST("/v2.1/TeamMembers")->body($team_member)->asJSON();
}
public function updateTeamMember($team_member){
return $this->PUT("/v2.1/TeamMembers")->body($team_member)->asJSON();
}
public function deleteTeamMember($id){
$this->DELETE("/v2.1/TeamMembers/$id")->asString();
return true;
}
public function getUsers(){
return $this->GET("/v2.1/Users")->asJSON();
}
public function getUser($id){
return $this->GET("/v2.1/Users/" . $id)->asJSON();
}
/**
* Add OData query filters to a request
*
* Accepted options:
* - top
* - skip
* - orderby
* - an array of filters
*
* @param InsightlyRequest $request
* @param array $options
* @return InsightlyRequest
* @link http://www.odata.org/documentation/odata-version-2-0/uri-conventions/
*/
private function buildODataQuery($request, $options){
$top = isset($options["top"]) ? $options["top"] : null;
$skip = isset($options["skip"]) ? $options["skip"] : null;
$orderby = isset($options["orderby"]) ? $options["orderby"] : null;
$filters = isset($options["filters"]) ? $options["filters"] : null;
if($top != null){
$request->queryParam('$top', $top);
}
if($skip != null){
$request->queryParam('$skip', $skip);
}
if($orderby != null){
$request->queryParam('$orderby', $orderby);
}
if($filters != null){
foreach($filters as $filter){
$filterValue = str_replace(array('=', '>', '<'),
array(' eq ', ' gt ', ' lt '),
$filter);
$request->queryParam('$filter', $filterValue);
}
}
return $request;
}
/**
* Create GET request
*
* @param string $url_path
* @return InsightlyRequest
*/
private function GET($url_path){
return new InsightlyRequest("GET", $this->apikey, $url_path);
}
/**
* Create PUT request
*
* @param string $url_path
* @return InsightlyRequest
*/
private function PUT($url_path){
return new InsightlyRequest("PUT", $this->apikey, $url_path);
}
/**
* Create POST request
*
* @param string $url_path
* @return InsightlyRequest
*/
private function POST($url_path){
return new InsightlyRequest("POST", $this->apikey, $url_path);
}
/**
* Create DELETE request
*
* @param string $url_path
* @return InsightlyRequest
*/
private function DELETE($url_path){
return new InsightlyRequest("DELETE", $this->apikey, $url_path);
}
/**
* Test all API library funtions
*
* @param int $top (Number of results in some requests)
* @throws Exception
*/
public function test($top=null){
echo "Test API .....\n";
echo "Testing authentication\n";
$passed = 0;
$failed = 0;
$currencies = $this->getCurrencies();
if(count($currencies) > 0){
echo "Authentication passed...\n";
$passed += 1;
}
else{
$failed += 1;
}
// Test getUsers()
try{
$users = $this->getUsers();
$user = $users[0];
$user_id = $user->USER_ID;
echo "PASS: getUsers(), found " . count($users) . " users.\n";
$passed += 1;
}
catch(Exception $ex){
$user = null;
$users = null;
$user_id = null;
echo "FAIL: getUsers()\n";
$failed += 1;
}
// Test getContacts()
try{
$contacts = $this->getContacts(array("orderby" => "DATE_UPDATED_UTC desc",
"top" => $top));
$contact = $contacts[0];
echo "PASS: getContacts(), found " . count($contacts) . " contacts.\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getContacts()\n";
$failed += 1;
}
if($contact != null){
$contact_id = $contact->CONTACT_ID;
try{
$emails = $this->getContactEmails($contact_id);
echo "PASS: getContactEmails(), found " . count($emails) . " emails.\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getContactEmails()\n";
$failed += 1;
}
try{
$notes = $this->getContactNotes($contact_id);
echo "PASS: getContactNotes(), found " . count($notes) . " notes.\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getContactNotes()\n";
$failed += 1;
}
try{
$tasks = $this->getContactTasks($contact_id);
echo "PASS: getContactTasks(), found " . count($tasks) . " tasks.\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getContactTasks()\n";
$failed += 1;
}
}
// Test addContact()
try{
$contact = (object)array("SALUTATION" => "Mr",
"FIRST_NAME" => "Testy",
"LAST_NAME" => "McTesterson");
$contact = $this->addContact($contact);
echo "PASS: addContact()\n";
$passed += 1;
// Test deleteContact()
try{
$this->deleteContact($contact->CONTACT_ID);
echo "PASS: deleteContact()\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: deleteContact()\n";
$failed += 1;
}
}
catch(Exception $ex){
$contact = null;
echo "FAIL: addContact()\n";
$failed += 1;
}
try{
$countries = $this->getCountries();
echo "PASS: getCountries(), found " . count($countries) . " countries.\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getCountries()\n";
$failed += 1;
}
try{
$currencies = $this->getCurrencies();
echo "PASS: getCurrencies(), found " . count($currencies) . " currencies\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getCurrencies()\n";
$failed += 1;
}
try{
$customfields = $this->getCustomFields();
echo "PASS: getCustomFields(), found " . count($customfields) . " custom fields.\n";
$passed += 1;
}
catch(Exception $ex){
echo "FAIL: getCustomFields()\n";
$failed += 1;
}
// Test getEmails()
try{