Skip to content

Instantly share code, notes, and snippets.

@tamizhvendan
Last active October 13, 2016 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamizhvendan/68d9a97ae4cf4b6488eb6cf0170e7b89 to your computer and use it in GitHub Desktop.
Save tamizhvendan/68d9a97ae4cf4b6488eb6cf0170e7b89 to your computer and use it in GitHub Desktop.
HelloRestApi
#load "Suave.Newtonsoft.Json.fsx"
open Suave
open Suave.Successful
open Suave.Operators
open Suave.Filters
open System
open Suave.Newtonsoft.Json
type Person = {
Id : Guid
Name : string
Email : string
}
let createPerson person =
let newPerson = {person with Id = Guid.NewGuid()}
newPerson
let getPeople () = [
{Id = Guid.NewGuid(); Name = "john"; Email = "j@g.co"}
{Id = Guid.NewGuid(); Name = "mark"; Email = "m@g.co"}]
let getPersonById id =
{Id = Guid.Parse(id); Name = "john"; Email = "j@g.co"}
|> ToJson ok
let deletePersonById id =
sprintf "person %s deleted" id |> OK
let app =
choose [
path "/people" >=> choose [
POST >=> MapJson created createPerson
GET >=> ToJson ok (getPeople ())
PUT >=> MapJson accepted id
]
GET >=> pathScan "/people/%s" getPersonById
DELETE >=> pathScan "/people/%s" deletePersonById
]
{
"disabled": false,
"bindings": [
{
"type": "httpTrigger",
"name": "req",
"methods": [
"get",
"put",
"post",
"delete"
],
"authLevel": "anonymous",
"direction": "in"
},
{
"type": "http",
"name": "res",
"direction": "out"
}
]
}
{
"frameworks": {
"net46": {
"dependencies": {
"Suave": "1.1.3",
"Suave.Azure.Functions": "0.0.9",
"NewtonSoft.Json" : "9.0.1"
}
}
}
}
#load "app.fsx"
open App
open System.Net.Http
open Suave.Azure.Functions.Context
let Run (req : HttpRequestMessage, log : TraceWriter) =
try
let res = runWebPart app req |> Async.RunSynchronously
res
with
| ex ->
ex.StackTrace |> sprintf "%s" |> log.Info
let res = new HttpResponseMessage()
res
curl -X GET -H "X-Suave-Path: /people" "https://hellosuave123.azurewebsites.net/api/EmptyFSharp"
curl -X GET -H "X-Suave-Path: /people/793d9681-949d-4aaf-bed0-c3f89159dcb9" "https://hellosuave123.azurewebsites.net/api/EmptyFSharp"
curl -X POST -H "X-Suave-Path: /people" -H "Content-Type: application/json" -d '{
"name": "tam",
"email": "t@g.co"
}' "https://hellosuave123.azurewebsites.net/api/EmptyFSharp"
curl -X PUT -H "X-Suave-Path: /people" -H "Content-Type: application/json" -d '{
"id" : "8493785f-a09d-4b54-a70b-4a5a088b8573",
"name": "tam",
"email": "t@gmail.co"
}' "https://hellosuave123.azurewebsites.net/api/EmptyFSharp"
curl -X DELETE -H "X-Suave-Path: /people/793d9681-949d-4aaf-bed0-c3f89159dcb9" "https://hellosuave123.azurewebsites.net/api/EmptyFSharp"
open Newtonsoft.Json
open Newtonsoft.Json.Serialization
open System.Text
open Suave.Json
open Suave.Http
open Suave
open Suave.Operators
let toJson<'T> (o: 'T) =
let settings = new JsonSerializerSettings()
settings.ContractResolver <- new CamelCasePropertyNamesContractResolver()
JsonConvert.SerializeObject(o, settings)
|> Encoding.UTF8.GetBytes
let fromJson<'T> (bytes : byte []) =
let json = Encoding.UTF8.GetString bytes
JsonConvert.DeserializeObject(json, typeof<'T>) :?> 'T
let mapJsonWith<'TIn, 'TOut> (deserializer:byte[] -> 'TIn) (serializer:'TOut->byte[]) webpart f =
request(fun r ->
f (deserializer r.rawForm)
|> serializer
|> webpart
>=> Writers.setMimeType "application/json")
let MapJson<'T1,'T2> webpart = mapJsonWith<'T1,'T2> fromJson toJson webpart
let ToJson webpart x = toJson x |> webpart >=> Writers.setMimeType "application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment