Skip to content

Commit

Permalink
Implement libtwilio
Browse files Browse the repository at this point in the history
  • Loading branch information
pbiggar committed Feb 24, 2022
1 parent 5edb18f commit 1ebad53
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 124 deletions.
222 changes: 101 additions & 121 deletions fsharp-backend/src/BackendOnlyStdLib/LibTwilio.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ open Prelude

module Errors = LibExecution.Errors

module Canvas = LibBackend.Canvas

let fn = FQFnName.stdlibFnName

let err (str : string) = Ply(Dval.errStr str)
Expand All @@ -18,124 +20,102 @@ let incorrectArgs = LibExecution.Errors.incorrectArgs
let varA = TVariable "a"
let varB = TVariable "b"

let fns : List<BuiltInFn> = []
// [ { name = fn "Twilio" "sendText" 0
//
// ; parameters =
// [ Param.make "accountSID" TStr
// ; Param.make "authToken" TStr
// ; Param.make "fromNumber" TStr
// ; Param.make "toNumber" TStr
// ; Param.make "body" TStr ]
// ; returnType = TObj
// ; description =
// "Send text with `body` to phone number `toNumber` from number `fromNumber`, authenticated via `accountSID` and `authToken`"
// ; fn =
// (function
// | ( s
// , [ DStr accountSID
// ; DStr authToken
// ; DStr fromNumber
// ; DStr toNumber
// ; DStr body ] ) ->
// let basic_auth_string =
// Libhttpclient.encode_basic_auth_broken accountSID authToken
// in
// let encoding = "application/x-www-form-urlencoded" in
// let headers =
// [ ("Authorization", DStr basic_auth_string)
// ; ("Content-Type", Libexecution.DStr encoding)
// ]
// |> DvalMap.from_list
// |> DObj
// in
// let host_url = Canvas.url_for s.canvas_id in
// let body =
// [ ("From", DStr fromNumber)
// ; ("To", DStr toNumber)
// ; ("Body", DStr body)
// ; ("ValidityPeriod", Libexecution.DStr "900")
// ; ( "StatusCallback"
// , Libexecution.DStr
// (host_url ^ "/twilioCallback") ) ]
// |> DvalMap.fromList
// |> DObj
// in
// let twilio_uri =
// "https://api.twilio.com/2010-04-01/Accounts/"
// ^ Libexecution.Unicode_string.to_string accountSID
// ^ "/Messages.json"
// in
// Legacy.LibhttpclientV2.send_request
// twilio_uri
// Httpclient.POST
// Libexecution.Dval.to_pretty_machine_json_v1
// body
// (DObj DvalMap.empty)
// headers
// | _ ->
// incorrectArgs ())
// ; sqlSpec = NotYetImplementedTODO
// ; previewable = Impure
// ; deprecated =
// true
// (* Deprecated due to using Libhttpclient.encode_basic_auth_broken *) }
// ; { name = fn "Twilio" "sendText" 1
// ; parameters =
// [ Param.make "accountSID" TStr
// ; Param.make "authToken" TStr
// ; Param.make "fromNumber" TStr
// ; Param.make "toNumber" TStr
// ; Param.make "body" TStr ]
// ; returnType = TObj
// ; description =
// "Send text with `body` to phone number `toNumber` from number `fromNumber`, authenticated via `accountSID` and `authToken`"
// ; fn =
// (function
// | ( s
// , [ DStr accountSID
// ; DStr authToken
// ; DStr fromNumber
// ; DStr toNumber
// ; DStr body ] ) ->
// let basic_auth_string =
// Libhttpclient.encode_basic_auth accountSID authToken
// in
// let encoding = "application/x-www-form-urlencoded" in
// let headers =
// [ ("Authorization", DStr basic_auth_string)
// ; ("Content-Type", Libexecution.DStr encoding)
// ]
// |> DvalMap.from_list
// |> DObj
// in
// let host_url = Canvas.url_for s.canvas_id in
// let body =
// [ ("From", DStr fromNumber)
// ; ("To", DStr toNumber)
// ; ("Body", DStr body)
// ; ("ValidityPeriod", Libexecution.DStr "900")
// ; ( "StatusCallback"
// , Libexecution.DStr
// (host_url ^ "/twilioCallback") ) ]
// |> DvalMap.fromList
// |> DObj
// in
// let twilio_uri =
// "https://api.twilio.com/2010-04-01/Accounts/"
// ^ Libexecution.Unicode_string.to_string accountSID
// ^ "/Messages.json"
// in
// Legacy.LibhttpclientV2.send_request
// twilio_uri
// Httpclient.POST
// Libexecution.Dval.to_pretty_machine_json_v1
// body
// (DObj DvalMap.empty)
// headers
// | _ ->
// incorrectArgs ())
// ; sqlSpec = NotYetImplementedTODO
// ; previewable = Impure
// ; deprecated = NotDeprecated } ]
//
let fns : List<BuiltInFn> =
[ { name = fn "Twilio" "sendText" 0

parameters =
[ Param.make "accountSID" TStr ""
Param.make "authToken" TStr ""
Param.make "fromNumber" TStr ""
Param.make "toNumber" TStr ""
Param.make "body" TStr "" ]
returnType = TDict(varA)
description =
"Send text with `body` to phone number `toNumber` from number `fromNumber`, authenticated via `accountSID` and `authToken`"
fn =
(function
| (s,
[ DStr accountSID
DStr authToken
DStr fromNumber
DStr toNumber
DStr body ]) ->
let basicAuthString =
LibHttpClientAuth.encodeBasicAuthBroken accountSID authToken
let encoding = "application/x-www-form-urlencoded" in
let headers =
[ ("Authorization", DStr basicAuthString)
("Content-Type", DStr encoding) ]
|> Map
|> DObj
let hostUrl = Canvas.urlFor s.program.canvasName in
let body =
[ ("From", DStr fromNumber)
("To", DStr toNumber)
("Body", DStr body)
("ValidityPeriod", DStr "900")
("StatusCallback", DStr(hostUrl + "/twilioCallback")) ]
|> Map
|> DObj
let twilioUri =
$"https://api.twilio.com/2010-04-01/Accounts/{accountSID}/Messages.json"
LegacyHttpClient0.sendRequest
twilioUri
System.Net.Http.HttpMethod.Post
LibExecution.DvalReprExternal.toPrettyMachineJsonStringV1
(Some body)
(DObj Map.empty)
headers
| _ -> incorrectArgs ())
sqlSpec = NotYetImplementedTODO
previewable = Impure
// Deprecated due to using encodeBasicAuthBroken
deprecated = DeprecatedBecause "bad unicode support" }
{ name = fn "Twilio" "sendText" 1
parameters =
[ Param.make "accountSID" TStr ""
Param.make "authToken" TStr ""
Param.make "fromNumber" TStr ""
Param.make "toNumber" TStr ""
Param.make "body" TStr "" ]
returnType = varA
description =
"Send text with `body` to phone number `toNumber` from number `fromNumber`, authenticated via `accountSID` and `authToken`"
fn =
(function
| (s,
[ DStr accountSID
DStr authToken
DStr fromNumber
DStr toNumber
DStr body ]) ->
let basicAuthString =
LibHttpClientAuth.encodeBasicAuth accountSID authToken
let encoding = "application/x-www-form-urlencoded" in
let headers =
[ ("Authorization", DStr basicAuthString)
("Content-Type", DStr encoding) ]
|> Map
|> DObj
let hostUrl = Canvas.urlFor s.program.canvasName in
let body =
[ ("From", DStr fromNumber)
("To", DStr toNumber)
("Body", DStr body)
("ValidityPeriod", DStr "900")
("StatusCallback", DStr(hostUrl + "/twilioCallback")) ]
|> Map
|> DObj
let twilioUri =
$"https://api.twilio.com/2010-04-01/Accounts/{accountSID}/Messages.json"
LegacyHttpClient0.sendRequest
twilioUri
System.Net.Http.HttpMethod.Post
LibExecution.DvalReprExternal.toPrettyMachineJsonStringV1
(Some body)
(DObj Map.empty)
headers
| _ -> incorrectArgs ())
sqlSpec = NotYetImplementedTODO
previewable = Impure
deprecated = NotDeprecated } ]
5 changes: 2 additions & 3 deletions fsharp-backend/src/LibBackend/Canvas.fs
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,8 @@ let updateCorsSetting
|> Sql.executeStatementAsync


// let url_for (id : Uuidm.t) : string =
// let canvas_name = name_for_id id in
// "http://" ^ canvas_name ^ "." ^ Config.public_domain
let urlFor (canvasName : CanvasName.T) : string =
$"https://{canvasName}.{Config.publicDomain}"


// -------------------------
Expand Down

0 comments on commit 1ebad53

Please sign in to comment.