Skip to content

Commit

Permalink
add json instances and update swagger.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
paweljakubas committed Feb 27, 2020
1 parent 7c92d33 commit c337fdb
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 3 deletions.
35 changes: 32 additions & 3 deletions lib/core/src/Cardano/Wallet/Api/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,10 @@ import GHC.Generics
( Generic )
import GHC.TypeLits
( Nat, Symbol )
import Network.NTP.Packet
( Microsecond (..), NtpOffset (..) )
import Network.NTP.Query
( NtpStatus )
( NtpStatus (..) )
import Numeric.Natural
( Natural )
import Servant.API
Expand Down Expand Up @@ -988,9 +990,36 @@ instance ToJSON ApiNetworkInformation where
toJSON = genericToJSON defaultRecordTypeOptions

instance FromJSON (ApiT NtpStatus) where
parseJSON = fail "to be implemented later"
parseJSON obj = do
offset <-
(withObject "NtpStatus" $
\o -> o .:? "offset" :: Aeson.Parser (Maybe Integer)) obj
sync <-
(withObject "NtpStatus" $
\o -> o .:? "sync" :: Aeson.Parser (Maybe Text)) obj
let errMsg = "invalid NtpStatus value"
case (offset, sync) of
(Just num, Just txt) ->
if txt == "done" then
pure $ ApiT $ NtpDrift (NtpOffset (Microsecond num))
else
fail errMsg
(Nothing, Just txt) ->
if txt == "pending" then
pure $ ApiT NtpSyncPending
else if txt == "unavailable" then
pure $ ApiT NtpSyncUnavailable
else
fail errMsg
_ -> fail errMsg

instance ToJSON (ApiT NtpStatus) where
toJSON = fail "to be implemented later"
toJSON (ApiT s) = case s of
NtpSyncPending -> object [ "sync" .= ("pending" :: Text) ]
NtpSyncUnavailable -> object [ "sync" .= ("unavailable" :: Text) ]
NtpDrift (NtpOffset (Microsecond num)) ->
object [ "sync" .= ("done" :: Text)
, "offset" .= toJSON num ]

instance FromJSON ApiNetworkClock where
parseJSON = genericParseJSON defaultRecordTypeOptions
Expand Down
65 changes: 65 additions & 0 deletions specifications/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,21 @@ x-percentage: &percentage
enum:
- percent

x-microseconds: &microseconds
type: object
required:
- quantity
- unit
properties:
quantity:
type: number
minimum: 0
example: 42
unit:
type: string
enum:
- microsecond

x-syncProgress: &syncProgress
type: object
required:
Expand All @@ -150,6 +165,26 @@ x-syncProgress: &syncProgress
example:
status: ready

x-syncClockProgress: &syncClockProgress
type: object
required:
- status
properties:
status:
type: string
enum:
- unavailable
- pending
- done
progress:
<<: *microseconds
description: |
<span style="position: relative; left: 35px; top: -21px; vertical-align: middle; background-color: rgba(142, 142, 220, 0.05); color: rgba(50, 50, 159, 0.9); margin: 0 5px; padding: 0 5px; border: 1px solid rgba(50, 50, 159, 0.1); line-height: 20px; font-size: 13px; border-radius: 2px;">
<strong>if:</strong> status == done
</span><br/>
example:
status: pending

x-amount: &amount
description: Coins, in Lovelace
type: object
Expand Down Expand Up @@ -514,6 +549,11 @@ x-networkInformationSyncProgress: &networkInformationSyncProgress
Estimated synchronization progress of the node with the underlying network. Note that this may
change quite arbitrarily as the node may switch to shorter or longer chain forks.
x-networkClockSyncProgress: &networkClockSyncProgress
<<: *syncClockProgress
description: |
The status progress of syncing local ntp client to get ntp offset.
x-networkInformationNtpStatus: &networkInformationNtpStatus
type: object
description: |
Expand Down Expand Up @@ -621,6 +661,13 @@ components:
network_tip: *ApiNetworkTip
next_epoch: *ApiEpochInfo

ApiNetworkClock: &ApiNetworkClock
type: object
required:
- ntp_status
properties:
ntp_status: *networkClockSyncProgress

ApiNetworkParameters: &ApiNetworkParameters
type: object
required:
Expand Down Expand Up @@ -1431,6 +1478,15 @@ x-responsesGetNetworkInformation: &responsesGetNetworkInformation
application/json:
schema: *ApiNetworkInformation

x-responsesGetNetworkClock: &responsesGetNetworkClock
<<: *responsesErr405
<<: *responsesErr406
200:
description: Ok
content:
application/json:
schema: *ApiNetworkClock

x-responsesGetNetworkParameters: &responsesGetNetworkParameters
<<: *responsesErr400
<<: *responsesErr404
Expand Down Expand Up @@ -1935,6 +1991,15 @@ paths:
<p align="right">status: <strong>stable</strong></p>
responses: *responsesGetNetworkInformation

/network/clock:
get:
operationId: getNetworkClock
tags: ["Network"]
summary: Clock
description: |
<p align="right">status: <strong>under development</strong></p>
responses: *responsesGetNetworkClock

/network/parameters/{epochId}:
get:
operationId: getNetworkParameters
Expand Down

0 comments on commit c337fdb

Please sign in to comment.