Skip to content

Commit

Permalink
Merge pull request #15 from Nike-Inc/refactor/network-req-types
Browse files Browse the repository at this point in the history
Refactor/network req types
  • Loading branch information
dogonthehorizon authored Nov 18, 2016
2 parents dafe91e + 0c4e720 commit cbb4aa9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
2 changes: 1 addition & 1 deletion bartlett.cabal
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: bartlett
version: 1.1.0
version: 1.1.1
synopsis: The Jenkins command-line tool to serve your needs.
description: Please see README.md
homepage: https://github.com/Nike-inc/bartlett
Expand Down
2 changes: 1 addition & 1 deletion src/Bartlett/Actions/Build.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ postBuild ::
-> Maybe JobParameters -- ^ Optional set of job parameters to trigger with.
-> IO ()
postBuild user base path parameters = do
resp <- execRequest "post" reqOpts reqUri Nothing
resp <- execRequest Post reqOpts reqUri Nothing
BL.putStrLn . encodePretty . BU.toResponseStatus $
resp ^. responseStatus
where (suffix, buildOpts) = consBuildType parameters
Expand Down
4 changes: 2 additions & 2 deletions src/Bartlett/Actions/Config.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ getConfig :: BasicAuthUser a =>
-> JobPath -- The Job for the given Jenkins instance to interact with.
-> IO () -- The XML configuration for the given job.
getConfig user base path = do
resp <- execRequest "get" reqOpts (configUri base path) Nothing
resp <- execRequest Get reqOpts (configUri base path) Nothing
BL.putStrLn $ resp ^. responseBody
where reqOpts = defaults & auth ?~ getBasicAuth user

Expand All @@ -44,6 +44,6 @@ updateConfig :: BasicAuthUser a =>
-> IO ()
updateConfig user base path configPath = do
configFile <- BL.readFile configPath
resp <- execRequest "post" reqOpts (configUri base path) (Just configFile)
resp <- execRequest Post reqOpts (configUri base path) (Just configFile)
BL.putStrLn . encodePretty . toResponseStatus $ resp ^. responseStatus
where reqOpts = defaults & auth ?~ getBasicAuth user
2 changes: 1 addition & 1 deletion src/Bartlett/Actions/Info.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ getInfo ::
-> IO ()
getInfo user base [] = return ()
getInfo user base (path:paths) = do
resp <- execRequest "get" reqOpts reqUri Nothing
resp <- execRequest Get reqOpts reqUri Nothing
BL.putStrLn . toPrettyJson $ resp ^. responseBody
getInfo user base paths
where reqOpts = defaults & auth ?~ getBasicAuth user
Expand Down
7 changes: 4 additions & 3 deletions src/Bartlett/Network.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module Bartlett.Network (
)where

import Bartlett.Util (toResponseStatus, withForcedSSL)
import Bartlett.Types (RequestType(Get, Post))

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

-- | General request handler that provides basic error handling.
execRequest ::
ByteString -- ^ The type of request to make (e.g. "get")
RequestType -- ^ The type of request to make
-> Options -- ^ Request params to pass along with the request.
-> ByteString -- ^ The uri to make the request to
-> Maybe ByteString -- ^ The file to upload to the Jenkins instance.
Expand All @@ -41,13 +42,13 @@ execRequest requestType opts reqUrl postBody =
-- TODO Need to get a CSRF crumb
-- JENKINS_URL/crumbIssuer/api/json?xpath=?xpath=concat(//crumbRequestField,":",//crumb)')
-- TODO create a proper sum type for requestType
"post" ->
Post ->
postSession reqUrl
`E.catch`
recoverableErrorHandler (postSession $ withForcedSSL reqUrl)
where fileToUpload = fromMaybe "" postBody :: ByteString
postSession url = S.postWith opts session (unpack url) fileToUpload
"get" ->
Get ->
getSession reqUrl
`E.catch`
recoverableErrorHandler (getSession $ withForcedSSL reqUrl)
Expand Down
23 changes: 22 additions & 1 deletion src/Bartlett/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,25 @@ Stability : stable
Types and type alises used throughout Bartlett.
-}
module Bartlett.Types where
module Bartlett.Types (
-- * Type Aliases
JenkinsInstance,
Username,
Password,
JobPath,
JobParameters,
Profile,
ConfigPath,
-- * User types
BasicAuthUser(..),
User(..),
-- * Command-Line Types
Command(..),
Options(..),
-- * Network Types
StatusResponse(..),
RequestType(..)
) where

import Data.Aeson (ToJSON, FromJSON)
import Data.ByteString.Lazy.Char8 (ByteString, toStrict)
Expand Down Expand Up @@ -70,3 +88,6 @@ data StatusResponse = StatusResponse {
-- Derived JSON serlializers
instance ToJSON StatusResponse
instance FromJSON StatusResponse

-- | Incomplete sum type for network requests
data RequestType = Get | Post

0 comments on commit cbb4aa9

Please sign in to comment.