Ejemplo n.º 1
0
    echo "<h1>Client example</h1>";
    echo "<p>The following calls are some example of how can you use the restful webservice!</p>";
    //Example of auth method - This method returns the token
    echo "<h2 style='color:green;'>[POST] Service route: /user/auth</h2>";
    $auth = $serviceRequest->post("/user/auth", ["email" => "*****@*****.**", "password" => "123456"], null, false);
    Utility::debug($auth);
    //Set the token for authenticated methods
    $userToken = $auth->payload->data->token;
    //Example of get request to an unauthenticated route to list user
    echo "<h2 style='color:green;'>[GET] Service route: /user/list_user [TOKEN: {$userToken}]</h2>";
    $listUser = $serviceRequest->get("/user/list_user", null, null, false);
    Utility::debug($listUser);
    //Example of get request to an unauthenticated route to read single user
    echo "<h2 style='color:green;'>[GET] Service route: /user/read_user [TOKEN: {$userToken}]</h2>";
    $readUser = $serviceRequest->get("/user/read_user", ["id" => 1], null, false);
    Utility::debug($readUser);
    //Example of post request to an authenticated route to create an user
    echo "<h2 style='color:green;'>[POST] Service: /user/create [TOKEN: {$userToken}]</h2>";
    $create = $serviceRequest->post("/user/create", ["name" => "Darth Vader", "email" => "*****@*****.**", "password" => "123456"], $userToken, false);
    Utility::debug($create);
    //Example of put request to an authenticated route to update an user
    echo "<h2 style='color:green;'>[PUT] Service route: /user/update [TOKEN: {$userToken}]</h2>";
    $update = $serviceRequest->put("/user/update", ["id" => 1, "name" => "Luke Skywalker"], $userToken, false);
    Utility::debug($update);
    //Example of delete request to an authenticated route to delete an user
    echo "<h2 style='color:green;'>[DELETE] Service route: /user/delete [TOKEN: {$userToken}]</h2>";
    $delete = $serviceRequest->delete("/user/delete", ["id" => 2], $userToken, false);
    Utility::debug($delete);
} catch (Exception $e) {
    throw $e;
}