summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuchen Pei <hi@ypei.me>2022-09-09 12:00:17 +1000
committerYuchen Pei <hi@ypei.me>2022-09-09 12:00:17 +1000
commit0566547e07065581e2f45288eed63e4fb5874cf4 (patch)
tree2e966d3990268a93d3461fd572b41efadba6a1bd
parenta71d579c266e85245e422cb595caec4a61304fcd (diff)
Fixing json passthrough
-rw-r--r--app/Main.hs14
1 files changed, 8 insertions, 6 deletions
diff --git a/app/Main.hs b/app/Main.hs
index f9e522b..f4c7afc 100644
--- a/app/Main.hs
+++ b/app/Main.hs
@@ -73,6 +73,7 @@ import Servant ( (:<|>)(..)
, Get
, Handler
, JSON
+ , PlainText
, Server
, serve
)
@@ -91,8 +92,9 @@ type API = Wikipedia
type Wikipedia
= SearchWikipedia :<|> GetWikiFormat :<|> GetOrgFormat :<|> GetPandocFormat :<|> GetWpSummary :<|> GetInfobox
+-- TODO: fix the problem with plaintext having the wront content-type: text/plain
type SearchWikipedia
- = "wikipedia" :> "search" :> Capture "query" Text :> Get '[JSON] Value
+ = "wikipedia" :> "search" :> Capture "query" Text :> Get '[PlainText] Text
type GetWikiFormat
= "wikipedia" :> "wiki" :> Capture "name" Text :> Get '[JSON] Text
@@ -104,7 +106,7 @@ type GetPandocFormat
= "wikipedia" :> "pandoc" :> Capture "name" Text :> Get '[JSON] Text
type GetWpSummary
- = "wikipedia" :> "summary" :> Capture "name" Text :> Get '[JSON] Value
+ = "wikipedia" :> "summary" :> Capture "name" Text :> Get '[PlainText] Text
type GetInfobox
= "wikipedia" :> "infobox" :> Capture "name" Text :> Get '[JSON] (HM.HashMap Text Text)
@@ -118,13 +120,13 @@ server =
:<|> getWpSummary
:<|> getInfobox
-searchWikipedia :: Text -> Handler Value
+searchWikipedia :: Text -> Handler Text
searchWikipedia query = do
r <- liftIO $ get
("https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch="
<> (T.unpack query)
)
- return $ fromJust $ decode $ r ^. responseBody
+ return $ TE.decodeUtf8 $ BSL.toStrict $ r ^. responseBody
getWikiFormat :: Text -> Handler Text
getWikiFormat name = do
@@ -146,11 +148,11 @@ getPandocFormat name = do
{ writerWrapText = WrapNone
}
-getWpSummary :: Text -> Handler Value
+getWpSummary :: Text -> Handler Text
getWpSummary name = do
r <- liftIO $ get
("https://en.wikipedia.org/api/rest_v1/page/summary/" <> (T.unpack name))
- return $ fromJust $ decode $ r ^. responseBody
+ return $ TE.decodeUtf8 $ BSL.toStrict $ r ^. responseBody
getInfobox :: Text -> Handler (HM.HashMap Text Text)
getInfobox name = do