gdris

A toy gopher client written in Idris2

git clone https://git.8pit.net/gdris.git

 1module Client
 2
 3import Gopher
 4import Network.Socket
 5
 6public export
 7createClient : Address -> IO (Either ResultCode Socket)
 8createClient addr = do
 9    Right sock <- socket AF_INET Stream 0
10        | Left fail => pure $ Left fail
11    res <- connect sock (Hostname $ fst addr) $ (cast $ snd addr)
12    if res /= 0
13        then pure $ Left res
14        else pure $ Right sock
15
16sendAndRecv : Socket -> String -> IO (Either ResultCode String)
17sendAndRecv sock input = do
18    n <- send sock input
19    case n of
20        Right _  => recvMsg sock
21        Left err => pure $ Left err
22
23public export
24makeReq : Address -> String -> IO (Either ResultCode String)
25makeReq addr input = do
26    sock <- createClient addr
27    case sock of
28        Right s => do out <- sendAndRecv s (input ++ "\r\n")
29                      close s
30                      pure out
31        Left err => pure $ Left err