Skip to content

support deploymentID but keep it backward compatible #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/DataRobot.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ module DataRobot
, defaultBaseURLPredict
, ProjectID(..)
, ModelID(..)
, DeploymentID (..)
, ModelIdentifier(..)
, projectID
, modelID
, deploymentID

-- * Features API
, features
Expand Down
14 changes: 8 additions & 6 deletions src/DataRobot/Features.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ import Network.Wreq.Types (ResponseChecker)


-- GET https://app.datarobot.com/api/v2/projects/5988c39bc808917519a2acbb/models/5988d164c8089128924bd6cf/features
features :: (MonadIO m, MonadThrow m) => Credentials -> ProjectID -> ModelID -> m Features
features c pid mid = do
features :: (MonadIO m, MonadThrow m) => Credentials -> ModelIdentifier -> m Features
features c mid = do
let opts = httpOptions c
url = featuresEndpoint (baseURL c) pid mid
url = featuresEndpoint (baseURL c) mid
r <- liftIO $ Wreq.getWith opts url
f <- parseResponse r
pure f


featuresEndpoint :: URI -> ProjectID -> ModelID -> String
featuresEndpoint base (ProjectID pid) (ModelID mid) =
featuresEndpoint :: URI -> ModelIdentifier -> String
featuresEndpoint base mident = do
let (ProjectID pid) = projectID mident
(ModelID mid) = modelID mident
endpoint base ["projects", cs pid, "models", cs mid, "features/"]


Expand All @@ -48,4 +50,4 @@ httpOptions c =
ignoreStatus _ _ = pure ()

authorization :: ByteString
authorization = "Token " <> (apiToken c)
authorization = "Token " <> apiToken c
22 changes: 11 additions & 11 deletions src/DataRobot/Predict.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ module DataRobot.Predict
, Credentials(..)
, ProjectID(..)
, ModelID(..)
, DeploymentID (..)
, ModelIdentifier(..)
) where

import Lens.Micro ((?~), (.~))
import Control.Monad.IO.Class (MonadIO, liftIO)
import Control.Monad.Catch (MonadThrow)
import Data.Aeson (Value(..), encode)
import Data.Aeson (Value(..))
import Data.Function ((&))
import qualified Data.HashMap.Strict as HM
import qualified Data.Vector as V
Expand All @@ -28,32 +30,30 @@ import Network.Wreq.Types (ResponseChecker)


-- https://app.datarobot.com/docs/users-guide/basics/predictions/prediction-api.html
predict :: (MonadIO m, MonadThrow m) => Credentials -> ProjectID -> ModelID -> Fields -> m (Either PredictError PredictResult)
predict c pid mid o = do
predict :: (MonadIO m, MonadThrow m) => Credentials -> ModelIdentifier -> Fields -> m (Either PredictError PredictResult)
predict c mident o = do
let opts = httpOptions c
url = predictURI (baseURLPredict c) pid mid
url = predictURI (baseURLPredict c) mident
body = Array $ V.singleton $ Object $ HM.fromList o
r <- liftIO $ Wreq.postWith opts url body
pure $ parseResponse r


httpOptions :: Credentials -> Wreq.Options
httpOptions c =
defaults
& auth ?~ (authorization c)
& auth ?~ authorization c
& header "datarobot-key" .~ [apiKey c]
& checkResponse .~ Just ignoreStatus
where
ignoreStatus :: ResponseChecker
ignoreStatus _ _ = pure ()



predictURI :: URI -> ProjectID -> ModelID -> String
predictURI base (ProjectID pid) (ModelID mid) =
predictURI :: URI -> ModelIdentifier -> String
predictURI base (ProjectBase (ProjectID pid) (ModelID mid)) =
endpoint base [cs pid, cs mid, "predict"]


predictURI base (DeploymentBase (DeploymentID did) _ _)=
endpoint base ["deployments",cs did, "predictions"]

authorization :: Credentials -> Auth
authorization c =
Expand Down
2 changes: 1 addition & 1 deletion src/DataRobot/PredictResponse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ responseFailure e = Left $ APIError 422 e
-- Parse the entire prediction response
-- This is needed because some of the data is delivered in the body and some is delivered via headers
parseResponse :: Response ByteString -> Either PredictError PredictResult
parseResponse r = do
parseResponse r =
either (responseFailure . cs) (handleResponse tm) $ eitherDecode b
where
b = r ^. responseBody
Expand Down
29 changes: 25 additions & 4 deletions src/DataRobot/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,43 @@ import Network.URI (URI(..))

data Credentials = Credentials
{ apiToken :: ByteString
, apiKey :: ByteString
, apiKey :: ByteString
, username :: ByteString
, baseURLPredict :: URI
, baseURL :: URI
} deriving (Show, Eq)


newtype ProjectID = ProjectID Text
deriving (Show, Eq, ToJSON, FromJSON)
deriving (Show, Eq, Generic, ToJSON, FromJSON)


newtype ModelID = ModelID Text
deriving (Show, Eq, ToJSON, FromJSON)
deriving (Show, Eq, Generic, ToJSON, FromJSON)

newtype DeploymentID = DeploymentID Text
deriving (Show, Eq, Generic, ToJSON, FromJSON)

data Features = Features
data ModelIdentifier
= ProjectBase ProjectID ModelID
| DeploymentBase DeploymentID ProjectID ModelID
deriving (Show, Eq, Generic)
instance ToJSON ModelIdentifier
instance FromJSON ModelIdentifier

projectID :: ModelIdentifier -> ProjectID
projectID (ProjectBase pid _) = pid
projectID (DeploymentBase _ pid _) = pid

modelID :: ModelIdentifier -> ModelID
modelID (ProjectBase _ mid) = mid
modelID (DeploymentBase _ _ mid) = mid

deploymentID :: ModelIdentifier -> Maybe DeploymentID
deploymentID (ProjectBase _ _) = Nothing
deploymentID (DeploymentBase did _ _) = Just did

newtype Features = Features
{ featureNames :: [Text]
} deriving (Show, Eq, Generic)
instance ToJSON Features
Expand Down