Verified Commit a7664097 authored by Camil Staps's avatar Camil Staps 🚀

Resolve #2: basic request function

parent b8bb9ccf
......@@ -8,7 +8,9 @@ definition module Cloogle
from StdOverloaded import class zero, class fromString, class toString,
class fromInt, class toInt, class <, class ==
from Data.Error import :: MaybeError, :: MaybeErrorString
from Data.Maybe import :: Maybe
from Internet.HTTP import :: HTTPRequest
from Text.JSON import generic JSONEncode, generic JSONDecode, :: JSONNode
from GenEq import generic gEq
......@@ -240,3 +242,7 @@ instance < BasicResult
instance < Result
instance == FunctionKind
toHTTPRequest :: Request -> HTTPRequest
request :: Request *World -> *(MaybeErrorString Response, *World)
implementation module Cloogle
from StdFunc import o
import StdOverloaded
from Control.Monad import =<<
import Data.Error
from Data.Func import $
import Text
import Data.Functor
import Data.Map
import Data.Tuple
import Internet.HTTP
from Text import class Text(join), instance Text String, instance + String
import Text.Encodings.UrlEncoding
import Text.JSON
import GenEq
......@@ -76,3 +84,40 @@ where
basic (ModuleResult (br,_)) = br
instance == FunctionKind where == a b = a === b
toHTTPRequest :: Request -> HTTPRequest
toHTTPRequest req =
{ newHTTPRequest
& req_method = HTTP_GET
, req_path = "/api.php"
, req_query = "?" + urlEncodePairs params
, req_headers = put "User-Agent" "Clean/libcloogle" newMap
, server_name = "local.cloogle.org"
}
where
params = catMaybes
[ Just ("str",searchStr)
, tuple "lib" o join "," <$> req.libraries
, tuple "mod" o join "," <$> req.modules
, tuple "include_builtins" o printBool <$> req.include_builtins
, tuple "include_core" o printBool <$> req.include_core
, tuple "page" o toString <$> req.page
]
where printBool b = if b "true" "false"
searchStr
| isJust req.typeName = fromJust req.typeName
| isJust req.className = fromJust req.className
| isJust req.unify = case req.name of
Nothing -> ":: " + fromJust req.unify
Just n -> n + " :: " + fromJust req.unify
| isJust req.name = fromJust req.name
| otherwise = ""
request :: Request *World -> *(MaybeErrorString Response, *World)
request req w = appFst ((=<<) parseResponse) $ doHTTPRequest (toHTTPRequest req) 10000 w
where
parseResponse :: HTTPResponse -> MaybeErrorString Response
parseResponse res = case fromJSON $ fromString res.rsp_data of
Nothing -> Error "Invalid JSON"
Just r -> Ok r
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment