Skip to content

Commit aa06478

Browse files
committed
stop using AString; uniformly use Text and ByteString
1 parent 18a55e9 commit aa06478

File tree

2 files changed

+39
-28
lines changed

2 files changed

+39
-28
lines changed

BraveSearch/BraveSearch.hs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ module BraveSearch
66
) where
77

88
import Network.HTTP.Simple
9+
import Data.Text.Encoding (encodeUtf8)
910
import Data.Aeson
1011
import qualified Data.Text as T
1112
import Control.Exception (try)
1213
import Network.HTTP.Client (HttpException)
1314
import qualified Data.ByteString.Char8 as BS
14-
import qualified Data.ByteString.Lazy.Char8 as LBS
15+
-- removed unused import Data.ByteString.Lazy.Char8
1516

1617
data SearchResponse = SearchResponse
1718
{ query :: QueryInfo
@@ -57,32 +58,39 @@ instance FromJSON WebResult where
5758
<*> v .:? "url"
5859
<*> v .:? "description"
5960

60-
getSearchSuggestions :: String -> String -> IO (Either String [T.Text])
61+
-- | Perform a Brave Search with the given API key (as raw bytes) and text query.
62+
getSearchSuggestions :: BS.ByteString -> T.Text -> IO (Either T.Text [T.Text])
6163
getSearchSuggestions apiKey query = do
62-
let url = "https://api.search.brave.com/res/v1/web/search?q=" ++ query ++ "&country=US&count=5"
63-
64-
request <- parseRequest url
65-
let requestWithHeaders = setRequestHeader "Accept" ["application/json"]
66-
$ setRequestHeader "X-Subscription-Token" [BS.pack apiKey]
67-
$ request
68-
69-
result <- try $ httpLBS requestWithHeaders
70-
64+
-- Build base request
65+
let baseUrl = "https://api.search.brave.com/res/v1/web/search"
66+
request0 <- parseRequest baseUrl
67+
-- Add query parameters (URL-encoded) and headers
68+
let request1 = setRequestQueryString
69+
[ ("q", Just $ encodeUtf8 query)
70+
, ("country", Just "US")
71+
, ("count", Just "5")
72+
]
73+
request0
74+
request = setRequestHeader "Accept" ["application/json"]
75+
$ setRequestHeader "X-Subscription-Token" [apiKey]
76+
$ request1
77+
78+
result <- try $ httpLBS request
79+
7180
case result of
72-
Left e -> return $ Left $ "Network error: " ++ show (e :: HttpException)
73-
Right response -> do
74-
let statusCode = getResponseStatusCode response
75-
if statusCode /= 200
76-
then return $ Left $ "HTTP error: " ++ show statusCode
77-
else do
78-
let body = getResponseBody response
79-
case eitherDecode body of
80-
Left err -> return $ Left $ "JSON parsing error: " ++ err
81-
Right searchResponse@SearchResponse{..} -> do
82-
let originalQuery = original query
83-
webResults = results web
84-
let suggestions = "Original Query: " <> originalQuery : map formatResult webResults
85-
return $ Right suggestions
81+
Left e -> return . Left $ T.pack $ "Network error: " ++ show (e :: HttpException)
82+
Right response ->
83+
let status = getResponseStatusCode response
84+
in if status /= 200
85+
then return . Left $ T.pack $ "HTTP error: " ++ show status
86+
else case eitherDecode (getResponseBody response) of
87+
Left err -> return . Left $ T.pack $ "JSON parsing error: " ++ err
88+
Right SearchResponse{..} ->
89+
let originalQuery = original query
90+
webResults = results web
91+
suggestions = ("Original Query: " <> originalQuery)
92+
: map formatResult webResults
93+
in return $ Right suggestions
8694

8795
formatResult :: WebResult -> T.Text
8896
formatResult WebResult{..} =

BraveSearch/Main.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,27 @@
33
module Main where
44

55
import BraveSearch (getSearchSuggestions)
6+
import qualified Data.ByteString.Char8 as BS
67
import System.Environment (getEnv)
78
import qualified Data.Text as T
89
import qualified Data.Text.IO as TIO
910

1011
main :: IO ()
1112
main = do
1213
-- Get the API key from the environment variable
13-
apiKey <- getEnv "BRAVE_SEARCH_API_KEY"
14+
-- Read API key from environment and convert to ByteString
15+
apiKeyRaw <- getEnv "BRAVE_SEARCH_API_KEY"
16+
let apiKey = BS.pack apiKeyRaw
1417

1518
-- Prompt the user for a search query
1619
TIO.putStrLn "Enter a search query:"
1720
query <- TIO.getLine
1821

1922
-- Call the function to get search suggestions
20-
result <- getSearchSuggestions apiKey (T.unpack query)
23+
result <- getSearchSuggestions apiKey query
2124

2225
case result of
23-
Left err -> TIO.putStrLn $ "Error: " <> T.pack err
26+
Left err -> TIO.putStrLn $ "Error: " <> err
2427
Right suggestions -> do
2528
TIO.putStrLn "Search suggestions:"
2629
mapM_ (TIO.putStrLn . ("- " <>)) suggestions

0 commit comments

Comments
 (0)