This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction.fs
163 lines (140 loc) · 5.15 KB
/
Function.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
namespace JsonToCsv
open FSharp.Control.Tasks
open Google.Cloud.Functions.Framework
open Microsoft.AspNetCore.Http
open System.Text.Json
open System.Text.Json.Serialization
open System
open Microsoft.Net.Http.Headers
open Microsoft.Extensions.Primitives
open ClosedXML
open ClosedXML.Excel
open ClosedXML.SimpleSheets
type Todo =
{ userId: int
id: int
title: string
completed: bool }
type Post =
{ userId: int
id: int
title: string
body: string }
type Address =
{ street: string
suite: string
city: string
zipcode: string
geo: {| lat: string; lng: string |}
phone: string
website: string
company: {| name: string
catchPhrase: string
bs: string |} }
type User =
{ id: int
name: string
username: string
email: string
address: Address }
type RequestData =
{ todos: Todo list option
posts: Post list option
users: User list option }
[<AutoOpen>]
module FileWriter =
let private writeTodos (todos: Todo list) (worksheet: IXLWorksheet) =
Excel.populate (
worksheet,
todos,
[ Excel
.field(fun (todo: Todo) -> todo.id)
.header("Id")
Excel
.field(fun (todo: Todo) -> todo.userId)
.header("User Id")
Excel
.field(fun (todo: Todo) -> todo.title)
.header("Title")
Excel
.field(fun (todo: Todo) -> todo.completed)
.header("Is Completed") ]
)
let private writePosts (posts: Post list) (worksheet: IXLWorksheet) =
Excel.populate (
worksheet,
posts,
[ Excel
.field(fun (post: Post) -> post.id)
.header("Id")
Excel
.field(fun (post: Post) -> post.userId)
.header("User Id")
Excel
.field(fun (post: Post) -> post.title)
.header("Title")
Excel
.field(fun (post: Post) -> post.body)
.header("Content") ]
)
let private writeUsers (users: User list) (worksheet: IXLWorksheet) =
Excel.populate (
worksheet,
users,
[ Excel
.field(fun (user: User) -> user.id)
.header("Id")
Excel
.field(fun (user: User) -> user.email)
.header("E-Mail")
Excel
.field(fun (user: User) -> user.name)
.header("Name")
Excel
.field(fun (user: User) -> user.username)
.header("Username") ]
)
let writeExcelFile (data: RequestData) =
use workbook = new XLWorkbook()
// just write the sheets that are in the payload
data.todos
|> Option.iter (fun todos -> writeTodos todos (workbook.AddWorksheet("Todos")))
data.posts
|> Option.iter (fun posts -> writePosts posts (workbook.AddWorksheet("Posts")))
data.users
|> Option.iter (fun users -> writeUsers users (workbook.AddWorksheet("Users")))
// once we've added out worksheets we can write our excel file
Excel.createFrom (workbook)
type Function() =
let jsonOptions =
let opts = JsonSerializerOptions()
opts.Converters.Add(JsonFSharpConverter())
opts.AllowTrailingCommas <- true
opts.IgnoreNullValues <- true
opts
interface IHttpFunction with
/// <summary>
/// This function takes a request with a json body of the type RequestData then creates an Excel file from
/// that which gets written into the response of the request
/// </summary>
/// <param name="context">The HTTP context, containing the request and the response.</param>
/// <returns>A task representing the asynchronous operation.</returns>
member this.HandleAsync context =
task {
try
let! payload = JsonSerializer.DeserializeAsync<RequestData>(context.Request.Body, jsonOptions)
let excel = writeExcelFile payload
let bytes = ReadOnlyMemory excel
context.Response.Headers.Add(
HeaderNames.ContentDisposition,
StringValues("""attachment;filename="asExcel.xlsx";""")
)
context.Response.Headers.Add("Content-Type", StringValues(Excel.contentType))
do! context.Response.Body.WriteAsync bytes
with ex ->
eprintfn "%O" ex
context.Response.Headers.Add("Content-Type", StringValues("application/json"))
context.Response.StatusCode <- 500
do! context.Response.WriteAsync("""{ "message": "Something went wrong" }""")
}
:> _