Skip to content

Commit e13e418

Browse files
committed
upgrade everything to decode API
1 parent a978804 commit e13e418

File tree

11 files changed

+212
-231
lines changed

11 files changed

+212
-231
lines changed

gleam.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "midas_sdk"
2-
version = "0.4.2"
2+
version = "0.5.0"
33
target = "javascript"
44

55
description = "A bag of SDK's that can be used with midas."
@@ -9,10 +9,9 @@ links = [{ title = "midas", href = "https://github.com/midas-framework/midas" }]
99

1010
[dependencies]
1111
gleam_stdlib = ">= 0.34.0 and < 2.0.0"
12-
gleam_http = ">= 3.7.0 and < 4.0.0"
12+
gleam_http = ">= 3.7.0 and < 5.0.0"
1313
midas = ">= 1.0.0 and < 2.0.0"
14-
gleam_json = ">= 1.0.0 and < 3.0.0"
15-
decode = ">= 0.5.0 and < 1.0.0"
14+
gleam_json = ">= 1.0.0 and < 4.0.0"
1615
snag = ">= 1.1.0 and < 2.0.0"
1716

1817
[dev-dependencies]

manifest.toml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@
22
# You typically do not need to edit this file
33

44
packages = [
5-
{ name = "decode", version = "0.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "decode", source = "hex", outer_checksum = "05E14DC95A550BA51B8774485B04894B87A898C588B9B1C920104B110AED218B" },
6-
{ name = "filepath", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "67A6D15FB39EEB69DD31F8C145BB5A421790581BD6AA14B33D64D5A55DBD6587" },
7-
{ name = "gleam_http", version = "3.7.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "8A70D2F70BB7CFEB5DF048A2183FFBA91AF6D4CF5798504841744A16999E33D2" },
8-
{ name = "gleam_json", version = "2.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C55C5C2B318533A8072D221C5E06E5A75711C129E420DD1CE463342106012E5D" },
9-
{ name = "gleam_stdlib", version = "0.59.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "F8FEE9B35797301994B81AF75508CF87C328FE1585558B0FFD188DC2B32EAA95" },
10-
{ name = "gleeunit", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "F7A7228925D3EE7D0813C922E062BFD6D7E9310F0BEE585D3A42F3307E3CFD13" },
11-
{ name = "midas", version = "1.4.1", build_tools = ["gleam"], requirements = ["filepath", "gleam_http", "gleam_json", "gleam_stdlib", "snag"], otp_app = "midas", source = "hex", outer_checksum = "5D0C9F924D527DCA35D818A2D87A1ED0923FA3CAF59976F1A2D1C970A30C9C07" },
5+
{ name = "filepath", version = "1.1.2", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "filepath", source = "hex", outer_checksum = "B06A9AF0BF10E51401D64B98E4B627F1D2E48C154967DA7AF4D0914780A6D40A" },
6+
{ name = "gleam_http", version = "4.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "0A62451FC85B98062E0907659D92E6A89F5F3C0FBE4AB8046C99936BF6F91DBC" },
7+
{ name = "gleam_json", version = "3.0.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "5BA154440B22D9800955B1AB854282FA37B97F30F409D76B0824D0A60C934188" },
8+
{ name = "gleam_stdlib", version = "0.60.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "621D600BB134BC239CB2537630899817B1A42E60A1D46C5E9F3FAE39F88C800B" },
9+
{ name = "gleeunit", version = "1.3.1", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "A7DD6C07B7DA49A6E28796058AA89E651D233B357D5607006D70619CD89DAAAB" },
10+
{ name = "midas", version = "1.4.2", build_tools = ["gleam"], requirements = ["filepath", "gleam_http", "gleam_json", "gleam_stdlib", "snag"], otp_app = "midas", source = "hex", outer_checksum = "64EC3840E278DA0C3A3552D93A5E7B6932C94540FEEF96F578E27ED53E1D737E" },
1211
{ name = "snag", version = "1.1.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "snag", source = "hex", outer_checksum = "7E9F06390040EB5FAB392CE642771484136F2EC103A92AE11BA898C8167E6E17" },
1312
]
1413

1514
[requirements]
16-
decode = { version = ">= 0.5.0 and < 1.0.0" }
17-
gleam_http = { version = ">= 3.7.0 and < 4.0.0" }
18-
gleam_json = { version = ">= 1.0.0 and < 3.0.0" }
15+
gleam_http = { version = ">= 3.7.0 and < 5.0.0" }
16+
gleam_json = { version = ">= 1.0.0 and < 4.0.0" }
1917
gleam_stdlib = { version = ">= 0.34.0 and < 2.0.0" }
2018
gleeunit = { version = ">= 1.0.0 and < 2.0.0" }
2119
midas = { version = ">= 1.0.0 and < 2.0.0" }

src/midas/sdk/accu_weather/forecast.gleam

Lines changed: 64 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import gleam/bit_array
2-
import gleam/dynamic
1+
import gleam/dynamic/decode
32
import gleam/http/request
43
import gleam/http/response
54
import gleam/json
@@ -31,14 +30,15 @@ pub fn five_day_forecast_request(api_key, location) {
3130
}
3231

3332
pub fn five_day_forecast_response(response: response.Response(BitArray)) {
34-
use json <- try(
35-
bit_array.to_string(response.body)
36-
|> result.replace_error(snag.new("not utf8 encoded")),
37-
)
38-
let decoder =
39-
dynamic.field("DailyForecasts", dynamic.list(daily_forcast_decoder))
33+
let decoder = {
34+
use daily_forecasts <- decode.field(
35+
"DailyForecasts",
36+
decode.list(daily_forcast_decoder()),
37+
)
38+
decode.success(daily_forecasts)
39+
}
4040
use message <- try(
41-
json.decode_bits(response.body, decoder)
41+
json.parse_bits(response.body, decoder)
4242
|> result.map_error(fn(reason) {
4343
snag.new(string.inspect(reason))
4444
|> snag.layer("failed to decode message")
@@ -59,23 +59,41 @@ pub type DailyForecast {
5959
)
6060
}
6161

62-
pub fn daily_forcast_decoder(raw) {
63-
dynamic.decode7(
64-
DailyForecast,
65-
dynamic.field("Date", dynamic.string),
66-
dynamic.field("Sun", dynamic.field("Rise", dynamic.string)),
67-
dynamic.field("Sun", dynamic.field("Set", dynamic.string)),
68-
dynamic.field(
69-
"Temperature",
70-
dynamic.field("Minimum", dynamic.field("Value", dynamic.float)),
71-
),
72-
dynamic.field(
73-
"Temperature",
74-
dynamic.field("Maximum", dynamic.field("Value", dynamic.float)),
75-
),
76-
dynamic.field("Day", detail_decoder),
77-
dynamic.field("Night", detail_decoder),
78-
)(raw)
62+
pub fn daily_forcast_decoder() {
63+
use date <- decode.field("Date", decode.string)
64+
use sunrise <- decode.field("Sun", {
65+
use sunrise <- decode.field("Rise", decode.string)
66+
decode.success(sunrise)
67+
})
68+
use sunset <- decode.field("Sun", {
69+
use sunrise <- decode.field("Set", decode.string)
70+
decode.success(sunrise)
71+
})
72+
use minimum_temperature <- decode.field("Temperature", {
73+
use minimum_temperature <- decode.field("Minimum", {
74+
use minimum_temperature <- decode.field("Value", decode.float)
75+
decode.success(minimum_temperature)
76+
})
77+
decode.success(minimum_temperature)
78+
})
79+
use maximum_temperature <- decode.field("Temperature", {
80+
use minimum_temperature <- decode.field("Maximum", {
81+
use minimum_temperature <- decode.field("Value", decode.float)
82+
decode.success(minimum_temperature)
83+
})
84+
decode.success(minimum_temperature)
85+
})
86+
use day <- decode.field("Day", detail_decoder())
87+
use night <- decode.field("Night", detail_decoder())
88+
decode.success(DailyForecast(
89+
date,
90+
sunrise,
91+
sunset,
92+
minimum_temperature,
93+
maximum_temperature,
94+
day,
95+
night,
96+
))
7997
}
8098

8199
pub type Detail {
@@ -86,17 +104,24 @@ pub type Detail {
86104
)
87105
}
88106

89-
fn detail_decoder(raw) {
90-
dynamic.decode3(
91-
Detail,
92-
dynamic.field("PrecipitationProbability", dynamic.int),
93-
dynamic.field(
94-
"Wind",
95-
dynamic.field("Speed", dynamic.field("Value", dynamic.float)),
96-
),
97-
dynamic.field(
98-
"Wind",
99-
dynamic.field("Direction", dynamic.field("English", dynamic.string)),
100-
),
101-
)(raw)
107+
fn detail_decoder() {
108+
use precipitation_probability <- decode.field(
109+
"PrecipitationProbability",
110+
decode.int,
111+
)
112+
use wind_speed <- decode.field("Wind", {
113+
use wind_speed <- decode.field("Speed", {
114+
use wind_speed <- decode.field("Value", decode.float)
115+
decode.success(wind_speed)
116+
})
117+
decode.success(wind_speed)
118+
})
119+
use wind_direction <- decode.field("Wind", {
120+
use wind_direction <- decode.field("Direction", {
121+
use wind_direction <- decode.field("English", decode.string)
122+
decode.success(wind_direction)
123+
})
124+
decode.success(wind_direction)
125+
})
126+
decode.success(Detail(precipitation_probability, wind_speed, wind_direction))
102127
}

src/midas/sdk/bluesky.gleam

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import gleam/dynamic
1+
import gleam/dynamic/decode
22
import gleam/http
33
import gleam/http/request
44
import gleam/json
@@ -22,11 +22,14 @@ pub fn create_session(handle, password) {
2222
|> request.set_body(<<json.to_string(body):utf8>>)
2323
|> request.prepend_header("content-type", "application/json")
2424
use response <- t.do(t.fetch(request))
25-
let decoder = dynamic.field("accessJwt", dynamic.string)
25+
let decoder = {
26+
use jwt <- decode.field("accessJwt", decode.string)
27+
decode.success(jwt)
28+
}
2629

2730
use token <- t.try(case response.status {
2831
200 ->
29-
case json.decode_bits(response.body, decoder) {
32+
case json.parse_bits(response.body, decoder) {
3033
Ok(token) -> Ok(token)
3134
Error(reason) ->
3235
Error(snag.new("failed to decode response " <> string.inspect(reason)))
@@ -58,10 +61,13 @@ pub fn create_post(access_token, handle, text, at) {
5861
|> request.prepend_header("content-type", "application/json")
5962
|> request.prepend_header("authorization", "Bearer " <> access_token)
6063
use response <- t.do(t.fetch(request))
61-
let decoder = dynamic.field("uri", dynamic.string)
64+
let decoder = {
65+
use uri <- decode.field("uri", decode.string)
66+
decode.success(uri)
67+
}
6268
use uri <- t.try(case response.status {
6369
200 ->
64-
case json.decode_bits(response.body, decoder) {
70+
case json.parse_bits(response.body, decoder) {
6571
Ok(uri) -> Ok(uri)
6672
Error(reason) ->
6773
Error(snag.new("failed to decode response " <> string.inspect(reason)))

src/midas/sdk/google.gleam

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import gleam/bit_array
2-
import gleam/dynamic
1+
import gleam/dynamic/decode
32
import gleam/http/request
43
import gleam/http/response
54
import gleam/int
@@ -94,17 +93,14 @@ pub fn userinfo_request(token) {
9493
|> request.set_body(<<>>)
9594
}
9695

97-
pub fn userinfo_response(response: response.Response(BitArray)) {
98-
use json <- try(
99-
bit_array.to_string(response.body)
100-
|> result.replace_error(snag.new("not utf8 encoded")),
101-
)
102-
use message <- try(
103-
json.decode_bits(response.body, dynamic.field("email", dynamic.string))
104-
|> result.map_error(fn(reason) {
105-
snag.new(string.inspect(reason))
106-
|> snag.layer("failed to decode message")
107-
}),
108-
)
109-
Ok(message)
96+
pub fn userinfo_response(response) {
97+
let response.Response(body:, ..) = response
98+
json.parse_bits(body, {
99+
use email <- decode.field("email", decode.string)
100+
decode.success(email)
101+
})
102+
|> result.map_error(fn(reason) {
103+
snag.new(string.inspect(reason))
104+
|> snag.layer("failed to decode message")
105+
})
110106
}

src/midas/sdk/google/calendar.gleam

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import gleam/bit_array
2-
import gleam/dynamic
3-
import gleam/http
1+
import gleam/dynamic/decode
42
import gleam/http/request
53
import gleam/http/response
64
import gleam/json
7-
import gleam/option.{type Option}
5+
import gleam/option.{type Option, None, Some}
86
import gleam/result.{try}
97
import gleam/string
108
import midas/task as t
@@ -24,13 +22,13 @@ fn get(token, path) {
2422
|> request.set_body(<<>>)
2523
}
2624

27-
fn post(token, path, mime, content) {
28-
base_request(token)
29-
|> request.set_method(http.Post)
30-
|> request.set_path(path)
31-
|> request.prepend_header("content-type", mime)
32-
|> request.set_body(content)
33-
}
25+
// fn post(token, path, mime, content) {
26+
// base_request(token)
27+
// |> request.set_method(http.Post)
28+
// |> request.set_path(path)
29+
// |> request.prepend_header("content-type", mime)
30+
// |> request.set_body(content)
31+
// }
3432

3533
// https://developers.google.com/calendar/api/v3/reference/events/list
3634
pub fn list_events(token, calendar_id, time_min) {
@@ -51,13 +49,12 @@ pub fn list_events_request(token, calendar_id, time_min) {
5149
}
5250

5351
pub fn list_events_response(response: response.Response(BitArray)) {
54-
use json <- try(
55-
bit_array.to_string(response.body)
56-
|> result.replace_error(snag.new("not utf8 encoded")),
57-
)
58-
let decoder = dynamic.field("items", dynamic.list(event_decoder))
52+
let decoder = {
53+
use items <- decode.field("items", decode.list(event_decoder()))
54+
decode.success(items)
55+
}
5956
use message <- try(
60-
json.decode_bits(response.body, decoder)
57+
json.parse_bits(response.body, decoder)
6158
|> result.map_error(fn(reason) {
6259
snag.new(string.inspect(reason))
6360
|> snag.layer("failed to decode message")
@@ -75,24 +72,34 @@ pub type Event {
7572
)
7673
}
7774

78-
pub fn event_decoder(raw) {
79-
dynamic.decode4(
80-
Event,
81-
dynamic.field("summary", dynamic.string),
82-
dynamic.optional_field("location", dynamic.string),
83-
dynamic.field("start", date_or_datetime_decoder),
84-
dynamic.field("end", date_or_datetime_decoder),
85-
)(raw)
75+
pub fn event_decoder() {
76+
use summary <- decode.field("summary", decode.string)
77+
use location <- decode.optional_field(
78+
"location",
79+
None,
80+
decode.string |> decode.map(Some),
81+
)
82+
use start <- decode.field("start", date_or_datetime_decoder())
83+
use end <- decode.field("end", date_or_datetime_decoder())
84+
decode.success(Event(summary, location, start, end))
8685
}
8786

8887
pub type DateOrDatetime {
8988
Date(String)
9089
Datetime(String)
9190
}
9291

93-
pub fn date_or_datetime_decoder(raw) {
94-
dynamic.any([
95-
dynamic.decode1(Date, dynamic.field("date", dynamic.string)),
96-
dynamic.decode1(Datetime, dynamic.field("dateTime", dynamic.string)),
97-
])(raw)
92+
pub fn date_or_datetime_decoder() {
93+
decode.one_of(
94+
{
95+
use date <- decode.field("date", decode.string)
96+
decode.success(Date(date))
97+
},
98+
[
99+
{
100+
use datetime <- decode.field("dateTime", decode.string)
101+
decode.success(Datetime(datetime))
102+
},
103+
],
104+
)
98105
}

0 commit comments

Comments
 (0)