Created
April 15, 2017 13:25
-
-
Save VelizarHristov/4084e3041530edc41ba03e1e6b6c5a04 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace CheapIroning | |
open System | |
open System.Net | |
open FSharp.Data | |
open Newtonsoft.Json | |
open Newtonsoft.Json.Converters | |
module Async = | |
let map f workflow = async { | |
let! res = workflow | |
return f res | |
} | |
/// Wraps around each of the URLs to the server | |
module ServerWrapper = | |
// TODO: make it a compile-time argument, rather than a hardcoded constant | |
let URL = "http://192.168.0.21:3000/" | |
let request<'T> path paramList httpMethod = | |
Http.AsyncRequestString( | |
sprintf "%s%s%s" URL "customer_app/" path, | |
query=paramList, | |
httpMethod=httpMethod | |
) |> Async.map (fun raw -> JsonConvert.DeserializeObject<'T> raw) | |
type LoginResult = | |
| Success | |
| IncorrectPassword | |
| EmailNotFound | |
| NotActivated | |
let login email password = | |
request<LoginResult> | |
"check_login" | |
["email", email; "password", password] | |
"POST" | |
type RequestCodeResult = | |
| Success | |
| EmailNotFound | |
| AlreadyActivated | |
let requestCode email = | |
request<RequestCodeResult> | |
"request_code" | |
["email", email] | |
"POST" | |
type CheckCodeResult = | |
| Success of string option // email | |
| AlreadyActivated | |
| Invalid | |
let checkCode code = | |
request<CheckCodeResult> | |
"check_code" | |
["code", code] | |
"POST" | |
type CompleteAccountResult = | |
| Success | |
| InvalidCode | |
let completeAccount codeToken (emailOpt : string option) password = | |
let emailPairOpt = Option.map (fun email -> ["email", email]) emailOpt | |
request<CompleteAccountResult> | |
"complete_account" | |
(["code_token", codeToken; "password", password] @ (defaultArg emailPairOpt [])) | |
"POST" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment