extism-pdk 1.0.0.1 → 1.1.0.0
raw patch · 5 files changed
+28/−38 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
- Extism.PDK.HTTP: [memory] :: Response -> Memory
- Extism.PDK.HTTP: responseMemory :: Response -> Memory
+ Extism.PDK.HTTP: [responseData] :: Response -> ByteString
- Extism.PDK.HTTP: Response :: Int -> Memory -> Response
+ Extism.PDK.HTTP: Response :: Int -> ByteString -> Response
- Extism.PDK.HTTP: response :: FromBytes a => Response -> IO (Either String a)
+ Extism.PDK.HTTP: response :: FromBytes a => Response -> Either String a
- Extism.PDK.HTTP: responseByteString :: Response -> IO ByteString
+ Extism.PDK.HTTP: responseByteString :: Response -> ByteString
- Extism.PDK.HTTP: responseString :: Response -> IO String
+ Extism.PDK.HTTP: responseString :: Response -> String
Files
- CHANGELOG.md +4/−0
- examples/HTTPGet.hs +1/−1
- extism-pdk.cabal +1/−1
- src/Extism/PDK.hs +0/−10
- src/Extism/PDK/HTTP.hs +22/−26
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for extism-pdk +## 1.1.0.0++* Remove calls to free where Extism host expects to take ownership+ ## 1.0.0.0 * Extism 1.0 compatible release
examples/HTTPGet.hs view
@@ -20,7 +20,7 @@ -- Send the request, get a 'Response' res <- sendRequest req (Nothing :: Maybe String) -- Save response body to memory- outputMemory (memory res)+ output (responseString res) -- Return code return 0
extism-pdk.cabal view
@@ -1,6 +1,6 @@ cabal-version: 3.0 name: extism-pdk-version: 1.0.0.1+version: 1.1.0.0 -- A short (one-line) description of the package. synopsis: Extism Plugin Development Kit
src/Extism/PDK.hs view
@@ -65,13 +65,11 @@ getVar key = do k <- allocString key v <- extismGetVar (memoryOffset k)- free k if v == 0 then return Nothing else do mem <- findMemory v bs <- load mem- free k case bs of Left _ -> return Nothing Right x -> return (Just x)@@ -81,20 +79,16 @@ setVar key Nothing = do k <- allocString key extismSetVar (memoryOffset k) 0- free k setVar key (Just v) = do k <- allocString key x <- alloc v extismSetVar (memoryOffset k) (memoryOffset x)- free k- free x -- | Get a configuration value getConfig :: String -> IO (Maybe String) getConfig key = do k <- allocString key v <- extismGetConfig (memoryOffset k)- free k if v == 0 then return Nothing else do@@ -117,19 +111,15 @@ log LogInfo msg = do s <- allocString msg extismLogInfo (memoryOffset s)- free s log LogDebug msg = do s <- allocString msg extismLogDebug (memoryOffset s)- free s log LogWarn msg = do s <- allocString msg extismLogWarn (memoryOffset s)- free s log LogError msg = do s <- allocString msg extismLogError (memoryOffset s)- free s -- Log with "error" level logError :: String -> IO ()
src/Extism/PDK/HTTP.hs view
@@ -11,7 +11,8 @@ import Extism.PDK import Extism.PDK.Bindings import Extism.PDK.Memory-import Text.JSON (Result(..), decode, encode, makeObj)+import Extism.PDK.Util (fromByteString)+import Text.JSON (Result (..), decode, encode, makeObj) import qualified Text.JSON.Generic -- | HTTP Request@@ -25,7 +26,7 @@ -- | HTTP Response data Response = Response { statusCode :: Int,- memory :: Memory+ responseData :: ByteString } -- | Creates a new 'Request'@@ -43,36 +44,30 @@ withHeaders h req = req {headers = h} --- | Access the Memory block associated with a 'Response'-responseMemory :: Response -> Memory-responseMemory (Response _ mem) = mem- -- | Get the 'Response' body as a 'ByteString'-responseByteString :: Response -> IO ByteString-responseByteString (Response _ mem) = do- a <- load mem- case a of- Left e -> error e- Right x -> return x+responseByteString :: Response -> ByteString+responseByteString (Response _ mem) = mem -- | Get the 'Response' body as a 'String'-responseString :: Response -> IO String-responseString (Response _ mem) = loadString mem+responseString :: Response -> String+responseString (Response _ mem) = fromByteString mem -- | Get the 'Response' body as JSON responseJSON :: (Text.JSON.Generic.Data a) => Response -> IO (Either String a)-responseJSON (Response _ mem) = do- json <- decode <$> loadString mem+responseJSON res = do case json of Ok json -> case Text.JSON.Generic.fromJSON json of Ok x -> return $ Right x Error msg -> return (Left msg) Error msg -> return (Left msg)+ where+ s = responseString res+ json = decode s -- | Get the 'Response' body and decode it-response :: (FromBytes a) => Response -> IO (Either String a)-response (Response _ mem) = load mem+response :: (FromBytes a) => Response -> Either String a+response (Response _ mem) = fromBytes mem -- | Send HTTP request with an optional request body sendRequestWithBody :: (ToBytes a) => Request -> a -> IO Response@@ -87,14 +82,14 @@ } j <- allocString json res <- extismHTTPRequest (memoryOffset j) (memoryOffset body)- free j- free body code <- extismHTTPStatusCode if res == 0- then return (Response (fromIntegral code) (Memory 0 0))+ then return (Response (fromIntegral code) empty) else do mem <- findMemory res- return (Response (fromIntegral code) mem)+ bs <- loadByteString mem+ free mem+ return (Response (fromIntegral code) bs) -- | Send HTTP request with an optional request body sendRequest :: (ToBytes a) => Request -> Maybe a -> IO Response@@ -113,11 +108,12 @@ body <- bodyMem j <- allocString json res <- extismHTTPRequest (memoryOffset j) (memoryOffset body)- free j- free body code <- extismHTTPStatusCode if res == 0- then return (Response (fromIntegral code) (Memory 0 0))+ then return (Response (fromIntegral code) empty) else do len <- extismLengthUnsafe res- return (Response (fromIntegral code) (Memory res len))+ let mem = Memory res len+ bs <- loadByteString mem+ free mem+ return (Response (fromIntegral code) bs)