From a1d255f03855c33375f87e799bead332fa7d5116 Mon Sep 17 00:00:00 2001 From: Klavs Madsen Date: Sun, 5 Feb 2017 12:57:09 +0100 Subject: [PATCH] Added usage examples * Cleaned up project.json * Updated README with example on how to deserialize json request body * Added reference to System.Runtime.Serialization.Json as this would seems a common usage * Added C# example files to the /Examples directory --- environments/dotnet/FissionCompiler.cs | 3 +- environments/dotnet/FissionContext.cs | 10 +++++ environments/dotnet/README.md | 56 +++++++++++++++++++++++++- environments/dotnet/project.json | 40 +++++++++--------- examples/dotnet/arguments.cs | 11 +++++ examples/dotnet/helloworld.cs | 9 +++++ examples/dotnet/requestbody.cs | 24 +++++++++++ examples/dotnet/requestheaders.cs | 17 ++++++++ 8 files changed, 148 insertions(+), 22 deletions(-) create mode 100644 examples/dotnet/arguments.cs create mode 100644 examples/dotnet/helloworld.cs create mode 100644 examples/dotnet/requestbody.cs create mode 100644 examples/dotnet/requestheaders.cs diff --git a/environments/dotnet/FissionCompiler.cs b/environments/dotnet/FissionCompiler.cs index 99f236cd..f4dc4ee6 100644 --- a/environments/dotnet/FissionCompiler.cs +++ b/environments/dotnet/FissionCompiler.cs @@ -26,7 +26,8 @@ namespace Fission.DotNetCore.Compiler { MetadataReference.CreateFromFile(coreDir.FullName + Path.DirectorySeparatorChar + "mscorlib.dll"), MetadataReference.CreateFromFile(typeof(object).GetTypeInfo().Assembly.Location), - MetadataReference.CreateFromFile(Assembly.GetEntryAssembly().Location) + MetadataReference.CreateFromFile(Assembly.GetEntryAssembly().Location), + MetadataReference.CreateFromFile(typeof(System.Runtime.Serialization.Json.DataContractJsonSerializer).GetTypeInfo().Assembly.Location) }; foreach (var referencedAssembly in Assembly.GetEntryAssembly().GetReferencedAssemblies()) diff --git a/environments/dotnet/FissionContext.cs b/environments/dotnet/FissionContext.cs index a2dbeee0..b4117531 100644 --- a/environments/dotnet/FissionContext.cs +++ b/environments/dotnet/FissionContext.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.IO; using System.Security.Cryptography.X509Certificates; +using System.Text; using Nancy; namespace Fission.DotNetCore.Api @@ -84,6 +85,15 @@ namespace Fission.DotNetCore.Api } public Stream Body { get { return _request.Body; } } + + public string BodyAsString() + { + int length = (int)_request.Body.Length; + byte[] data = new byte[length]; + _request.Body.Read(data, 0, length); + return Encoding.UTF8.GetString(data); + } + public Dictionary> Headers { get diff --git a/environments/dotnet/README.md b/environments/dotnet/README.md index f3869862..3e9829d6 100644 --- a/environments/dotnet/README.md +++ b/environments/dotnet/README.md @@ -163,7 +163,7 @@ $ curl "http://$FISSION_ROUTER/add?x=30&y=12" 42 ``` -## Accessing Http request information example +## Accessing http request information example ### Setup fission environment First you need to setup the fission according to your cluster setup as @@ -216,6 +216,60 @@ Url: http://fissionserver:8888, method: GET ``` +## Accessing http request body example + +### Setup fission environment +First you need to setup the fission according to your cluster setup as +specified here: https://github.com/fission/fission + + +### Create the class to run + +Secondly you need to create a file /tmp/func.cs containing the following code: + +``` +using System.IO; +using System.Runtime.Serialization.Json; +using Fission.DotNetCore.Api; + +public class FissionFunction +{ + public string Execute(FissionContext context) + { + var person = Person.Deserialize(context.Request.Body); + return $"Hello, my name is {person.Name} and I am {person.Age} years old."; + } +} + +public class Person +{ + public string Name { get; set; } + public int Age { get; set; } + + public static Person Deserialize(Stream json) + { + var serializer = new DataContractJsonSerializer(typeof(Person)); + return (Person)serializer.ReadObject(json); + } +} + +``` +### Run the example + +Lastly to run the example: + +``` +$ fission env create --name dotnet --image fission/dotnet-env + +$ fission function create --name httpbody --env dotnet --code /tmp/func.cs + +$ fission route create --method GET --url /http_body --function httpbody + +$ curl -XPOST "http://$FISSION_ROUTER/http_body" -d '{ "Name":"Arthur", "Age":42}' +Hello, my name is Arthur and I am 42 years old. + +``` + ## Developing/debugging the enviroment locally diff --git a/environments/dotnet/project.json b/environments/dotnet/project.json index 2c1ea729..d4b6cb83 100755 --- a/environments/dotnet/project.json +++ b/environments/dotnet/project.json @@ -1,24 +1,24 @@ { - "version": "1.0.0-*", - "buildOptions": { - "emitEntryPoint": true, - "outputName": "fission-dotnet" - }, - - "dependencies": { - "Microsoft.NETCore.App": { - "version": "1.1.0", - "type": "platform" - }, - "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", - "Microsoft.AspNetCore.Owin": "1.0.0", - "Nancy": "2.0.0-barneyrubble", - "Microsoft.CodeAnalysis.CSharp": "1.3.0-*", - "System.Runtime.Loader": "4.0.0-*", + "version": "1.0.0-*", + "buildOptions": { + "emitEntryPoint": true, + "outputName": "fission-dotnet" }, - "frameworks": { - "netcoreapp1.0": { - "imports": "dnxcore50" + "frameworks": { + "netcoreapp1.0": { + "dependencies": { + "Microsoft.NETCore.App": { + "version": "1.1.0", + "type": "platform" + }, + "Microsoft.AspNetCore.Server.Kestrel": "1.0.0", + "Microsoft.AspNetCore.Owin": "1.0.0", + "Nancy": "2.0.0-barneyrubble", + "Microsoft.CodeAnalysis.CSharp": "1.3.0-*", + "System.Runtime.Loader": "4.0.0-*", + "System.Runtime.Serialization.Json": "4.0.2" + }, + "imports": "netstandard1.3" + } } - } } \ No newline at end of file diff --git a/examples/dotnet/arguments.cs b/examples/dotnet/arguments.cs new file mode 100644 index 00000000..a39e0421 --- /dev/null +++ b/examples/dotnet/arguments.cs @@ -0,0 +1,11 @@ +using System; +using Fission.DotNetCore.Api; + +public class FissionFunction +{ + public string Execute(FissionContext context){ + var x = Convert.ToInt32(context.Arguments["x"]); + var y = Convert.ToInt32(context.Arguments["y"]); + return (x+y).ToString(); + } + diff --git a/examples/dotnet/helloworld.cs b/examples/dotnet/helloworld.cs new file mode 100644 index 00000000..cae5a7e9 --- /dev/null +++ b/examples/dotnet/helloworld.cs @@ -0,0 +1,9 @@ +using Fission.DotNetCore.Api; + +public class FissionFunction +{ + public string Execute(FissionContext context) + { + return "Hello World!"; + } +} diff --git a/examples/dotnet/requestbody.cs b/examples/dotnet/requestbody.cs new file mode 100644 index 00000000..c4be5f44 --- /dev/null +++ b/examples/dotnet/requestbody.cs @@ -0,0 +1,24 @@ +using System.IO; +using System.Runtime.Serialization.Json; +using Fission.DotNetCore.Api; + +public class FissionFunction +{ + public string Execute(FissionContext context) + { + var person = Person.Deserialize(context.Request.Body); + return $"Hello, my name is {person.Name} and I am {person.Age} years old."; + } +} + +public class Person +{ + public string Name { get; set; } + public int Age { get; set; } + + public static Person Deserialize(Stream json) + { + var serializer = new DataContractJsonSerializer(typeof(Person)); + return (Person)serializer.ReadObject(json); + } +} diff --git a/examples/dotnet/requestheaders.cs b/examples/dotnet/requestheaders.cs new file mode 100644 index 00000000..8e44f2f7 --- /dev/null +++ b/examples/dotnet/requestheaders.cs @@ -0,0 +1,17 @@ +using System; +using Fission.DotNetCore.Api; + +public class FissionFunction +{ + public string Execute(FissionContext context){ + var buffer = new System.Text.StringBuilder(); + foreach(var header in context.Request.Headers){ + buffer.AppendLine(header.Key); + foreach(var item in header.Value){ + buffer.AppendLine($"\t{item}"); + } + } + buffer.AppendLine($"Url: {context.Request.Url}, method: {context.Request.Method}"); + return buffer.ToString(); + } +}