Skip to content

Instantly share code, notes, and snippets.

@LucWollants
Last active March 31, 2023 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LucWollants/3e73f974284431a651486be20438325c to your computer and use it in GitHub Desktop.
Save LucWollants/3e73f974284431a651486be20438325c to your computer and use it in GitHub Desktop.
Insightly Tests
<?php
$apiEndpoint = 'https://api.na1.insightly.com/v3.1/Contacts';
$apiKey = 'your-api-key';
//
// Create a new contact
//
$data = [
'FIRST_NAME' => 'Jane',
'LAST_NAME' => 'Doe',
'EMAIL_ADDRESS' => 'jane.doe@anonymous.com'
];
$options = [
CURLOPT_URL => $apiEndpoint,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Basic ' . base64_encode($apiKey . ':'),
'Content-Type: application/json'
],
CURLOPT_POSTFIELDS => json_encode($data),
];
$curl = curl_init();
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
curl_close($curl);
$data = json_decode($response, true);
$contactId = $data['CONTACT_ID'];
echo 'Contact Id: ' . $contactId . PHP_EOL . PHP_EOL;
//
// Get the contact by ID
//
$getOptions = array(
CURLOPT_URL => $apiEndpoint . '/' . $contactId,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ' . base64_encode($apiKey . ':'),
'Content-Type: application/json'
)
);
$getCurl = curl_init();
curl_setopt_array($getCurl, $getOptions);
$getResponse = curl_exec($getCurl);
curl_close($getCurl);
echo 'Contact GET by id' . $getResponse . PHP_EOL . PHP_EOL;
//
// Search the contact by email address
//
$getOptions = array(
CURLOPT_URL => $apiEndpoint . '/' . 'Search?field_name=EMAIL_ADDRESS&field_value=jane.doe%40anonymous&top=1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
'Authorization: Basic ' . base64_encode($apiKey . ':'),
'Content-Type: application/json'
)
);
$getCurl = curl_init();
curl_setopt_array($getCurl, $getOptions);
$getResponse = curl_exec($getCurl);
curl_close($getCurl);
if ($getResponse = '[]') {
echo '!!! NOTHING FOUND BY EMAIL BUT FOUND BY ID !!!' . PHP_EOL . PHP_EOL;
} else {
echo 'Contact found by email: ' . $getResponse . PHP_EOL . PHP_EOL;
}
//
// Delete the contact
//
$deleteOptions = [
CURLOPT_URL => $apiEndpoint . '/' . $contactId,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'DELETE',
CURLOPT_HTTPHEADER => [
'Authorization: Basic ' . base64_encode($apiKey . ':'),
'Content-Type: application/json'
]
];
$deleteCurl = curl_init();
curl_setopt_array($deleteCurl, $deleteOptions);
$deleteResponse = curl_exec($deleteCurl);
curl_close($deleteCurl);
echo $deleteResponse;
@LucWollants
Copy link
Author

Replace your-api-key

Scenario 1: searching on e-mail

Steps:

  • Create a contact and store the id
  • Getting the contact by id will always work
  • Searching the contact by email will sometimes fail (1 out of 10 executions will fail). Even adding a timeout of 5 seconds can result in failure.

Scenario 2: linking a contact to an opportunity

Steps:

  • Create an opportunity and store the id
  • Create a contact and store the id
  • Link the contact to the opportunity
  • Get the opportunity by id and sometimes the link will be missing even when the previous call was successful (1 out of 10 executions will fail). Even adding a timeout of 5 seconds can result in failure.

Problem

How can we build a polling mechanism to cover these failures?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment