make arch optional for backwards compatability on version latest endpoint

This commit is contained in:
Lucy Cifferello
2022-11-29 14:19:40 -05:00
parent 42b670194b
commit b9ea7d82ab
3 changed files with 27 additions and 32 deletions

View File

@@ -19,7 +19,7 @@ import Model (
Metric (Metric),
PkgDependency (..),
PkgRecord (PkgRecord),
VersionRecord (VersionRecord), VersionPlatform, EntityField (VersionPlatformPkgId, VersionPlatformVersionNumber, VersionPlatformArch, VersionPlatformId, VersionRecordId),
VersionRecord (VersionRecord), VersionPlatform, EntityField (VersionPlatformPkgId, VersionPlatformVersionNumber, VersionPlatformArch),
)
import Orphans.Emver ()
import Startlude (

View File

@@ -19,7 +19,6 @@ import Network.HTTP.Types (status400)
import Startlude (Bool (True), Down (Down), Either (..), Generic, Maybe (..), NonEmpty, Show, const, encodeUtf8, filter, flip, nonEmpty, pure, ($), (.), (<$>), (<&>))
import Yesod (ToContent (..), ToTypedContent (..), YesodPersist (runDB), YesodRequest (reqGetParams), getRequest, sendResponseStatus)
import Handler.Util (getArchQuery)
import Control.Monad.Reader.Has ((>>=))
newtype VersionLatestRes = VersionLatestRes (HashMap PkgId (Maybe Version))
@@ -38,33 +37,32 @@ getVersionLatestR = do
getOsVersionQuery <&> \case
Nothing -> const True
Just v -> flip satisfies v
getArchQuery >>= \case
Nothing -> sendResponseStatus status400 (InvalidParamsE "Param is required" "arch")
Just osArch -> do
case lookup "ids" getParameters of
Nothing -> sendResponseStatus status400 (InvalidParamsE "get:ids" "<MISSING>")
Just packages -> case eitherDecode $ LBS.fromStrict $ encodeUtf8 packages of
Left _ -> sendResponseStatus status400 (InvalidParamsE "get:ids" packages)
Right p -> do
let packageList = (,Nothing) <$> p
let source = getPkgDataSource p osArch
filteredPackages <-
runDB $
runConduit $
source
-- group conduit pipeline by pkg id
.| collateVersions
-- filter out versions of apps that are incompatible with the OS predicate
.| mapC (second (filter (osPredicate' . versionRecordOsVersion)))
-- prune empty version sets
.| concatMapC (\(pkgId, vs) -> (pkgId,) <$> nonEmpty vs)
-- grab the latest matching version if it exists
.| mapC (\(a, b) -> (a, (Just $ selectLatestVersion b)))
.| sinkList
-- if the requested package does not have available versions, return it as a key with a null value
pure $
VersionLatestRes $
HM.union (HM.fromList $ filteredPackages) (HM.fromList packageList)
osArch <- getArchQuery
do
case lookup "ids" getParameters of
Nothing -> sendResponseStatus status400 (InvalidParamsE "get:ids" "<MISSING>")
Just packages -> case eitherDecode $ LBS.fromStrict $ encodeUtf8 packages of
Left _ -> sendResponseStatus status400 (InvalidParamsE "get:ids" packages)
Right p -> do
let packageList = (,Nothing) <$> p
let source = getPkgDataSource p osArch
filteredPackages <-
runDB $
runConduit $
source
-- group conduit pipeline by pkg id
.| collateVersions
-- filter out versions of apps that are incompatible with the OS predicate
.| mapC (second (filter (osPredicate' . versionRecordOsVersion)))
-- prune empty version sets
.| concatMapC (\(pkgId, vs) -> (pkgId,) <$> nonEmpty vs)
-- grab the latest matching version if it exists
.| mapC (\(a, b) -> (a, (Just $ selectLatestVersion b)))
.| sinkList
-- if the requested package does not have available versions, return it as a key with a null value
pure $
VersionLatestRes $
HM.union (HM.fromList $ filteredPackages) (HM.fromList packageList)
where
selectLatestVersion :: NonEmpty VersionRecord -> Version
selectLatestVersion vs = NE.head $ (versionRecordNumber <$>) $ NE.sortOn (Down . versionRecordNumber) $ vs

View File

@@ -87,9 +87,6 @@ import Yesod (
YesodPersist (runDB),
lookupGetParam,
)
import Yesod.Core.Handler (sendResponseStatus)
import Network.HTTP.Types (status400)
import Lib.Error (S9Error(InvalidParamsE))
data PackageReq = PackageReq
{ packageReqId :: !PkgId