Skip to content

Commit

Permalink
[SQSERVICES-1644] Servantify brig calling API (#2815)
Browse files Browse the repository at this point in the history
  • Loading branch information
battermann authored Nov 7, 2022
1 parent 8360726 commit 50a198d
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 199 deletions.
1 change: 1 addition & 0 deletions changelog.d/5-internal/pr-2815
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Brig calling API is now migrated to servant
45 changes: 27 additions & 18 deletions libs/types-common/src/Data/Misc.hs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import Data.ByteString.Lazy (toStrict)
import Data.IP (IP (IPv4, IPv6), toIPv4, toIPv6b)
import Data.Range
import Data.Schema
import Data.String.Conversions (cs)
import qualified Data.Swagger as S
import qualified Data.Swagger.Build.Api as Doc
import qualified Data.Text as Text
Expand All @@ -83,14 +84,15 @@ import Servant (FromHttpApiData (..))
import Test.QuickCheck (Arbitrary (arbitrary), chooseInteger)
import qualified Test.QuickCheck as QC
import Text.Read (Read (..))
import URI.ByteString hiding (Port)
import URI.ByteString hiding (Port, portNumber)
import qualified URI.ByteString.QQ as URI.QQ

--------------------------------------------------------------------------------
-- IpAddr / Port

newtype IpAddr = IpAddr {ipAddr :: IP}
deriving stock (Eq, Ord, Show, Generic)
deriving (A.ToJSON, A.FromJSON, S.ToSchema) via (Schema IpAddr)

instance S.ToParamSchema IpAddr where
toParamSchema _ = mempty & S.type_ ?~ S.SwaggerString
Expand Down Expand Up @@ -125,24 +127,22 @@ newtype Port = Port
{portNumber :: Word16}
deriving stock (Eq, Ord, Show, Generic)
deriving newtype (Real, Enum, Num, Integral, NFData, Arbitrary)
deriving (A.ToJSON, A.FromJSON, S.ToSchema) via (Schema Port)

instance Read Port where
readsPrec n = map (first Port) . readsPrec n

instance ToJSON IpAddr where
toJSON (IpAddr ip) = A.String (Text.pack $ show ip)

instance FromJSON IpAddr where
parseJSON = A.withText "IpAddr" $ \txt ->
case readMaybe (Text.unpack txt) of
Nothing -> fail "Failed parsing IP address."
Just ip -> pure (IpAddr ip)
instance ToSchema IpAddr where
schema = toText .= parsedText "IpAddr" fromText
where
toText :: IpAddr -> Text
toText = cs . toByteString

instance ToJSON Port where
toJSON (Port p) = toJSON p
fromText :: Text -> Either String IpAddr
fromText = maybe (Left "Failed parsing IP address.") Right . fromByteString . cs

instance FromJSON Port where
parseJSON = fmap Port . parseJSON
instance ToSchema Port where
schema = Port <$> portNumber .= schema

--------------------------------------------------------------------------------
-- Location
Expand All @@ -158,8 +158,10 @@ instance ToSchema Location where
schema =
object "Location" $
Location
<$> _latitude .= field "lat" genericToSchema
<*> _longitude .= field "lon" genericToSchema
<$> _latitude
.= field "lat" genericToSchema
<*> _longitude
.= field "lon" genericToSchema

instance Show Location where
show p =
Expand Down Expand Up @@ -273,7 +275,10 @@ instance ToSchema HttpsUrl where
schema =
(decodeUtf8 . toByteString')
.= parsedText "HttpsUrl" (runParser parser . encodeUtf8)
& doc' . S.schema . S.example ?~ toJSON ("https://example.com" :: Text)
& doc'
. S.schema
. S.example
?~ toJSON ("https://example.com" :: Text)

instance Cql HttpsUrl where
ctype = Tagged BlobColumn
Expand Down Expand Up @@ -319,7 +324,10 @@ instance ToSchema (Fingerprint Rsa) where
schema =
(decodeUtf8 . B64.encode . fingerprintBytes)
.= parsedText "Fingerprint" (runParser p . encodeUtf8)
& doc' . S.schema . S.example ?~ toJSON ("ioy3GeIjgQRsobf2EKGO3O8mq/FofFxHRqy0T4ERIZ8=" :: Text)
& doc'
. S.schema
. S.example
?~ toJSON ("ioy3GeIjgQRsobf2EKGO3O8mq/FofFxHRqy0T4ERIZ8=" :: Text)
where
p :: Chars.Parser (Fingerprint Rsa)
p = do
Expand Down Expand Up @@ -353,7 +361,8 @@ instance Show PlainTextPassword where
instance ToSchema PlainTextPassword where
schema =
PlainTextPassword
<$> fromPlainTextPassword .= untypedRangedSchema 6 1024 schema
<$> fromPlainTextPassword
.= untypedRangedSchema 6 1024 schema

instance Arbitrary PlainTextPassword where
-- TODO: why 6..1024? For tests we might want invalid passwords as well, e.g. 3 chars
Expand Down
Loading

0 comments on commit 50a198d

Please sign in to comment.