7 import qualified Data.ByteString.Lazy as B ( ByteString )
8 import qualified Data.ByteString.Char8 as BC ( pack )
9 import Network.Connection ( TLSSettings(..) )
10 import Network.HTTP.Client (
16 import Network.HTTP.Client.TLS ( mkManagerSettings )
17 import Web.Authenticate.OAuth (
24 import Configuration ( Cfg(..) )
27 -- | The API URL of username's timeline.
31 -- <https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline>
33 user_timeline_url :: String -> String
34 user_timeline_url username =
35 concat [ "https://api.twitter.com/",
38 "user_timeline.json?",
45 -- | Given username's last status id, constructs the API URL for
46 -- username's new statuses. Essentially, 'user_timeline_url' with a
47 -- \"since_id\" parameter tacked on.
51 -- >>> user_new_statuses_url "someuser" 8675309
52 -- "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=someuser&include_rts=true&count=10&since_id=8675309"
54 user_new_statuses_url :: String -> Integer -> String
55 user_new_statuses_url username last_status_id =
56 url ++ "&since_id=" ++ since_id
58 url = user_timeline_url username
59 since_id = show last_status_id
62 -- | Return's username's timeline.
64 get_user_timeline :: Cfg -> String -> IO B.ByteString
65 get_user_timeline cfg username = do
66 let uri = user_timeline_url username
70 -- | Returns the JSON representing all of username's statuses that are
71 -- newer than @last_status_id@.
73 get_user_new_statuses :: Cfg -> String -> Integer -> IO B.ByteString
74 get_user_new_statuses cfg username last_status_id = do
75 let uri = user_new_statuses_url username last_status_id
79 -- | The default 'tlsManagerSettings' attempts to verify the
80 -- certificate that we get from the server, but it's quite pointless
83 -- The default 'tlsManagerSettings' is constructed is like,
85 -- tlsManagerSettings = mkManagerSettings def Nothing
87 -- and the 'def' passes in a 'TLSSettings' from
88 -- \"Network.Connection\". By constructing our own 'TLSSettings', we
89 -- can disable the certificate validation.
91 novalidate_tls_manager_settings :: ManagerSettings
92 novalidate_tls_manager_settings =
93 mkManagerSettings mytls Nothing
95 -- The first field is "disable validation"
96 mytls = TLSSettingsSimple True False False
99 -- | Retrieve a URL, or crash. The request is signed using all of the
100 -- OAuth junk contained in the configuration.
102 http_get :: Cfg -> String -> IO B.ByteString
103 http_get cfg url = do
104 manager <- newManager novalidate_tls_manager_settings
105 request <- parseUrl url
106 signed_request <- signOAuth oauth credential request
107 response <- httpLbs signed_request manager
108 return $ responseBody response
111 consumer_key' = BC.pack (consumer_key cfg)
112 consumer_secret' = BC.pack (consumer_secret cfg)
113 access_token' = BC.pack (access_token cfg)
114 access_secret' = BC.pack (access_secret cfg)
118 oauthConsumerKey = consumer_key',
119 oauthConsumerSecret = consumer_secret' }
121 credential :: Credential
122 credential = newCredential access_token' access_secret'