Live Helper Chat support forum.. Forum is locked. New place for questions - Github Discussions

You are not logged in.

Announcement

#1 2018-08-15 18:27:31

kdim
Member
Registered: 2018-08-15
Posts: 1

Custom REST API endpoints

Hi,
I am trying to add a custom endpoint in the API. So far, I

1) Added the path in swagger.json like this:

    "/restapi/myendpoint/{user_id}": {
      "get": {
        "tags": [
          "user"
        ],
        "summary": "",
        "description": "",
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "user_id",
            "in": "path",
            "description": "User ID",
            "required": true,
            "type": "string",
            "format": "int32"
          }
        ],
        "responses": {
          "200": {
            "description": "",
            "schema": {
            }
          },
          "400": {
            "description": "Error",
            "schema": {
            }
          }
        },
        "security": [
          {
            "login": []
          }
        ]
      }
    }

2) created a file named myendpoint.php under modules/lhrestapi :

[== PHP ==]
<?php

try {
    erLhcoreClassRestAPIHandler::validateRequest();

    $user = erLhcoreClassModelUser::fetch((int) $Params['user_parameters']['user_id']);

    if ($user instanceof erLhcoreClassModelUser) {

        $db = ezcDbInstance::get();

        echo json_encode(array('error' => false, 'result' => array('msg' => 'Status changed', 'online' => $user->hide_online == 0)));

    } else {
        throw new Exception('User could not be found!');
    }

} catch (Exception $e) {
    echo erLhcoreClassRestAPIHandler::outputResponse(array(
        'error' => true,
        'result' => $e->getMessage()
    ));
}

exit();

and

3) added this inside lhrestapi\module.php :

$ViewList['myendpoint'] = array(
    'params' => array('user_id')
);

When I try this endpoint, I am getting code 302 (a redirect to /index.php/) as if the route doesn't exist.

Any help will be appreciated. Thanks.

Last edited by kdim (2018-08-15 19:38:01)

Offline

#2 2018-08-16 09:07:04

PeopleInside
Administrator
From: Italy
Registered: 2014-04-10
Posts: 4,046
Website

Re: Custom REST API endpoints

Hi kdim,
welcome on the live helper chat forum.

I am really sorry but my basic help do not cover your request.
You can find some info about commercial support and Github issue here:
https://forum.livehelperchat.com/viewtopic.php?id=2255


lol PeopleInside - Live helper chat - free limited forum support!
wink For commercial support or GitHub [see FAQ here]
ops If you want to support this open source project, just donate [see support page]
glasses Something wrong with the forum? [contact a superhero]

Offline

#3 2018-08-19 16:31:09

remdex
Administrator
From: Lithuania
Registered: 2012-09-23
Posts: 3,661
Website

Re: Custom REST API endpoints

Hi,

1. You have to clear cache after you have do your changes. Or just develop with disable cache smile

Offline

Board footer