Safe Haskell | None |
---|---|
Language | Haskell2010 |
Low-level tools for querying the NationStates API.
Most of the time, you should use the high-level wrappers in e.g. NationStates.Nation instead. But if you need something not provided by these wrappers, then feel free to use this module directly.
- type NS = Compose ((,) Query) (Compose ((->) Query) ((->) Element))
- makeNS :: String -> String -> NS String
- makeNS' :: Query -> (Query -> Element -> a) -> NS a
- requestNS :: Maybe (String, String) -> NS a -> Context -> IO a
- apiVersion :: Integer
- data Query = Query {
- queryShards :: Map String (Set (Maybe Integer))
- queryOptions :: Map String String
- queryParams :: Map String String
- shard :: String -> Query
- shard' :: String -> Integer -> Query
- withOptions :: [(String, String)] -> Query
- withParams :: [(String, String)] -> Query
- data Context = Context {
- contextManager :: Manager
- contextRateLimit :: forall a. IO a -> IO a
- contextIsSecure :: Bool
- contextUserAgent :: String
- wordsBy :: (a -> Bool) -> [a] -> [[a]]
- readMaybe :: Read a => String -> Maybe a
- expect :: String -> String -> Maybe a -> a
- pureIf :: Alternative f => (a -> Bool) -> a -> f a
- module NationStates.Types
Requests
type NS = Compose ((,) Query) (Compose ((->) Query) ((->) Element)) Source
A request to the NationStates API.
- Construct an
NS
usingmakeNS
ormakeNS'
. - Compose
NS
values using theApplicative
interface. - Execute an
NS
usingrequestNS
.
This type wraps a query string, along with a function that parses the
response. The funky type machinery keeps these two parts in sync, as
long as you stick to the Applicative
interface.
type NS a = (Query
, Query ->Element
-> a)
Construct a request.
:: Maybe (String, String) | Request type |
-> NS a | Set of shards to request |
-> Context | Connection manager |
-> IO a |
Perform a request on the NationStates API.
The version of the NationStates API used by this package.
Every request to NationStates includes this number. This means that if the response format changes, existing code will continue to work under the old API.
This number should match the current API version, as given by https://www.nationstates.net/cgi-bin/api.cgi?a=version. If not, please file an issue.
Query strings
Keeps track of the set of shards to request.
Query | |
|
shard' :: String -> Integer -> Query Source
Create a query for a single shard, with an extra ID.
For example, the censusscore-23
shard would be written as:
shard' "censusscore" 23
.
withOptions :: [(String, String)] -> Query Source
Add extra ;
-delimited arguments.
withParams :: [(String, String)] -> Query Source
Add extra &
-delimited arguments.
Connection manager
Keeps track of rate limits and TLS connections.
You should create a single Context
at the start of your program,
then share it between multiple threads and requests.
Context | |
|
Utilities
wordsBy :: (a -> Bool) -> [a] -> [[a]] Source
Split a list by the given predicate, dropping empty sublists.
>>>
wordsBy (== ',') "the_vines,motesardo-east_adanzi,yellowapple"
["the_vines", "montesardo-east_adanzi", "yellowapple"]
>>>
wordsBy (== ',') ""
[]
readMaybe :: Read a => String -> Maybe a
Parse a string using the Read
instance.
Succeeds if there is exactly one valid result.
Since: 4.6.0.0
expect :: String -> String -> Maybe a -> a Source
Parse an input string using the given parser function.
If parsing fails, raise an error
.
>>>
(expect "integer" <*> readMaybe) "42" :: Integer
42
>>>
(expect "integer" <*> readMaybe) "butts" :: Integer
*** Exception: expected integer but got: butts
pureIf :: Alternative f => (a -> Bool) -> a -> f a Source
Return the value only if the given predicate is true.
>>>
pureIf (> 0) 5 :: Maybe Integer
Just 5
>>>
pureIf (> 0) (-2) :: Maybe Integer
Nothing
Data structures
module NationStates.Types