Skip to content

Commit cbb4aa9

Browse files
Merge pull request #15 from Nike-Inc/refactor/network-req-types
Refactor/network req types
2 parents dafe91e + 0c4e720 commit cbb4aa9

6 files changed

Lines changed: 31 additions & 9 deletions

File tree

bartlett.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: bartlett
2-
version: 1.1.0
2+
version: 1.1.1
33
synopsis: The Jenkins command-line tool to serve your needs.
44
description: Please see README.md
55
homepage: https://github.com/Nike-inc/bartlett

src/Bartlett/Actions/Build.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ postBuild ::
4040
-> Maybe JobParameters -- ^ Optional set of job parameters to trigger with.
4141
-> IO ()
4242
postBuild user base path parameters = do
43-
resp <- execRequest "post" reqOpts reqUri Nothing
43+
resp <- execRequest Post reqOpts reqUri Nothing
4444
BL.putStrLn . encodePretty . BU.toResponseStatus $
4545
resp ^. responseStatus
4646
where (suffix, buildOpts) = consBuildType parameters

src/Bartlett/Actions/Config.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ getConfig :: BasicAuthUser a =>
3131
-> JobPath -- The Job for the given Jenkins instance to interact with.
3232
-> IO () -- The XML configuration for the given job.
3333
getConfig user base path = do
34-
resp <- execRequest "get" reqOpts (configUri base path) Nothing
34+
resp <- execRequest Get reqOpts (configUri base path) Nothing
3535
BL.putStrLn $ resp ^. responseBody
3636
where reqOpts = defaults & auth ?~ getBasicAuth user
3737

@@ -44,6 +44,6 @@ updateConfig :: BasicAuthUser a =>
4444
-> IO ()
4545
updateConfig user base path configPath = do
4646
configFile <- BL.readFile configPath
47-
resp <- execRequest "post" reqOpts (configUri base path) (Just configFile)
47+
resp <- execRequest Post reqOpts (configUri base path) (Just configFile)
4848
BL.putStrLn . encodePretty . toResponseStatus $ resp ^. responseStatus
4949
where reqOpts = defaults & auth ?~ getBasicAuth user

src/Bartlett/Actions/Info.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ getInfo ::
3333
-> IO ()
3434
getInfo user base [] = return ()
3535
getInfo user base (path:paths) = do
36-
resp <- execRequest "get" reqOpts reqUri Nothing
36+
resp <- execRequest Get reqOpts reqUri Nothing
3737
BL.putStrLn . toPrettyJson $ resp ^. responseBody
3838
getInfo user base paths
3939
where reqOpts = defaults & auth ?~ getBasicAuth user

src/Bartlett/Network.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Bartlett.Network (
1818
)where
1919

2020
import Bartlett.Util (toResponseStatus, withForcedSSL)
21+
import Bartlett.Types (RequestType(Get, Post))
2122

2223
import qualified Control.Exception as E
2324
import Data.Aeson.Encode.Pretty (encodePretty)
@@ -30,7 +31,7 @@ import qualified Network.Wreq.Session as S
3031

3132
-- | General request handler that provides basic error handling.
3233
execRequest ::
33-
ByteString -- ^ The type of request to make (e.g. "get")
34+
RequestType -- ^ The type of request to make
3435
-> Options -- ^ Request params to pass along with the request.
3536
-> ByteString -- ^ The uri to make the request to
3637
-> Maybe ByteString -- ^ The file to upload to the Jenkins instance.
@@ -41,13 +42,13 @@ execRequest requestType opts reqUrl postBody =
4142
-- TODO Need to get a CSRF crumb
4243
-- JENKINS_URL/crumbIssuer/api/json?xpath=?xpath=concat(//crumbRequestField,":",//crumb)')
4344
-- TODO create a proper sum type for requestType
44-
"post" ->
45+
Post ->
4546
postSession reqUrl
4647
`E.catch`
4748
recoverableErrorHandler (postSession $ withForcedSSL reqUrl)
4849
where fileToUpload = fromMaybe "" postBody :: ByteString
4950
postSession url = S.postWith opts session (unpack url) fileToUpload
50-
"get" ->
51+
Get ->
5152
getSession reqUrl
5253
`E.catch`
5354
recoverableErrorHandler (getSession $ withForcedSSL reqUrl)

src/Bartlett/Types.hs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,25 @@ Stability : stable
1010
1111
Types and type alises used throughout Bartlett.
1212
-}
13-
module Bartlett.Types where
13+
module Bartlett.Types (
14+
-- * Type Aliases
15+
JenkinsInstance,
16+
Username,
17+
Password,
18+
JobPath,
19+
JobParameters,
20+
Profile,
21+
ConfigPath,
22+
-- * User types
23+
BasicAuthUser(..),
24+
User(..),
25+
-- * Command-Line Types
26+
Command(..),
27+
Options(..),
28+
-- * Network Types
29+
StatusResponse(..),
30+
RequestType(..)
31+
) where
1432

1533
import Data.Aeson (ToJSON, FromJSON)
1634
import Data.ByteString.Lazy.Char8 (ByteString, toStrict)
@@ -70,3 +88,6 @@ data StatusResponse = StatusResponse {
7088
-- Derived JSON serlializers
7189
instance ToJSON StatusResponse
7290
instance FromJSON StatusResponse
91+
92+
-- | Incomplete sum type for network requests
93+
data RequestType = Get | Post

0 commit comments

Comments
 (0)