Jump to content

Plugin API Request - Support Manager


Syleron

Recommended Posts

I'm attempting to access support tickets via the API but no matter the request I keep getting the following error:


object(BlestaResponse)#7 (2) {

["raw":"BlestaResponse":private]=>

string(68) "{"message":"The requested resource does not exist.","response":null}"

["response_code":"BlestaResponse":private]=>

int(404)

}
 

Example Request:

 

    function getTicket($id) {
        $data = [
            'ticket_id' => $id,
            'get_replies' => true,
            'reply_types' => null,
            'staff_id' => null
        ];
        
        $response = $this->api->get("support_manager.support_manager_tickets", "get", $data);
        
        var_dump($response);
        
        return $response;
    }

Can someone please advise?

 

Cheers

Link to comment
Share on other sites

Syleron,

This is a bug within Blesta and more so with minPHP. Great find.

 

The issue is within app/controllers/api.php in the preAction function. The function tries to load the model from the url: support_manager.support_manager_tickets. 

app/controllers/api.php
// Ensure that the request is formatted correctly
if (isset($this->get[0]) && isset($this->get[1])) {
    $this->model = Loader::toCamelCase($this->get[0]);

It calls Loader:toCamelCase which returns

SupportManager.supportManagerTickets

What is expected is the following format:

SupportManager.SupportManagerTickets

On the surface this should not have caused an issue as if you called a function like get_declared_classes() it would show up as a class that was loaded. However, the problem is the case sensitive does manner when the api class calls method_exists and Router::isCallable. The minPHP function toCamelCase does not account for periods in its algorithm.

    /**
     * Convert a string to "CamelCase" from "file_case"
     *
     * @param string $str the string to convert
     * @return string the converted string           
     */
    public static function toCamelCase($str)
    {
        if (isset($str[0])) {
            $str[0] = strtoupper($str[0]);           
        }                                            
    
        return preg_replace_callback(                
            '/_([a-z])/',
            function ($c) {
                return strtoupper($c[1]);
            },
            $str
        );
    }

 

 

The Blesta team will have a permanent fix in place as there are many ways to solve this. For now, the below patch file will get things working on your end. Apply the following patch file with patch(1):

--- app/controllers/api.php	2017-03-14 10:23:18.000000000 -0700
+++ app/controllers/api.php	2017-07-05 21:35:18.337455862 -0700
@@ -68,7 +68,7 @@
 
         // Ensure that the request is formatted correctly
         if (isset($this->get[0]) && isset($this->get[1])) {
-            $this->model = Loader::toCamelCase($this->get[0]);
+            $this->model = str_replace('_', '', ucwords($this->get[0], '_.'));
             $this->model_name = Loader::toCamelCase(
                 strpos($this->model, '.') !== false
                 ? ltrim(strrchr($this->model, '.'), '.')

-Adam

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...