|
| 1 | +{-# LANGUAGE TypeOperators #-} |
| 2 | +{-# LANGUAGE OverloadedStrings #-} |
| 3 | +{-# LANGUAGE LambdaCase #-} |
| 4 | +{-# LANGUAGE DerivingStrategies #-} |
| 5 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 6 | +{-# LANGUAGE DataKinds #-} |
| 7 | + |
| 8 | + |
| 9 | +module Main (main) where |
| 10 | + |
| 11 | +import Control.Monad.IO.Class |
| 12 | +import Data.Proxy |
| 13 | +import Servant.API |
| 14 | +import System.IO |
| 15 | +import System.Environment (getEnv) |
| 16 | +import qualified Data.Aeson as Aeson |
| 17 | +import qualified Data.Text as T |
| 18 | +import qualified Network.HTTP.Client as HTTP |
| 19 | +import qualified Network.HTTP.Conduit as HTTP |
| 20 | +import qualified Network.HTTP.Types as HTTP |
| 21 | +import qualified Network.Wai as Wai |
| 22 | +import qualified Network.Wai.Handler.Lambda as Lambda |
| 23 | +import qualified Network.Wai.Middleware.Cors as Cors |
| 24 | +import qualified Servant as Servant |
| 25 | +import qualified Servant.Client as Client |
| 26 | + |
| 27 | +-- https://api.unsplash.com/search/photos/?query={searchTerm}&page={next}&client_id={secret_key} |
| 28 | +-- https://api.unsplash.com/photos/{photoId}/download/?client_id={secret_key} |
| 29 | + |
| 30 | +newtype UnsplashQuery = UnsplashQuery { _unUnsplashQuery :: T.Text } |
| 31 | + deriving newtype (FromHttpApiData, ToHttpApiData) |
| 32 | + |
| 33 | +newtype UnsplashCliendId = UnsplashCliendId { _unUnsplashClientId :: T.Text } |
| 34 | + deriving newtype (FromHttpApiData, ToHttpApiData) |
| 35 | + |
| 36 | +newtype UnsplashPhotoId = UnsplashPhotoId { _unUnsplashPhotoId :: T.Text } |
| 37 | + deriving newtype (FromHttpApiData, ToHttpApiData) |
| 38 | + |
| 39 | +type ServerAPI = "unsplash" :> ClientAPI |
| 40 | + |
| 41 | +type ClientAPI = |
| 42 | + "search" :> |
| 43 | + "photos" :> |
| 44 | + QueryParam "query" UnsplashQuery :> |
| 45 | + QueryParam "client_id" UnsplashCliendId :> |
| 46 | + QueryParam "page" T.Text :> |
| 47 | + Get '[JSON] Aeson.Value :<|> |
| 48 | + "photos" :> |
| 49 | + Capture "photoId" UnsplashPhotoId :> |
| 50 | + "download" :> |
| 51 | + QueryParam "client_id" UnsplashCliendId :> |
| 52 | + Get '[JSON] Aeson.Value |
| 53 | + |
| 54 | +runClient |
| 55 | + :: MonadIO io |
| 56 | + => Client.ClientM a |
| 57 | + -> io (Either Client.ServantError a) |
| 58 | +runClient act = liftIO $ do |
| 59 | + mgr <- HTTP.newManager HTTP.tlsManagerSettings |
| 60 | + Client.runClientM act (clientEnv mgr) |
| 61 | + where |
| 62 | + clientEnv mgr = |
| 63 | + Client.mkClientEnv |
| 64 | + mgr (Client.BaseUrl Client.Https "api.unsplash.com" 443 "") |
| 65 | + |
| 66 | +getUnsplashClientId :: IO UnsplashCliendId |
| 67 | +getUnsplashClientId = |
| 68 | + (UnsplashCliendId . T.pack) <$> getEnv "UNSPLASH_CLIENT_ID" |
| 69 | + |
| 70 | +proxySearch |
| 71 | + :: Maybe UnsplashQuery |
| 72 | + -> Maybe UnsplashCliendId |
| 73 | + -> Maybe T.Text |
| 74 | + -> Servant.Handler Aeson.Value |
| 75 | +proxySearch mq Nothing mPage = do |
| 76 | + c <- liftIO getUnsplashClientId |
| 77 | + liftIO $ putStrLn "proxySearch: calling" |
| 78 | + runClient (proxySearch' mq (Just c) mPage) >>= \case |
| 79 | + Left e -> do |
| 80 | + liftIO $ print e |
| 81 | + Servant.throwError Servant.err500 |
| 82 | + Right bs -> pure bs |
| 83 | +proxySearch mq (Just c) mPage = do |
| 84 | + liftIO $ putStrLn "proxySearch: calling" |
| 85 | + runClient (proxySearch' mq (Just c) mPage) >>= \case |
| 86 | + Left e -> do |
| 87 | + liftIO $ print e |
| 88 | + Servant.throwError Servant.err500 |
| 89 | + Right bs -> pure bs |
| 90 | + |
| 91 | +proxySearch' |
| 92 | + :: Maybe UnsplashQuery |
| 93 | + -> Maybe UnsplashCliendId |
| 94 | + -> Maybe T.Text |
| 95 | + -> Client.ClientM Aeson.Value |
| 96 | +proxyDownload' |
| 97 | + :: UnsplashPhotoId |
| 98 | + -> Maybe UnsplashCliendId |
| 99 | + -> Client.ClientM Aeson.Value |
| 100 | +proxySearch' :<|> proxyDownload' = Client.client clientApi |
| 101 | + |
| 102 | +proxyDownload |
| 103 | + :: UnsplashPhotoId |
| 104 | + -> Maybe UnsplashCliendId |
| 105 | + -> Servant.Handler Aeson.Value |
| 106 | +proxyDownload photId Nothing = do |
| 107 | + c <- liftIO getUnsplashClientId |
| 108 | + liftIO $ putStrLn "proxyDownload: calling" |
| 109 | + runClient (proxyDownload' photId (Just c)) >>= \case |
| 110 | + Left e -> do |
| 111 | + liftIO $ print e |
| 112 | + Servant.throwError Servant.err500 |
| 113 | + Right bs -> pure bs |
| 114 | +proxyDownload photId (Just c) = do |
| 115 | + liftIO $ putStrLn "proxyDownload: calling" |
| 116 | + runClient (proxyDownload' photId (Just c)) >>= \case |
| 117 | + Left e -> do |
| 118 | + liftIO $ print e |
| 119 | + Servant.throwError Servant.err500 |
| 120 | + Right bs -> pure bs |
| 121 | + |
| 122 | +serverApi :: Proxy ServerAPI |
| 123 | +serverApi = Proxy |
| 124 | + |
| 125 | +clientApi :: Proxy ClientAPI |
| 126 | +clientApi = Proxy |
| 127 | + |
| 128 | +server :: Servant.Server ServerAPI |
| 129 | +server = proxySearch :<|> proxyDownload |
| 130 | + |
| 131 | +application :: Wai.Application |
| 132 | +application = Servant.serve serverApi server |
| 133 | + |
| 134 | +main :: IO () |
| 135 | +main = do |
| 136 | + hSetBuffering stdin LineBuffering |
| 137 | + hSetBuffering stdout LineBuffering |
| 138 | + Lambda.runSettings settings $ cors $ application |
| 139 | + where |
| 140 | + settings = Lambda.defaultSettings |
| 141 | + { Lambda.timeoutValue = 9 * 1000 * 1000 } |
| 142 | + |
| 143 | +cors :: Wai.Middleware |
| 144 | +cors = Cors.cors $ |
| 145 | + const $ |
| 146 | + Just Cors.simpleCorsResourcePolicy { Cors.corsMethods = methods } |
| 147 | + |
| 148 | +methods :: [HTTP.Method] |
| 149 | +methods = |
| 150 | + [ "GET" |
| 151 | + , "HEAD" |
| 152 | + ] |
0 commit comments