GithubHelp home page GithubHelp logo

anthrax3 / hyper-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tmitchel2/hyper

0.0 1.0 0.0 151 KB

A hypermedia api framework for building fully REST compliant internet scale web applications

C# 100.00%

hyper-1's Introduction

Hyper

A hypermedia api framework for building fully REST compliant internet scale web applications.

Features

  • Discoverability through hypertext aware media types.
  • Transport protocol independence (Tcp, Websocket, etc).
  • Transfer format independence (Json, Xml, Html, etc).
  • Client / Server independence enabling internet scale versioning across multiple domains.
  • Stateless communication (No server side session state) to enable internet level scaling.
  • Caching for improved network efficiency.

References

Example

Client

using (var client = new HyperClient(Application.GetTypes()))
{
  // Get Api
  var api = await client.Get<Api>(@"http://localhost:80");
  
  // Login - Create a session object using HTTP POST
  var session = await api.Sessions.Post(new Session { Username = "username", Password = "password" }, client);
  
  // Logout - Delete the session object using HTTP DELETE
  await session.Delete(client, HttpStatusCode.Unauthorized);
}

Server

// Configuration
var config = new HyperHttpSelfHostConfiguration"http://localhost:80", false);
config.AuthenticationCookieName = "hyper-auth";
config.WebApiUserNamePasswordValidator = new HyperUserNamePasswordValidator();
config.Routes.MapHttpRouteLowercase(
    name: "DefaultApi",
    routeTemplate: "{controller}/{id}",
    defaults: new { id = RouteParameter.Optional, controller = "Root" });

var types = GetTypes();
config.Formatters.Remove(config.Formatters.JsonFormatter);
config.Formatters.Add(new HyperJsonMediaTypeFormatter(types));
config.Formatters.Add(new HyperXmlMediaTypeFormatter(types));
config.MessageHandlers.Add(new RestQueryParameterHandler());
config.MessageHandlers.Add(new AuthenticationHandler(config));
config.Filters.Add(new BasicAuthenticationAttribute(config));
config.Services.Replace(typeof(ITraceWriter), new SimpleTracer());

// Start server
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
Console.ReadKey();

Server Model

[HyperContract(Name = "api", MediaType = "application/vnd.hypertests.api", Version = "1.0.0.0")]
public class Api : IHyperEntity<Api>
{
    [HyperLink(Rel = "self")]
    public HyperLink<Api> Self { get; set; }

    [HyperMember(Name = "name")]
    public string Name { get; set; }

    [HyperMember(Name = "version")]
    public Version Version { get; set; }

    [HyperLink(Rel = "types")]
    public HyperListLink<HyperType> Types { get; set; }
    
    [HyperLink(Rel = "sessions")]
    public HyperListLink<Session> Sessions { get; set; }

    [HyperLink(Rel = "users")]
    public HyperListLink<User> Users { get; set; }

    [HyperLink(Rel = "messages")]
    public HyperListLink<Message> Messages { get; set; }
}

Server Controller

public class RootController : ApiController
{
    [AllowAnonymous]
    public Api Get()
    {
        return new Api
            {
                Self = new HyperLink<Api>(ControllerContext.Request.RequestUri.ToString()),
                Name = Assembly.GetExecutingAssembly().GetName().Name,
                Version = Assembly.GetExecutingAssembly().GetName().Version,
                Types = new HyperListLink<HyperType>(GetRoute("Type")),
                Sessions = new HyperListLink<Session>(GetRoute("Session")),
                Users = new HyperListLink<User>(GetRoute("User")),
                Messages = new HyperListLink<Message>(GetRoute("Message"))
            };
    }

    private string GetRoute(string controller, string id = null)
    {
        return new Uri(Request.RequestUri, Url.Route("DefaultApi", new { controller, id })).ToString();
    }
}

Hypermedia Json

Run the HyperTests project and navigate to http://localhost/?accept=application/json

{
    "name": "HyperTests",
    "version": "0.0.1.0",
    "self": {
        "href": "http://localhost/?accept=application/json"
    },
    "types": {
        "href": "http://localhost/type?accept=application/json"
    },
    "sessions": {
        "href": "http://localhost/session?accept=application/json"
    },
    "users": {
        "href": "http://localhost/user?accept=application/json"
    },
    "messages": {
        "href": "http://localhost/message?accept=application/json"
    }
}

Status

Hyper is work in progress but development is active. Questions on usage accepted.

hyper-1's People

Contributors

tmitchel2 avatar

Watchers

 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.