Giter VIP home page Giter VIP logo

rust-odata's Introduction

rust-odata

Rust oData is library for building OData REST services in Rust. This library is referenced and utilized by the ConnectFour project in the game_server repository for service generation.

The online documentation for rust-odata can be found here.

What is oData?

OData (Open Data Protocol) is an ISO/IEC approved, OASIS standard that defines a set of best practices for building and consuming RESTful APIs. OData helps you focus on your business logic while building RESTful APIs without having to worry about the various approaches to define request and response headers, status codes, HTTP methods, URL conventions, media types, payload formats, query options, etc. OData also provides guidance for tracking changes, defining functions/actions for reusable procedures, and sending asynchronous/batch requests. [1]

Usage Overview

The library can be used to build REST API based using the Entity Data Model. EntityTypes and EntitySets are declared using macros which provide additional information used to generate metadata and create resource paths. For instance, we can declare a Game EntityType

// Creates a new Game struct and generates serveral methods used internally
defEntity!(Game(keys => id) {
    id: Int64,
    width: Int64,
    height: Int64,
    k: Int16,
    curr_player: Int16,
    status: String,
    board: String
});

and and EntitySet Games which consists of these types.

/// Declares Games as an EntitySet containing entities of type Game. Once added to
/// the model, it will be reachable via <...>/Games(I) 
defEntitySet!(Games, Game);

Now we can implemented CRUD operations for our sevice via the EntitySet trait:

 fn create(&self, value: serde_json::Value) -> Res { ... } 
 fn read(&self, key: String) -> Res { ... } 
 fn read_list(&self) -> Res { ... } 
 fn update ...
 fn delete ...

The EntitySet trait provides a default implementation for these methods, meaning that we only need to directly implement those that we plan on using. Lastly we need to instantiate the model using our EntitySet (here we only have one), as well as declare the name of our service and start listening for requests.

let model = ModelBuilder::new("connect_four.svc")
    .add(Games::declare())
    .build()
    
let svc = ServiceBuilder::new("api")
    .add(model)
    .build();

svc.start();

Now our clients can access our API using <hostname:port>/api/connect_four.svc to, for example, get a list of games by going to <hostname:port>/api/connect_four.svc/Games

rust-odata's People

Contributors

tov 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.