-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d1a41cb
commit 692ece0
Showing
5 changed files
with
43 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,37 @@ | ||
module Lib.Common.Types where | ||
|
||
import Data.Aeson | ||
import GHC.Generics (Generic) | ||
import Lib.OpenApi.Types | ||
import Lib.Path.Types | ||
import Data.Scientific | ||
import Data.Text qualified as T | ||
import Data.Text.Encoding qualified as T | ||
import Data.Text.Read (decimal) | ||
import Network.HTTP.Types.Method | ||
import Network.HTTP.Types.Status | ||
|
||
data ServerConfig = PathConf Paths | OpenApiConf OpenApi | ||
deriving (Generic, Show, Eq) | ||
newtype HTTPStatus = HTTPStatus {unStatus :: Status} deriving (Show, Eq, Ord) | ||
|
||
instance FromJSON ServerConfig where | ||
parseJSON = genericParseJSON defaultOptions{sumEncoding = UntaggedValue} | ||
instance FromJSON HTTPStatus where | ||
parseJSON = withScientific "Status" $ \s -> | ||
case toBoundedInteger s of | ||
Nothing -> fail "Bad status code" | ||
Just i -> return (HTTPStatus . toEnum $ i) | ||
|
||
instance FromJSONKey HTTPStatus where | ||
fromJSONKey = FromJSONKeyTextParser $ \s -> | ||
case decimal s of | ||
Left e -> fail e | ||
Right (i, _) -> return (HTTPStatus . toEnum $ i) | ||
|
||
newtype HTTPMethod = HTTPMethod {unMethod :: StdMethod} deriving (Show, Eq, Ord) | ||
|
||
instance FromJSON HTTPMethod where | ||
parseJSON = withText "Method" $ \s -> | ||
case (parseMethod . T.encodeUtf8 . T.toUpper) s of | ||
Left e -> fail $ (T.unpack . T.decodeUtf8) e | ||
Right m -> return $ HTTPMethod m | ||
|
||
instance FromJSONKey HTTPMethod where | ||
fromJSONKey = FromJSONKeyTextParser $ \s -> | ||
case (parseMethod . T.encodeUtf8 . T.toUpper) s of | ||
Left e -> fail $ (T.unpack . T.decodeUtf8) e | ||
Right m -> return $ HTTPMethod m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters