Giter VIP home page Giter VIP logo

peopleapi's Introduction

A simple WebSharper-based REST API

This simple micro-service provides a CRUD interface for a database of people.

To keep things simple, the data is stored in a simple dictionary.

Here is an example HTTP session demonstrating the implemented API:

Request:                              | Response:
--------------------------------------+-----------------------------------------
GET /api/people/42                    | 200 OK
                                      |
                                      | { "id": 42,
                                      |   "firstName": "Alan",
                                      |   "lastName": "Turing",
                                      |   "born": "1912-06-23",
                                      |   "died": "1954-06-07" }
--------------------------------------+-----------------------------------------
GET /api/people                       | 200 OK
                                      |
                                      | [
                                      |   { "id": 1,
                                      |     "firstName": "Alonzo",
                                      |     "lastName": "Church",
                                      |     "born": "1903-6-14",
                                      |     "died": "1995-8-11" },
                                      |   // ...
                                      | ]
--------------------------------------+-----------------------------------------
POST /api/people                      | 200 OK
                                      |
{ "id": 0,                            | { "id": 43 }
  "firstName": "Noam",                |
  "lastName": "Chomsky",              |
  "born": "1928-12-07" }              |
--------------------------------------+-----------------------------------------
PUT /api/people                       | 404 Not Found
                                      |
{ "id": 44,                           | { "error": "Person not found." }
  "firstName": "Bertrand",            |
  "lastName": "Russell",              |
  "born": "1872-05-18",               |
  "died": "1970-02-02" }              |
--------------------------------------+-----------------------------------------
DELETE /api/people/43                 | 200 OK
                                      |
                                      | { "id": 43 }

Building and starting the application

Simply run the following commands:

cd src
dotnet run

Code walkthrough

The API is implemented in App.fs.

  • The module Model contains the types used by our application:

    • EndPoint and ApiEndPoint together describe the endpoints of the API. They describe the accepted URLs and JSON bodies.
    • PersonData, Id, Error and ApiResult describe the JSON data accepted and returned by the API.
  • The module Backend contains the back-end of the application:

    • A global dictionary which plays the role of the database.
    • Functions to create, get, edit and delete a person from this dictionary.
  • The module Site ties everything together. It creates a MultiPage site which parses EndPoints, calls the corresponding Backend functions, and returns the resulting value as HTTP JSON content.

Play along

There are plenty of ways you can extend this application to try your hand at WebSharper sitelets. Here are some suggestions:

  • Add pagination to GET /api/people.

    For this, you'll probably want to use query parameters, such as GET /api/people?pageId=3. You can do so using the Query attribute on your endpoint:

    type ApiEndPoint =
        | [<EndPoint "GET /api/people"; Query "pageId">]
            GetPeople of pageId: option<int>
  • Add a PATCH /api/people/{id} which allows setting only some fields of a person (as opposed to PUT which sets all fields).

    You'll need to create a new record type, which will be similar to Person except that all its fields should be optional.

Happy coding!

peopleapi's People

Contributors

tarmil avatar jand42 avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.