Jump to content

Adding Attachments via API - webdav session


Recommended Posts

We are currently implementing a in built Widget to our applications to allow the logging of Requests into Hornbill to replace a current ZenDesk solution. This includes the ability to opt in  take a screenshot of the current screen.

We are trying to find more details or examples of using the 'webdav' session in conjunction with the invoking of the appropriate api items such as addAttachmentToEntit.

 https://api.hornbill.com/apps/com.hornbill.servicemanager/Attachment?op=addAttachmentToEntity

Has anyone got any examples for this this or any insight on how achieve this?

Cheers

Martyn

Link to comment
Share on other sites

Hi @Martyn Houghton,

The process would be:

  • HTTP POST the file to the users session folder in dav, providing the API key in the header in the same was as you do for other API calls: https://mdh-p01-api.hornbill.com/yourinstanceid/dav/session/yourfilename.png
  • Make an API call to apps/com.hornbill.servicemanager/Requests::attachFileFromSession as below to attach the file
  • HTTP DELETE the file from the users session folder (same URL as above)
<methodCall service="apps/com.hornbill.servicemanager/Requests" method="attachFileFromSession">
  <params>
    <requestId>IN00012345</requestId>
    <fileName>/session/yourfilename.png</fileName>
    <description>some file description</description>
    <visibility>trustedGuest</visibility>
  </params>
</methodCall>

Hope this helps,

Steve

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

@Steve G

We have made some progress on this but our developer is hitting some inconsistencies.

I've found that the screenshots aren't always successfully attaching to the ticket, I originally thought this was an issue on my end with how I was saving the images locally but after doing some digging it seems that there's an issue with attaching the images from the session.
 
When calling the attachFileFromSession method of the api, when its not worked the following is returned:

Text

<methodCallResult status="ok">
    <params>
        <strErrorMessage>failedToAttachFile</strErrorMessage>
        <exceptionDescription>&quot;EspMethodCall::invoke: Operation[apps/com.hornbill.servicemanager/Requests::systemAttachFileFromSession] /apps/com.hornbill.servicemanager/entities/Requests/fc_ops/systemAttachFileFromSession.js(31): error X1001: Uncaught There was an error in attaching the file to the request or in retrieving the file info. Details: EspMethodCall::invoke: Operation[data::entityAttachFile] The requested file &apos;Screenshot_1619092562.png&apos; does not exist.&quot;</exceptionDescription>
    </params>
    <flowCodeDebugState>
        <executionId>dfeae680-07fa-4e6f-a06b-0bf8f5d3fc7d</executionId>
    </flowCodeDebugState>
</methodCallResult>
Expand (9 lines) Collapse
When uploading the file to the webdav server, the status code of the request is 200. That is irrespective of whether it will be successfully attached or not.
 
I'm wondering if perhaps my upload logic is off, its strange because its hit and miss whether the file successfully uploads or not. The upload logic is as follows:

Text

$image_handler = fopen($image_path, 'rb');

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://mdh-p01-api.hornbill.com/idoxsd/dav/session/" . $file_name); // also have tried 'https://eurapi.hornbill.com/idoxsd/'
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: ESP-APIKEY '.self::WIDGET_API_KEY,
));
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_POST, 1); // have also tried PUT
curl_setopt($ch, CURLOPT_INFILE, $image_handler);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);

curl_exec($ch);

$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
Expand (20 lines) Collapse
Sending the subsequent HTTP DELETE request returns either a 204 status or 404, oddly enough sometimes a 404 is returned despite the file having been present and attached to the ticket.
 
If you wouldn't mind putting a post up regarding this on the forum when you have a chance, I would really appreciate it. If you need any additional information, please feel free to give me a shout
Link to comment
Share on other sites

@Steve G

We have made some progress on this but our developer is hitting some inconsistencies.

[14:46] David Hutchman
Hi Martyn, I've found that the screenshots aren't always successfully attaching to the ticket, I originally thought this was an issue on my end with how I was saving the images locally but after doing some digging it seems that there's an issue with attaching the images from the session. 
When calling the attachFileFromSession method of the api, when its not worked the following is returned:

Text

<methodCallResult status="ok">
    <params>
        <strErrorMessage>failedToAttachFile</strErrorMessage>
        <exceptionDescription>&quot;EspMethodCall::invoke: Operation[apps/com.hornbill.servicemanager/Requests::systemAttachFileFromSession] /apps/com.hornbill.servicemanager/entities/Requests/fc_ops/systemAttachFileFromSession.js(31): error X1001: Uncaught There was an error in attaching the file to the request or in retrieving the file info. Details: EspMethodCall::invoke: Operation[data::entityAttachFile] The requested file &apos;Screenshot_1619092562.png&apos; does not exist.&quot;</exceptionDescription>
    </params>
    <flowCodeDebugState>
        <executionId>dfeae680-07fa-4e6f-a06b-0bf8f5d3fc7d</executionId>
    </flowCodeDebugState>
</methodCallResult>
When uploading the file to the webdav server, the status code of the request is 200. That is irrespective of whether it will be successfully attached or not. 
I'm wondering if perhaps my upload logic is off, its strange because its hit and miss whether the file successfully uploads or not. The upload logic is as follows:

Text

$image_handler = fopen($image_path, 'rb');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://mdh-p01-api.hornbill.com/idoxsd/dav/session/" . $file_name); // also have tried 'https://eurapi.hornbill.com/idoxsd/'
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: ESP-APIKEY '.self::WIDGET_API_KEY,
));
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_POST, 1); // have also tried PUT
curl_setopt($ch, CURLOPT_INFILE, $image_handler);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($image_path));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_exec($ch);
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
Sending the subsequent HTTP DELETE request returns either a 204 status or 404, oddly enough sometimes a 404 is returned despite the file having been present and attached to the ticket.
 
Any advice on anything we are missing or doing wrong.
 
Cheers
Martyn
Link to comment
Share on other sites

Hi @Martyn Houghton,

The code looks ok, I think the issue may be with the delete - deleting the file from the session before attachFileFromSession can perform its logic. I've looked at the Service Manager code behind attachFileFromSession, and it actually handles the delete from the session for you, so performing the delete in your code is unnecessary.

I knocked up a very basic example PHP script, using your example from above, and had it attach 1000 images to a request, and all 1000 were successful:

<?

$image_path = "/Users/steveg/Pictures/me_new.jpg";

for ($x = 0; $x < 1000; $x++) {
    $file_name = "me_new".$x.".jpg";
    $url = "https://hhq-p01-api.hornbill.com/stevegdev/dav/session/" . $file_name;
    uploadFile($url, $image_path);
    attachFileToRequest($file_name);
}


function uploadFile($url, $image_path) {
    $image_handler = fopen($image_path, 'rb');
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Authorization: ESP-APIKEY myapikey'
    ));
    curl_setopt($ch, CURLOPT_UPLOAD, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_INFILE, $image_handler);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($image_path));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    
    curl_exec($ch);
    
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "[UPLOAD] [".$http_status."] [".$url."] [".$image_path."]\n";
    curl_close($ch);
}

function attachFileToRequest($file_name) {
    
    $ch = curl_init();

    $url = "https://hhq-p01-api.hornbill.com/stevegdev/xmlmc/apps/com.hornbill.servicemanager/Requests?method=attachFileFromSession";
    
    $request_id = "IN00013698";
    $file_in_session = "/session/".$file_name;

    $data = '<methodCall service="apps/com.hornbill.servicemanager/Requests" method="attachFileFromSession">
            <params>
                <requestId>'.$request_id.'</requestId>
                <fileName>'.$file_in_session.'</fileName>
                <description>some file description</description>
                <visibility>trustedGuest</visibility>
            </params>
            </methodCall>';

    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 15,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $data,
        CURLOPT_HTTPHEADER => array(
            'Authorization: ESP-APIKEY myapikey',
            'Content-Type: text/xmlmc',
            'Accept: application/json'
        ),
      ));
    
    $result = curl_exec($ch);
    
    $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $json_response = json_decode($result);
    $hornbill_api_status = $json_response->{'@status'} ? 'true' : 'false';
    echo "[ATTACH] [".$http_status."] [". $hornbill_api_status ."] [".$file_name."]\n";
    curl_close($ch);
}

?>

And the 1000 attachments against the request:

image.png

Hope this helps, 

Steve

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...