Commit 1c6953e7 authored by Camil Staps's avatar Camil Staps 🍃

Make Cloogle module hierarchy; split up client functionality so that the API...

Make Cloogle module hierarchy; split up client functionality so that the API can be used without TCPIP+ArgEnv dependency
parent cac3a305
definition module Cloogle
definition module Cloogle.API
/**
* The API for Cloogle, a Clean search engine.
......@@ -11,7 +11,6 @@ from StdOverloaded import class zero, class fromString, class toString,
from Data.Error import :: MaybeError, :: MaybeErrorString
from Data.GenEq import generic gEq
from Data.Maybe import :: Maybe
from Internet.HTTP import :: HTTPRequest
from Text.GenJSON import generic JSONEncode, generic JSONDecode, :: JSONNode
/**
......@@ -317,7 +316,3 @@ instance == FunctionKind
toSingleLine :: Request -> Maybe String
parseSingleLineRequest :: String -> MaybeErrorString Request
toHTTPRequest :: Request -> HTTPRequest
request :: Request *World -> *(MaybeErrorString Response, *World)
implementation module Cloogle
implementation module Cloogle.API
import _SystemArray
import StdBool
import StdChar
from StdFunc import flip, o
from StdFunc import flip
import StdInt
import StdOverloaded
import StdString
from Control.Monad import class Monad(bind), =<<
import Data.Error
from Data.Func import $
import Data.Functor
import Data.Map
import Data.Tuple
import Internet.HTTP
import Data.Maybe
from Text import class Text(join,split,trim), instance Text String,
instance + String, <+
import Text.Encodings.UrlEncoding
import Text.GenJSON
import Data.GenEq
......@@ -44,7 +41,7 @@ where
ABCTypeString -> "string"
JSONDecode{|ABCArgument|} _ [JSONString s:r]
| size s > 0 && s.[0] == '[' && s.[size s-1] == ']'
| 0 < size s && s.[0] == '[' && s.[size s-1] == ']'
= (flip ABCArgument True <$> fromString (s % (1,size s-2)), r)
= (flip ABCArgument False <$> fromString s, r)
where
......@@ -154,31 +151,3 @@ toSingleLine req
Nothing -> Just $ ":: " + fromJust req.unify
Just n -> Just $ n + " :: " + fromJust req.unify
| otherwise = req.Request.name
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 = "cloogle.org"
}
where
params = catMaybes
[ tuple "str" <$> toSingleLine req
, 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"
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
definition module Cloogle.Client
from Data.Error import :: MaybeError, :: MaybeErrorString
from Internet.HTTP import :: HTTPRequest
import Cloogle.API
toHTTPRequest :: Request -> HTTPRequest
request :: Request *World -> *(MaybeErrorString Response, *World)
implementation module Cloogle.Client
from StdFunc import flip, o
from Control.Monad import class Monad(bind), =<<
import Data.Error
from Data.Func import $
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.GenJSON
import Cloogle.API
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 = "cloogle.org"
}
where
params = catMaybes
[ tuple "str" <$> toSingleLine req
, 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"
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