Skip to content

Commit

Permalink
First Zenefits connection
Browse files Browse the repository at this point in the history
  • Loading branch information
achlipala committed Apr 8, 2021
1 parent d91942e commit 5354965
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 4 deletions.
5 changes: 1 addition & 4 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
basic_in.ur
*.exe
*.sql
slackSecrets.ur
zoomSecrets.ur
oidcSecrets.ur
dropboxSecrets.ur
*Secrets.ur
11 changes: 11 additions & 0 deletions examples/zenefitsDemo.ur
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(* For this demo, it's necessary to create zenefitsScrets.ur,
* defining [api_token]. *)
structure Z = Zenefits.Make(Zenefits.TwoLegged(ZenefitsSecrets))

fun main () =
ps <- Z.People.list;
return <xml><body>
<ul>
{List.mapX (fn p => <xml><li>{[p.FirstName]} {[p.LastName]} (<tt>{[p.WorkEmail]}</tt>)</li></xml>) ps}
</ul>
</body></xml>
10 changes: 10 additions & 0 deletions examples/zenefitsDemo.urp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
library ../src/ur
rewrite all ZenefitsDemo/*
database dbname=zenefitsDemo
sql zenefitsDemo.sql
safeGetDefault
allow url https://*
prefix http://localhost:8080/

zenefitsSecrets
zenefitsDemo
1 change: 1 addition & 0 deletions examples/zenefitsDemo.urs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val main : unit -> transaction page
1 change: 1 addition & 0 deletions src/ur/lib.urp
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ salesforce
slack
zoom
openIdConnect
zenefits
70 changes: 70 additions & 0 deletions src/ur/zenefits.ur
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
open Json
open Urls

functor TwoLegged(M : sig
val api_token : string
end) = struct
open M

val token = return (Some api_token)
end

type person = {
FirstName : option string,
PreferredName : option string,
LastName : option string,
WorkEmail : option string,
PersonalEmail : option string
}
val _ : json person = json_record
{FirstName = "first_name",
PreferredName = "preferred_name",
LastName = "last_name",
WorkEmail = "work_email",
PersonalEmail = "personal_email"}

type subresults a = {
Data : list a,
NextUrl : option string
}
fun json_subresults [a] (_ : json a) : json (subresults a) =
json_record {Data = "data", NextUrl = "next_url"}

type results a = {
Data : subresults a
}
fun json_results [a] (_ : json a) : json (results a) =
json_record {Data = "data"}

functor Make(M : sig
val token : transaction (option string)
end) = struct
val token =
tok <- M.token;
case tok of
Some tok => return tok
| None => error <xml>How odd: no Zenefits token!</xml>

val urlPrefix = "https://api.zenefits.com/"

fun api url =
tok <- token;
WorldFfi.get url (Some ("Bearer " ^ tok)) True

fun apiPaged' [a] (_ : json a) (url : url) : transaction (list a) =
s <- api url;
debug (show url ^ " -> " ^ s);
r <- return (fromJson s : results a);
case r.Data.NextUrl of
None => return r.Data.Data
| Some nu =>
r' <- apiPaged' (bless nu);
return (List.append r.Data.Data r')

fun apiPaged [a] (_ : json a) (url : string) : transaction (list a) =
apiPaged' (bless (urlPrefix ^ url))

structure People = struct
val list = apiPaged "core/people"
end
end
25 changes: 25 additions & 0 deletions src/ur/zenefits.urs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
functor TwoLegged(M : sig
val api_token : string
end) : sig
val token : transaction (option string)
end

(** * Person API records *)

type person = {
FirstName : option string,
PreferredName : option string,
LastName : option string,
WorkEmail : option string,
PersonalEmail : option string
}

(** * Now for the actual methods.... *)

functor Make(M : sig
val token : transaction (option string)
end) : sig
structure People : sig
val list : transaction (list person)
end
end

0 comments on commit 5354965

Please sign in to comment.