Jump to content
Syleron

Plugin API Request - Support Manager

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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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

×