A [Fake] REST server using Azure Functions

Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure. Use Azure Functions to run a script or piece of code in response to a variety of events. [Ref]

You can setup a simple REST (Representational State Transfer) server for development purposes in Azure functions.

Let’s start.

Create a new function called FakeRest in new Function App project inside Visual studio.

Create a static class and setup a users list.

public static class FakeRest
    {
        private static IEnumerable<User> _users;

        static FakeRest()
        {
            _users = new List<User>
            {
                new User
                {
                    Id= 1,
                    FirstName= "Jaye",
                    LastName="Tumility",
                    Email="[email protected]",
                    Gender= "Male",
                    Avatar= "https://robohash.org/velcommodisoluta.jpg?size=150x150&set=set1"
                },
                new User
                {
                    Id= 2,
                    FirstName= "Winifield",
                    LastName="Donat",
                    Email="[email protected]",
                    Gender= "Male",
                    Avatar= "https://robohash.org/velcommodisoluta.jpg?size=150x150&set=set1"
                },
                new User
                {
                    Id= 3,
                    FirstName= "Deana",
                    LastName="Ritmeier",
                    Email="[email protected]",
                    Gender= "Female",
                    Avatar= "https://robohash.org/velcommodisoluta.jpg?size=150x150&set=set1"
                },
                new User
                {
                    Id= 4,
                    FirstName= "Brade",
                    LastName="Westwick",
                    Email="[email protected]",
                    Gender= "Male",
                    Avatar= "https://robohash.org/velcommodisoluta.jpg?size=150x150&set=set1"
                },
                new User
                {
                    Id= 5,
                    FirstName= "Marcille",
                    LastName="Cinnamond",
                    Email="[email protected]",
                    Gender= "Female",
                    Avatar= "https://robohash.org/velcommodisoluta.jpg?size=150x150&set=set1"
                }
            };
        }
}

GET

[FunctionName("FakeRestUserGet")]
public static IActionResult UserGet([HttpTrigger(AuthorizationLevel.Anonymous, "get"
      , Route = "fakerest/user/{id=id}")]HttpRequest req, int id, TraceWriter log, ExecutionContext 
{
      return new OkObjectResult(_users?.FirstOrDefault(x => x.Id == id));
}

POST – CREATE

[FunctionName("FakeRestUserCreate")]
public static IActionResult UserCreate([HttpTrigger(AuthorizationLevel.Anonymous, "post"
    , Route = "fakerest/user")]HttpRequest req, TraceWriter log, ExecutionContext context)
{
    try
    {
        var reader = new StreamReader(req.Body, Encoding.UTF8);
        var user = JsonConvert.DeserializeObject<User>(reader.ReadToEnd());
        return new OkObjectResult(user);
    }
    catch (Exception ex)
    {
        return new BadRequestObjectResult(ex.Message);
    }
}

PUT – UPDATE

[FunctionName("FakeRestUserUpdate")]
public static IActionResult UserUpdate([HttpTrigger(AuthorizationLevel.Anonymous, "put"
    , Route = "fakerest/user")]HttpRequest req, TraceWriter log, ExecutionContext context)
{
    try
    {
        var reader = new StreamReader(req.Body, Encoding.UTF8);
        var user = JsonConvert.DeserializeObject<User>(reader.ReadToEnd());
        if (_users.Any(x => x.Id == user?.Id))
            return new OkObjectResult(user);
        return new NotFoundObjectResult(user);
    }
    catch (Exception ex)
    {
        return new BadRequestObjectResult(ex.Message);
    }
}

DELETE

[FunctionName("FakeRestUserDelete")]
public static IActionResult UserDelete([HttpTrigger(AuthorizationLevel.Anonymous, "delete"
    , Route = "fakerest/user/{id=id}")]HttpRequest req, int id, TraceWriter log, ExecutionContext context)
{
    return new OkResult();
}

GET ALL

[FunctionName("FakeRestUserGetAll")]
public static IActionResult UserGetAll([HttpTrigger(AuthorizationLevel.Anonymous, "get"
    , Route = "fakerest/users")]HttpRequest req, TraceWriter log, ExecutionContext context)
{
    return new OkObjectResult(_users);
}

You can find the complete code with postman scripts at my Github repository.

FakeRest Function

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *