Skip to content

Commit

Permalink
Smartsheet.Templates.list
Browse files Browse the repository at this point in the history
  • Loading branch information
achlipala committed Aug 2, 2023
1 parent 5d4f44e commit cb110f8
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/smartsheetDemo.ur
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(* For this demo, it's necessary to create smartsheetSecrets.ur,
* defining [api_token]. *)
structure Z = Smartsheet.Make(Smartsheet.TwoLegged(SmartsheetSecrets))

val main =
ts <- Z.Templates.list;
return <xml><body>
<ul>
{List.mapX (fn r => <xml><li>{[r.Nam]}</li></xml>) ts}
</ul>
</body></xml>
10 changes: 10 additions & 0 deletions examples/smartsheetDemo.urp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library ../src/ur
rewrite all SmartsheetDemo/*
database dbname=smartsheetDemo
sql smartsheetDemo.sql
safeGetDefault
allow url https://*
prefix http://localhost:8080/

smartsheetSecrets
smartsheetDemo
1 change: 1 addition & 0 deletions examples/smartsheetDemo.urs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val main : transaction page
1 change: 1 addition & 0 deletions src/ur/lib.urp
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ openIdConnect
zenefits
netSuite
chatgpt
smartsheet
128 changes: 128 additions & 0 deletions src/ur/smartsheet.ur
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
open Json

signature AUTH = sig
val token : transaction (option string)
end

functor TwoLegged(M : sig
val api_token : string
end) = struct
val token = return (Some M.api_token)
end

datatype template_type =
Report
| Sheet

val _ : json template_type = json_derived
(fn x =>
case x of
"report" => Report
| "sheet" => Sheet
| _ => error <xml>Bad Smartsheet template type {[x]}</xml>)
(fn x =>
case x of
Report => "report"
| Sheet => "sheet")

datatype access_level =
ADMIN
| COMMENTER
| EDITOR
| EDITOR_SHARE
| OWNER
| VIEWER

val _ : json access_level = json_derived
(fn x => case x of
"ADMIN" => ADMIN
| "COMMENTER" => COMMENTER
| "EDITOR" => EDITOR
| "EDITOR_SHARE" => EDITOR_SHARE
| "OWNER" => OWNER
| "VIEWER" => VIEWER
| _ => error <xml>Bad Smartsheet access level {[x]}</xml>)
(fn x =>
case x of
ADMIN => "ADMIN"
| COMMENTER => "COMMENTER"
| EDITOR => "EDITOR"
| EDITOR_SHARE => "EDITOR_SHARE"
| OWNER => "OWNER"
| VIEWER => "VIEWER")

datatype global_template_type =
BLANK_SHEET
| PROJECT_SHEET
| TASK_LIST

val _ : json global_template_type = json_derived
(fn x => case x of
"BLANK_SHEET" => BLANK_SHEET
| "PROJECT_SHEET" => PROJECT_SHEET
| "TASK_LIST" => TASK_LIST
| _ => error <xml>Bad Smartsheet global template type {[x]}</xml>)
(fn x =>
case x of
BLANK_SHEET => "BLANK_SHEET"
| PROJECT_SHEET => "PROJECT_SHEET"
| TASK_LIST => "TASK_LIST")

type template = {
Id : option int,
Typ : option template_type,
AccessLevel : option access_level,
Blank : option bool,
Categories : option (list string),
GlobalTemplate : option global_template_type,
Image : option string,
LargeImage : option string,
Nam : string,
Tags : option (list string)
}

val _ : json template = json_record_withOptional
{Nam = "name"}
{Id = "id",
Typ = "type",
AccessLevel = "accessLevel",
Blank = "blank",
Categories = "categories",
GlobalTemplate = "globalTemplate",
Image = "image",
LargeImage = "largeImage",
Tags = "tags"}

type templates = {
Data : list template
}

val _ : json templates = json_record {Data = "data"}

functor Make(M : AUTH) = struct
open M

val token =
toko <- token;
case toko of
None => error <xml>You must be logged into Smartsheet to use this feature.</xml>
| Some tok => return tok

val prefix = "https://api.smartsheet.com/2.0/"

fun logged [a] (_ : show a) (t : transaction a) =
v <- t;
debug ("Smartsheet response: " ^ show v);
return v

fun api url =
tok <- token;
debug ("Smartsheet GET: " ^ prefix ^ url);
logged (WorldFfi.get (bless (prefix ^ url)) (WorldFfi.addHeader WorldFfi.emptyHeaders "Authorization" ("Bearer " ^ tok)) False)

structure Templates = struct
val list =
s <- api "templates?includeAll=true";
return ((fromJson s : templates).Data)
end
end
45 changes: 45 additions & 0 deletions src/ur/smartsheet.urs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
signature AUTH = sig
val token : transaction (option string)
end

functor TwoLegged(M : sig
val api_token : string
end) : sig
val token : transaction (option string)
end

datatype template_type =
Report
| Sheet

datatype access_level =
ADMIN
| COMMENTER
| EDITOR
| EDITOR_SHARE
| OWNER
| VIEWER

datatype global_template_type =
BLANK_SHEET
| PROJECT_SHEET
| TASK_LIST

type template = {
Id : option int,
Typ : option template_type,
AccessLevel : option access_level,
Blank : option bool,
Categories : option (list string),
GlobalTemplate : option global_template_type,
Image : option string,
LargeImage : option string,
Nam : string,
Tags : option (list string)
}

functor Make(M : AUTH) : sig
structure Templates : sig
val list : transaction (list template)
end
end

0 comments on commit cb110f8

Please sign in to comment.