Skip to content

Commit

Permalink
WIP: Move low-level codecs to dedicated module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jskripsky committed Nov 23, 2023
1 parent 6adef5b commit 8b0059c
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 66 deletions.
1 change: 1 addition & 0 deletions AutoJobs.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<ItemGroup>
<Compile Include="src/Utils.fs" />
<Compile Include="src/Types.fs" />
<Compile Include="src/Codec.fs" />
<Compile Include="src/MeteoSwiss.fs" />
<Compile Include="src/GotCourts.fs" />
<Compile Include="src/Logger.fs" />
Expand Down
83 changes: 83 additions & 0 deletions src/Codec.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
namespace TcFairplay

open System
open System.Text.Json

[<AutoOpen>]
module Codec =
module private Api =
let private dateFormat = "yyyy-MM-dd"

let formatDate (date: DateOnly): string =
date.ToString(dateFormat)

let parseDate (s: string): DateOnly =
DateOnly.ParseExact(s, dateFormat)

let formatTime (time: TimeOnly): string =
(time.Hour * 60 + time.Minute) * 60
|> string

let calcTime (secondsSinceMidnight: int): TimeOnly =
TimeOnly(int64 secondsSinceMidnight * 10_000_000L)

let courtToId = function
| Court1 -> 8153
| Court2 -> 8154
| Court3 -> 8155

let idToCourt = function
| 8153 -> Court1
| 8154 -> Court2
| 8155 -> Court3
| id -> failwithf "Unknown court id '%d." id

module Blocking =
// FIXME!
let date = DateOnly(2023, 1, 1)

let parse (el: JsonElement): (Guid * Blocking) =
let get (name: string) = el.GetProperty name
let getString name = (get name).GetString()
let getInt name = (get name).GetInt32()
let getTime = getInt >> Api.calcTime

let guid = Guid.Parse (getString "id")
let blocking = {
Description = (getString "shortDesc")
Courts = [getInt "courtId" |> Api.idToCourt]
Date = date // FIXME!
StartEnd = Some (getTime "startTime", getTime "endTime")
Note = getString "note"
}
(guid, blocking)

let toKeyValueMap (b: Blocking): (string * string) list =
let toCourtPair no = ("courts[]", no |> Api.courtToId |> string)
let dateTimePairs =
let timePairs =
match b.StartEnd with
| Some (s, e) -> [
"time[start]", Api.formatTime s
"time[end]", Api.formatTime e
]
| None -> []

let date = Api.formatDate b.Date
[
"date", date
"dateTo", date
"allDay[disabled]", (b.StartEnd |> Option.isSome |> string |> fun s -> s.ToLower())
] @ timePairs

dateTimePairs @
(b.Courts |> List.map toCourtPair) @
[
"autoremove", "true"
"type", "other"
"description", b.Description
"note", b.Note
]

//module Reservation =
//module Player =
68 changes: 2 additions & 66 deletions src/GotCourts.fs
Original file line number Diff line number Diff line change
Expand Up @@ -38,58 +38,10 @@ module GotCourts =
let formatDate (date: DateOnly): string =
date.ToString(dateFormat)

let parseDate (s: string): DateOnly =
DateOnly.ParseExact(s, dateFormat)

let formatTime (time: TimeOnly): string =
(time.Hour * 60 + time.Minute) * 60
|> string

let calcTime (secondsSinceMidnight: int): TimeOnly =
TimeOnly(int64 secondsSinceMidnight * 10_000_000L)

let private toKeyValuePair (x, y) = KeyValuePair(x, y)

let private clubId = 53223

let private courtToId = function
| Court1 -> 8153
| Court2 -> 8154
| Court3 -> 8155

let private idToCourt = function
| 8153 -> Court1
| 8154 -> Court2
| 8155 -> Court3
| id -> failwithf "Unknown court id '%d." id

let private buildBlockingPairs (blocking: Blocking): (string * string) list =
let toCourtPair no = ("courts[]", no |> courtToId |> string)
let dateTimePairs =
let timePairs =
match blocking.StartEnd with
| Some (s, e) -> [
"time[start]", Api.formatTime s
"time[end]", Api.formatTime e
]
| None -> []

let date = Api.formatDate blocking.Date
[
"date", date
"dateTo", date
"allDay[disabled]", (blocking.StartEnd |> Option.isSome |> string |> fun s -> s.ToLower())
] @ timePairs

dateTimePairs @
(blocking.Courts |> List.map toCourtPair) @
[
"autoremove", "true"
"type", "other"
"description", blocking.Description
"note", blocking.Note
]

let processResponse (rawJson: string): Result<JsonElement, GotCourtsError> =
let doc = JsonDocument.Parse rawJson
let success = (doc.RootElement.GetProperty "status").GetBoolean()
Expand All @@ -102,7 +54,7 @@ module GotCourts =
Error errorText

let createBlocking (client: HttpClient) (blocking: Blocking): Result<Guid list, GotCourtsError> =
let pairs = buildBlockingPairs blocking
let pairs = Blocking.toKeyValueMap blocking
use content = new FormUrlEncodedContent(pairs |> List.map toKeyValuePair)

let respMsg = client.PostAsync (blockingUrl, content) |> await
Expand Down Expand Up @@ -134,24 +86,8 @@ module GotCourts =
let getBlockings (resp: JsonElement) =
let blockings = resp.GetProperty "blockings"

let parseBlocking (el: JsonElement): (Guid * Blocking) =
let get (name: string) = el.GetProperty name
let getString name = (get name).GetString()
let getInt name = (get name).GetInt32()
let getTime = getInt >> Api.calcTime

let guid = Guid.Parse (getString "id")
let blocking = {
Description = (getString "shortDesc")
Courts = [getInt "courtId" |> idToCourt]
Date = date
StartEnd = Some (getTime "startTime", getTime "endTime")
Note = getString "note"
}
(guid, blocking)

blockings.EnumerateArray ()
|> Seq.map parseBlocking
|> Seq.map Blocking.parse
|> Seq.toList

processResponse rawJson
Expand Down

0 comments on commit 8b0059c

Please sign in to comment.