ctucx.git: nimhafas

[nimlang] hafas-client library

commit da5f5f437b6f08b995c9aa080e4d11acb62a7478
parent e815bbfd2ef4f49c4e3a757739a60d802bcc53f5
Author: Leah (ctucx) <leah@ctu.cx>
Date: Sat, 13 Mar 2021 15:10:24 +0100

make hafas.nim usable as external dependency via nimble
9 files changed, 209 insertions(+), 12 deletions(-)
M
hafas.nim
|
3
+++
M
hafas.nimble
|
3
++-
M
hafas/api/suggestions.nim
|
6
+++---
M
hafas/errors.nim
|
2
+-
M
hafas/parse/journeys_response.nim
|
6
+++---
M
hafas/parse/operator.nim
|
2
+-
M
hafas/parse/products.nim
|
2
+-
M
hafas/parse/remark.nim
|
2
+-
M
hafas/types.nim
|
195
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
diff --git a/hafas.nim b/hafas.nim
@@ -1,7 +1,9 @@
 import hafas/api/journeys
 import hafas/api/suggestions
 import hafas/api/refresh_journey
+import hafas/types
 
 export journeys
 export suggestions
 export refresh_journey
+export types+
\ No newline at end of file
diff --git a/hafas.nimble b/hafas.nimble
@@ -5,7 +5,8 @@ author        = "Milan P\xC3\xA4ssler (petabyteboy)"
 description   = "a hafas-client library written in nim"
 license       = "GPL-3.0"
 srcDir        = "./"
-
+installDirs   = @["hafas"]
+installFiles  = @["hafas.nim"]
 
 # Dependencies
 
diff --git a/hafas/api/suggestions.nim b/hafas/api/suggestions.nim
@@ -1,10 +1,10 @@
-import ../../../types
-import ../parse/point
-import ../util
 import json
 import asyncdispatch
 import sequtils
 import options
+import ../types
+import ../parse/point
+import ../util
 
 proc suggestions*(params: SuggestionsParams): Future[seq[Point]] {.async.} =
   let req = %* {
diff --git a/hafas/errors.nim b/hafas/errors.nim
@@ -1,5 +1,5 @@
 import asynchttpserver
-import ../../types
+import types
 
 proc parseError*(errstr: string): hafasException =
   case errstr:
diff --git a/hafas/parse/journeys_response.nim b/hafas/parse/journeys_response.nim
@@ -1,3 +1,6 @@
+import json
+import sequtils
+import strutils
 import ../types
 import ./remark
 import ./point

@@ -5,9 +8,6 @@ import ./operator
 import ./journey
 import ./line
 import ./polyline
-import json
-import sequtils
-import strutils
 
 proc parseJourneysResponse*(data: JsonNode, isRefresh: bool = false): JourneysResponse =
   let points = map(data["res"]["common"]["locL"].getElems(), parsePoint)
diff --git a/hafas/parse/operator.nim b/hafas/parse/operator.nim
@@ -1,4 +1,4 @@
-import ../../../types
+import ../types
 import ../util
 import json
 
diff --git a/hafas/parse/products.nim b/hafas/parse/products.nim
@@ -1,4 +1,4 @@
-import ../../../types
+import ../types
 import bitops
 
 proc parseProduct*(cls: int): Product =
diff --git a/hafas/parse/remark.nim b/hafas/parse/remark.nim
@@ -1,4 +1,4 @@
-import ../../../types
+import ../types
 import json
 
 proc parseRemark*(rem: JsonNode): Remark =
diff --git a/hafas/types.nim b/hafas/types.nim
@@ -1,7 +1,7 @@
-import ../../types
 import options
 export types
 import json
+import asynchttpserver
 
 type
   CommonData* = object

@@ -51,3 +51,196 @@ type
   HafasPolyline* = object
     crdEncYX*: string
     ppLocRefL*: Option[seq[HafasLocRef]]
+
+  Stop* = object
+    id*:       string
+    name*:     string
+    location*: Location
+    products*: Products
+    station*:  Option[Station]
+
+  Station* = object
+    id*:       int
+    name*:     string
+    location*: Location
+    products*: Products
+
+  Location* = object
+    latitude*:  float
+    longitude*: float
+    name*:      Option[string]
+    address*:   Option[string]
+    id*:        Option[string]
+
+  Product* = enum
+    nationalExp, national, regionalExp, regional, suburban, bus, ferry, subway, tram, taxi
+
+  Mode* = enum
+    Train, Bus, Watercraft, Taxi, Walking
+
+  Products* = object
+    nationalExp*:     bool
+    national*:        bool
+    regionalExp*:     bool
+    regional*:        bool
+    suburban*:        bool
+    bus*:             bool
+    ferry*:           bool
+    subway*:          bool
+    tram*:            bool
+    taxi*:            bool
+
+  Point* = object
+    stop*:     Option[Stop]
+    location*: Option[Location]
+
+  Journey* = object
+    refreshToken*: string
+    lastUpdated*:  int64
+    legs*:         seq[Leg]
+    cycle*:        Option[Cycle]
+    price*:        Option[Price]
+
+  LegPart* = object
+    point*:                Point
+    plannedTime*:          int64
+    prognosedTime*:        Option[int64]
+    plannedPlatform*:      Option[string]
+    prognosedPlatform*:    Option[string]
+
+  Leg* = object
+    isWalking*:            bool
+    isTransfer*:           bool
+    cancelled*:            bool
+    departure*:            LegPart
+    arrival*:              LegPart
+    polyline*:             Option[Polyline]
+    distance*:             Option[int]              # required for isWalking or isTransfer
+    tripId*:               Option[string]           # required for not isWalking and not isTranfer
+    line*:                 Option[Line]             # required for not isWalking and not isTranfer
+    direction*:            Option[string]           # required for not isWalking and not isTranfer
+    cycle*:                Option[Cycle]
+    stopovers*:            Option[seq[Stopover]]
+    remarks*:              Option[seq[Remark]]
+
+  Line* = object
+    id*:                string
+    name*:              string
+    mode*:              Mode
+    product*:           Product
+    tripNum*:           string
+    productName*:       string
+    fullProductName*:   string
+    operator*:          Option[Operator]
+    additionalName*:    Option[string]
+    trainType*:         Option[string]
+    trainTypeShort*:    Option[string]
+
+  Operator* = object
+    id*:   string
+    name*: string
+
+  Cycle* = object
+    min*: Option[int]
+    max*: Option[int]
+    nr*:  Option[int]
+
+  # TODO: Make this unnecessary :3
+  Price* = object
+    amount*:   float
+    currency*: Option[string]
+
+  StopoverPart* = object
+    plannedTime*:          Option[int64]
+    prognosedTime*:        Option[int64]
+    plannedPlatform*:      Option[string]
+    prognosedPlatform*:    Option[string]
+
+  Stopover* = object
+    stop*:                           Stop
+    cancelled*:                      bool
+    departure*:                      StopoverPart 
+    arrival*:                        StopoverPart
+
+  Remark* = object
+    `type`*:  Option[string]
+    code*:    Option[string]
+    summary*: Option[string]
+    text*:    Option[string]
+    tripId*:  Option[string]
+
+  JourneysResponse* = object
+    earlierRef*:   string
+    laterRef*:     string
+    journeys*:     seq[Journey]
+
+  Accessibility* = enum
+    none, partial, complete
+
+  SuggestionsParams* = object
+    query*:      string
+    results*:    Option[int]         #default value: 10
+    stations*:   Option[bool]        #default value: true
+    poi*:        Option[bool]        #default value: true
+    addresses*:  Option[bool]        #default value: true
+
+  JourneysParams* = object
+    fromPoint*:          Point
+    toPoint*:            Point
+    viaPoint*:           Option[Point]              #default value: null      support in hafas backend: ✅
+    earlierRef*:         Option[string]             #default value: null      support in hafas backend: ✅
+    laterRef*:           Option[string]             #default value: null      support in hafas backend: ✅
+    results*:            Option[int]                #default value: 5         support in hafas backend: ✅
+    stopovers*:          Option[bool]               #default value: false     support in hafas backend: ✅
+    polylines*:          Option[bool]               #default value: false     support in hafas backend: ✅
+    remarks*:            Option[bool]               #default value: true      support in hafas backend:
+    bikeFriendly*:       Option[bool]               #default value: false     support in hafas backend:
+    tickets*:            Option[bool]               #default value: false     support in hafas backend: ✅
+    startWithWalking*:   Option[bool]               #default value: true      support in hafas backend: ✅
+    scheduledDays*:      Option[bool]
+    accessibility*:      Option[Accessibility]      #default value: none      support in hafas backend: ✅
+    transfers*:          Option[int]                #default value: -1        support in hafas backend: ✅
+    transferTime*:       Option[int]                #default value: 00        support in hafas backend: ✅
+    arrival*:            Option[int64]              #default value: null      support in hafas backend: ✅
+    departure*:          Option[int64]              #default value: null      support in hafas backend: ✅
+    products*:           Option[Products]           #default value: all true  support in hafas backend: ✅
+
+  RefreshJourneyParams* = object
+    refreshToken*:       string
+    stopovers*:          Option[bool]               #default value: false     support in hafas backend: ✅
+    polylines*:          Option[bool]               #default value: false     support in hafas backend: ✅
+    tickets*:            Option[bool]               #default value: false     support in hafas backend: ✅
+
+  Polyline* = object
+    `type`*:        string
+    features*:      seq[Feature]
+
+  Feature* = object
+    `type`*:        string
+    properties*:    Option[Stop]
+    geometry*:      FeatureGeometry
+
+  FeatureGeometry* = object
+    `type`*:       string
+    coordinates*:  seq[float]
+
+  hafasExceptionKind* = enum
+    SERVER_ERROR, ACCESS_DENIED, INVALID_REQUEST, NOT_FOUND
+
+  hafasException*    = ref object of ValueError
+    code*: hafasExceptionKind
+    message*: string
+    statusCode*: HttpCode
+
+const MODES* = [
+  Train,       # nationalExp
+  Train,       # national
+  Train,       # regionalExp
+  Train,       # regional
+  Train,       # suburban
+  Bus,         # bus
+  Watercraft,  # ferry
+  Train,       # subway
+  Train,       # tram
+  Taxi,        # taxi
+]