Add a status endpoint exposing the API version

This commit is contained in:
Correl Roush 2022-03-24 22:54:08 -04:00
parent 47c85f026c
commit 3aabb35da5

View file

@ -15,13 +15,15 @@ import Data.Aeson
import Data.Aeson.TH
import Data.Maybe
import Data.OpenApi hiding (Server, name, server)
import Data.Text
import qualified Data.Text as T
import Data.Typeable (Typeable)
import Data.UUID
import Data.Version (showVersion)
import GHC.Generics
import Network.Wai
import Network.Wai.Handler.Warp
import Network.Wai.Logger
import qualified Paths_tutor as PT
import Servant
( Description,
Get,
@ -39,12 +41,22 @@ import Servant
)
import Servant.OpenApi (HasOpenApi (toOpenApi))
data Status = Status
{apiVersion :: T.Text}
deriving (Generic, Typeable)
instance FromJSON Status
instance ToJSON Status
instance ToSchema Status
data Prices = Prices
{ usd :: Maybe Text,
usd_foil :: Maybe Text,
eur :: Maybe Text,
eur_foil :: Maybe Text,
tix :: Maybe Text
{ usd :: Maybe T.Text,
usd_foil :: Maybe T.Text,
eur :: Maybe T.Text,
eur_foil :: Maybe T.Text,
tix :: Maybe T.Text
}
deriving (Eq, Show, Generic, Typeable)
@ -56,12 +68,12 @@ instance ToSchema Prices
data Card = Card
{ scryfall_id :: UUID,
name :: Text,
set_code :: Text,
collector_number :: Text,
rarity :: Text,
color_identity :: Text,
oracle_text :: Maybe Text,
name :: T.Text,
set_code :: T.Text,
collector_number :: T.Text,
rarity :: T.Text,
color_identity :: T.Text,
oracle_text :: Maybe T.Text,
prices :: Prices
}
deriving (Eq, Show, Generic, Typeable)
@ -72,8 +84,14 @@ instance ToJSON Card
instance ToSchema Card
type GetStatus =
Summary "Get API Status"
:> Description "Get the current running API version."
:> "api" :> "status"
:> Get '[JSON] Status
type SearchCards =
Summary "Search for cards in a collection."
Summary "Search for cards in a collection"
:> Description
"Text in the query string will be used to filter cards having that text in their \n\
\name. Additionally, the keyword expressions below can be used to search for \n\
@ -177,11 +195,12 @@ type SearchCards =
\Operators \n\
\: `:` (matches)"
:> "search"
:> QueryParam' '[Description "Query string"] "q" Text
:> QueryParam' '[Description "Query string"] "q" T.Text
:> Get '[JSON] [Card]
type TutorAPI =
SearchCards
GetStatus
:<|> SearchCards
type DocsAPI =
"api" :> "openapi.json" :> Get '[JSON] OpenApi
@ -207,12 +226,12 @@ openapi :: OpenApi
openapi =
toOpenApi tutorAPI
& info . title .~ "Tutor API"
& info . version .~ "1.0"
& info . version .~ packageVersion
& info . description ?~ "An API for searching a Magic: The Gathering card collection."
& info . license ?~ ("MIT" & url ?~ URL "http://mit.com")
tutorServer :: Server TutorAPI
tutorServer = cards
tutorServer = status :<|> cards
docsServer :: Server DocsAPI
docsServer = return openapi
@ -223,57 +242,68 @@ elmServer = serveDirectoryFileServer "www/public"
server :: Server API
server = tutorServer :<|> docsServer :<|> elmServer
cards :: Maybe Text -> Handler [Card]
cards q = return
[ Card
{ scryfall_id = fromJust $ fromText "f6cd7465-9dd0-473c-ac5e-dd9e2f22f5f6",
name = "Esika, God of the Tree // The Prismatic Bridge",
set_code = "KHM",
collector_number = "168",
rarity = "mythic",
color_identity = "WUBGR",
oracle_text = Nothing,
prices =
Prices
{ usd = Just "9.07",
usd_foil = Just "10.77",
eur = Just "7.79",
eur_foil = Just "12.27",
tix = Just "1.08"
}
},
Card
{ scryfall_id = fromJust $ fromText "d761ff73-0717-4ee4-996b-f5547bcf9b2f",
name = "Go-Shintai of Life's Origin",
set_code = "NEC",
collector_number = "66",
rarity = "mythic",
color_identity = "WUBGR",
oracle_text = Just "{W}{U}{B}{R}{G}, {T}: Return target enchantment card from your graveyard to the battlefield.\nWhenever Go-Shintai of Life's Origin or another nontoken Shrine enters the battlefield under your control, create a 1/1 colorless Shrine enchantment creature token.",
prices =
Prices
{ usd = Just "23.28",
usd_foil = Nothing,
eur = Just "23.05",
eur_foil = Nothing,
tix = Nothing
}
},
Card
{ scryfall_id = fromJust $ fromText "e2539ff7-2b7d-47e3-bd77-3138a6c42d2b",
name = "Godsire",
set_code = "ALA",
collector_number = "170",
rarity = "mythic",
color_identity = "WGR",
oracle_text = Just "Vigilance\n{T}: Create an 8/8 Beast creature token that's red, green, and white.",
prices =
Prices
{ usd = Just "7.22",
usd_foil = Just "16.56",
eur = Just "4.00",
eur_foil = Just "13.95",
tix = Just "0.02"
}
packageVersion :: T.Text
packageVersion = T.pack $ showVersion PT.version
status :: Handler Status
status =
return
Status
{ apiVersion = packageVersion
}
]
cards :: Maybe T.Text -> Handler [Card]
cards q =
return
[ Card
{ scryfall_id = fromJust $ fromText "f6cd7465-9dd0-473c-ac5e-dd9e2f22f5f6",
name = "Esika, God of the Tree // The Prismatic Bridge",
set_code = "KHM",
collector_number = "168",
rarity = "mythic",
color_identity = "WUBGR",
oracle_text = Nothing,
prices =
Prices
{ usd = Just "9.07",
usd_foil = Just "10.77",
eur = Just "7.79",
eur_foil = Just "12.27",
tix = Just "1.08"
}
},
Card
{ scryfall_id = fromJust $ fromText "d761ff73-0717-4ee4-996b-f5547bcf9b2f",
name = "Go-Shintai of Life's Origin",
set_code = "NEC",
collector_number = "66",
rarity = "mythic",
color_identity = "WUBGR",
oracle_text = Just "{W}{U}{B}{R}{G}, {T}: Return target enchantment card from your graveyard to the battlefield.\nWhenever Go-Shintai of Life's Origin or another nontoken Shrine enters the battlefield under your control, create a 1/1 colorless Shrine enchantment creature token.",
prices =
Prices
{ usd = Just "23.28",
usd_foil = Nothing,
eur = Just "23.05",
eur_foil = Nothing,
tix = Nothing
}
},
Card
{ scryfall_id = fromJust $ fromText "e2539ff7-2b7d-47e3-bd77-3138a6c42d2b",
name = "Godsire",
set_code = "ALA",
collector_number = "170",
rarity = "mythic",
color_identity = "WGR",
oracle_text = Just "Vigilance\n{T}: Create an 8/8 Beast creature token that's red, green, and white.",
prices =
Prices
{ usd = Just "7.22",
usd_foil = Just "16.56",
eur = Just "4.00",
eur_foil = Just "13.95",
tix = Just "0.02"
}
}
]