module Ballast.Client where
import Ballast.Types
import Data.Aeson (eitherDecode, encode)
import Data.Aeson.Types
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.Monoid ((<>))
import Data.String (IsString)
import qualified Data.Text as T
import Network.HTTP.Client
import Network.HTTP.Client.TLS
import qualified Network.HTTP.Types.Method as NHTM
paramsToByteString
:: (Monoid m, IsString m)
=> [(m, m)]
-> m
paramsToByteString [] = mempty
paramsToByteString ((x,y) : []) = x <> "=" <> y
paramsToByteString ((x,y) : xs) =
mconcat [ x, "=", y, "&" ] <> paramsToByteString xs
createRateRequest :: GetRate -> ShipwireRequest RateRequest TupleBS8 BSL.ByteString
createRateRequest getRate = mkShipwireRequest NHTM.methodPost url params
where
url = "/rate"
params = [Body (encode getRate)]
getStockInfo :: ShipwireRequest StockRequest TupleBS8 BSL.ByteString
getStockInfo = mkShipwireRequest NHTM.methodGet url params
where
url = "/stock"
params = []
getReceivings :: ShipwireRequest GetReceivingsRequest TupleBS8 BSL.ByteString
getReceivings = mkShipwireRequest NHTM.methodGet url params
where
url = "/receivings"
params = []
createReceiving :: CreateReceiving -> ShipwireRequest CreateReceivingRequest TupleBS8 BSL.ByteString
createReceiving crReceiving = mkShipwireRequest NHTM.methodPost url params
where
url = "/receivings"
params = [Body (encode crReceiving)]
getReceiving :: ReceivingId -> ShipwireRequest GetReceivingRequest TupleBS8 BSL.ByteString
getReceiving receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.append "/receivings/" $ getReceivingId receivingId
params = []
modifyReceiving :: ReceivingId -> ModifyReceiving -> ShipwireRequest ModifyReceivingRequest TupleBS8 BSL.ByteString
modifyReceiving receivingId modReceiving = request
where
request = mkShipwireRequest NHTM.methodPut url params
url = T.append "/receivings/" $ getReceivingId receivingId
params = [Body (encode modReceiving)]
cancelReceiving :: ReceivingId -> ShipwireRequest CancelReceivingRequest TupleBS8 BSL.ByteString
cancelReceiving receivingId = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/cancel"]
params = []
cancelReceivingLabels :: ReceivingId -> ShipwireRequest CancelReceivingLabelsRequest TupleBS8 BSL.ByteString
cancelReceivingLabels receivingId = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/labels/cancel"]
params = []
getReceivingHolds :: ReceivingId -> ShipwireRequest GetReceivingHoldsRequest TupleBS8 BSL.ByteString
getReceivingHolds receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/holds"]
params = []
getReceivingInstructionsRecipients :: ReceivingId -> ShipwireRequest GetReceivingInstructionsRecipientsRequest TupleBS8 BSL.ByteString
getReceivingInstructionsRecipients receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/instructionsRecipients"]
params = []
getReceivingItems :: ReceivingId -> ShipwireRequest GetReceivingItemsRequest TupleBS8 BSL.ByteString
getReceivingItems receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/items"]
params = []
getReceivingShipments :: ReceivingId -> ShipwireRequest GetReceivingShipmentsRequest TupleBS8 BSL.ByteString
getReceivingShipments receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/shipments"]
params = []
getReceivingTrackings :: ReceivingId -> ShipwireRequest GetReceivingTrackingsRequest TupleBS8 BSL.ByteString
getReceivingTrackings receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/trackings"]
params = []
getReceivingLabels :: ReceivingId -> ShipwireRequest GetReceivingLabelsRequest TupleBS8 BSL.ByteString
getReceivingLabels receivingId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.concat ["/receivings/", getReceivingId receivingId, "/labels"]
params = []
getProducts :: ShipwireRequest GetProductsRequest TupleBS8 BSL.ByteString
getProducts = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = "/products"
params = []
createProduct :: [CreateProductsWrapper] -> ShipwireRequest CreateProductsRequest TupleBS8 BSL.ByteString
createProduct cpr = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = "/products"
params = [Body (encode cpr)]
modifyProducts :: [CreateProductsWrapper] -> ShipwireRequest ModifyProductsRequest TupleBS8 BSL.ByteString
modifyProducts mpr = request
where
request = mkShipwireRequest NHTM.methodPut url params
url = "/products"
params = [Body (encode mpr)]
modifyProduct :: CreateProductsWrapper -> Id -> ShipwireRequest ModifyProductRequest TupleBS8 BSL.ByteString
modifyProduct mpr productId = request
where
request = mkShipwireRequest NHTM.methodPut url params
url = T.append "/products/" $ T.pack . show $ unId productId
params = [Body (encode mpr)]
getProduct :: Id -> ShipwireRequest GetProductRequest TupleBS8 BSL.ByteString
getProduct productId = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = T.append "/products/" $ T.pack . show $ unId productId
params = []
retireProducts :: ProductsToRetire -> ShipwireRequest RetireProductsRequest TupleBS8 BSL.ByteString
retireProducts ptr = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = "/products/retire"
params = [Body (encode ptr)]
getOrders :: ShipwireRequest GetOrdersRequest TupleBS8 BSL.ByteString
getOrders = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = "/orders"
params = []
getOrder :: IdWrapper -> ShipwireRequest GetOrderRequest TupleBS8 BSL.ByteString
getOrder idw = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = case idw of
(WrappedId x) -> T.concat ["/orders/", T.pack . show $ unId x]
(WrappedExternalId x) -> T.concat ["/orders/E", unExternalId x]
params = []
createOrder :: CreateOrder -> ShipwireRequest CreateOrderRequest TupleBS8 BSL.ByteString
createOrder co = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = "/orders"
params = [Body (encode co)]
cancelOrder :: IdWrapper -> ShipwireRequest CancelOrderRequest TupleBS8 BSL.ByteString
cancelOrder idw = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = case idw of
(WrappedId x) -> T.concat ["/orders/", T.pack . show $ unId x, "/cancel"]
(WrappedExternalId x) -> T.concat ["/orders/E", unExternalId x, "/cancel"]
params = []
getOrderTrackings :: IdWrapper -> ShipwireRequest GetOrderTrackingsRequest TupleBS8 BSL.ByteString
getOrderTrackings idwr = request
where
request = mkShipwireRequest NHTM.methodGet url params
url = case idwr of
(WrappedId x) -> T.concat ["/orders/", T.pack . show $ unId x, "/trackings"]
(WrappedExternalId x) -> T.concat ["/orders/E", unExternalId x, "/trackings"]
params = []
validateAddress :: AddressToValidate -> ShipwireRequest ValidateAddressRequest TupleBS8 BSL.ByteString
validateAddress atv = request
where
request = mkShipwireRequest NHTM.methodPost url params
url = ".1/addressValidation"
params = [Body (encode atv)]
shipwire' :: (FromJSON (ShipwireReturn a))
=> ShipwireConfig
-> ShipwireRequest a TupleBS8 BSL.ByteString
-> IO (Response BSL.ByteString)
shipwire' ShipwireConfig {..} ShipwireRequest {..} = do
manager <- newManager tlsManagerSettings
initReq <- parseRequest $ T.unpack $ T.append (hostUri host) endpoint
let reqBody | rMethod == NHTM.methodGet = mempty
| otherwise = filterBody params
reqURL = paramsToByteString $ filterQuery params
req = initReq { method = rMethod
, requestBody = RequestBodyLBS reqBody
, queryString = reqURL
}
shipwireUser = unUsername email
shipwirePass = unPassword pass
authorizedRequest = applyBasicAuth shipwireUser shipwirePass req
httpLbs authorizedRequest manager
data ShipwireError =
ShipwireError {
parseError :: String
, shipwireResponse :: Response BSL.ByteString
} deriving (Eq, Show)
shipwire
:: (FromJSON (ShipwireReturn a))
=> ShipwireConfig
-> ShipwireRequest a TupleBS8 BSL.ByteString
-> IO (Either ShipwireError (ShipwireReturn a))
shipwire config request = do
response <- shipwire' config request
let result = eitherDecode $ responseBody response
case result of
Left s -> return (Left (ShipwireError s response))
(Right r) -> return (Right r)