-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathAPI.fs
89 lines (66 loc) · 2.43 KB
/
API.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
namespace Plotly.NET.ChartStudio
open DynamicObj
open System
open System.Runtime.InteropServices
open System.Net.Http
open System.Web
type API() =
static let RESOURCE = "plots"
static member buildURL
(
resource,
[<Optional; DefaultParameterValue("")>] ?id: String,
[<Optional; DefaultParameterValue("")>] ?route: String
) =
let config = Config.getConfigFile ()
let baseURL =
match config.TryGetValue "plotly_api_domain" with
| Some apiDomain -> string apiDomain
| _ -> "https://api.plotly.com"
let urlWithID =
match id with
| Some x when (not (String.IsNullOrEmpty(x))) -> $"{baseURL}//v2//{resource}//{x}"
| _ -> $"{baseURL}//v2//{resource}"
let url =
match route with
| Some x when (not (String.IsNullOrEmpty(x))) -> $"{urlWithID}//{route}"
| _ -> $"{urlWithID}"
url
static member create body =
async {
use client = new HttpClient()
let url = API.buildURL RESOURCE
let content = new StringContent(body)
printf "%A" url
let! response = client.PostAsync(url, content) |> Async.AwaitTask
let! body =
response.Content.ReadAsStringAsync()
|> Async.AwaitTask
return
match response.IsSuccessStatusCode with
| true -> Ok body
| false -> Error body
}
|> Async.RunSynchronously
static member retrieve(fid, [<Optional; DefaultParameterValue("")>] ?share_key: String) =
async {
use client = new HttpClient()
let url =
API.buildURL (resource = RESOURCE, id = fid)
let url =
match share_key with
| Some key ->
let query =
HttpUtility.ParseQueryString(String.Empty)
query.Add("share_key", key)
let builder =
new UriBuilder(url, Query = query.ToString())
builder.Uri.ToString()
| _ -> url
let! response = client.GetAsync(url) |> Async.AwaitTask
let! content =
response.Content.ReadAsStringAsync()
|> Async.AwaitTask
return content
}
|> Async.RunSynchronously