Skip to content

Add support for PATCH requests #139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Network/Wreq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ module Network.Wreq
-- ** PUT
, put
, putWith
-- ** PATCH
, patch
, patchWith
-- ** DELETE
, delete
, deleteWith
Expand Down Expand Up @@ -291,6 +294,14 @@ put url payload = putWith defaults url payload
putWith :: Putable a => Options -> String -> a -> IO (Response L.ByteString)
putWith opts url payload = runRead =<< preparePut opts url payload

-- | Issue a PATCH request.
patch :: Patchable a => String -> a -> IO (Response L.ByteString)
patch url payload = patchWith defaults url payload

-- | Issue a PATCH request, using the supplied 'Options'.
patchWith :: Patchable a => Options -> String -> a -> IO (Response L.ByteString)
patchWith opts url payload = runRead =<< preparePatch opts url payload

-- | Issue an OPTIONS request.
--
-- Example:
Expand Down
9 changes: 7 additions & 2 deletions Network/Wreq/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Network.Wreq.Internal
, runIgnore
, prepareOptions
, preparePut
, preparePatch
, prepareDelete
, prepareMethod
, preparePayloadMethod
Expand All @@ -34,7 +35,7 @@ import Network.HTTP.Client.Internal (Proxy(..), Request, Response(..), addProxy)
import Network.HTTP.Client.TLS (tlsManagerSettings)
import Network.Wreq.Internal.Lens (setHeader)
import Network.Wreq.Internal.Types (Mgr, Req(..), Run, RunHistory)
import Network.Wreq.Types (Auth(..), Options(..), Postable(..), Putable(..))
import Network.Wreq.Types (Auth(..), Options(..), Postable(..), Patchable(..), Putable(..))
import Prelude hiding (head)
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as Char8
Expand All @@ -49,7 +50,7 @@ import qualified Network.Wreq.Lens as Lens hiding (checkResponse)
-- This mess allows this module to continue to load during interactive
-- development in ghci :-(
#if defined(VERSION_base)
import Paths_wreq (version)
import Paths_wreq_patchable (version)
#else
import Data.Version (Version(..))
version :: Version
Expand Down Expand Up @@ -201,5 +202,9 @@ preparePut :: Putable a => Options -> String -> a -> IO Req
preparePut opts url payload = Req (manager opts) <$>
prepare (putPayload payload . (Lens.method .~ HTTP.methodPut)) opts url

preparePatch :: Patchable a => Options -> String -> a -> IO Req
preparePatch opts url payload = Req (manager opts) <$>
prepare (patchPayload payload . (Lens.method .~ HTTP.methodPatch)) opts url

prepareDelete :: Options -> String -> IO Req
prepareDelete = prepareMethod HTTP.methodDelete
9 changes: 9 additions & 0 deletions Network/Wreq/Internal/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module Network.Wreq.Internal.Types
-- * Request payloads
, Payload(..)
, Postable(..)
, Patchable(..)
, Putable(..)
-- ** URL-encoded forms
, FormParam(..)
Expand Down Expand Up @@ -218,6 +219,14 @@ class Postable a where
-- ^ Represent a value in the request body (and perhaps the
-- headers) of a POST request.

-- | A type that can be converted into a PATCH request payload.
class Patchable a where
patchPayload :: a -> Request -> IO Request
default patchPayload :: Putable a => a -> Request -> IO Request
patchPayload = putPayload
-- ^ Represent a value in the request body (and perhaps the
-- headers) of a PATCH request.

-- | A type that can be converted into a PUT request payload.
class Putable a where
putPayload :: a -> Request -> IO Request
Expand Down
14 changes: 14 additions & 0 deletions Network/Wreq/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Network.Wreq.Types
-- * Request payloads
, Payload(..)
, Postable(..)
, Patchable(..)
, Putable(..)
-- ** URL-encoded forms
, FormParam(..)
Expand Down Expand Up @@ -67,6 +68,19 @@ instance Postable L.ByteString
instance Postable Value
instance Postable Encoding

-- By default if the type is Putable, we use that as patchPayload
instance Patchable Part
instance Patchable [Part]
instance Patchable [(S.ByteString, S.ByteString)]
instance Patchable (S.ByteString, S.ByteString)
instance Patchable [FormParam]
instance Patchable FormParam
instance Patchable Payload
instance Patchable S.ByteString
instance Patchable L.ByteString
instance Patchable Value
instance Patchable Encoding

instance Putable Part where
putPayload a = putPayload [a]

Expand Down
36 changes: 1 addition & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,2 @@
# wreq: a Haskell web client library [![Build Status](https://travis-ci.org/bos/wreq.svg)](https://travis-ci.org/bos/wreq)
# wreq-patchable: A fork of wreq that allow PATCH requests

`wreq` is a library that makes HTTP client programming in Haskell
easy.

# Features

* Simple but powerful `lens`-based API

* Over 100 tests, and built on reliable libraries like [`http-client`](http://hackage.haskell.org/package/http-client/)
and [`lens`](https://lens.github.io/)

* Session handling includes connection keep-alive and pooling, and
cookie persistence

* Automatic decompression

* Powerful multipart form and file upload handling

* Support for JSON requests and responses, including navigation of
schema-less responses

* Basic and OAuth2 bearer authentication

* Amazon Web Services (AWS) request signing (Version 4)

* AWS signing supports sending requests through the
[Runscope Inc.](https://www.runscope.com) Traffic Inspector

# Tutorials

See [the tutorials](http://www.serpentine.com/wreq/) for a quick-start.

# Is it done?

No! See [`TODO.md`](TODO.md) for a rather long list of ideas.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
-*- markdown -*-

2020-04-22 1.0.0.0

* Fork and add PATCH support

2019-01-25 0.5.3.2

* Compatibility with http-client >= 0.6.0
Expand Down
40 changes: 9 additions & 31 deletions wreq.cabal → wreq-patchable.cabal
Original file line number Diff line number Diff line change
@@ -1,38 +1,16 @@
name: wreq
version: 0.5.3.2
name: wreq-patchable
version: 1.0.0.0
synopsis: An easy-to-use HTTP client library.
description:
.
A web client library that is designed for ease of use.
.
Tutorial: <http://www.serpentine.com/wreq/tutorial.html>
.
Features include:
.
* Simple but powerful `lens`-based API
.
* A solid test suite, and built on reliable libraries like
http-client and lens
.
* Session handling includes connection keep-alive and pooling, and
cookie persistence
.
* Automatic response body decompression
.
* Powerful multipart form and file upload handling
.
* Support for JSON requests and responses, including navigation of
schema-less responses
.
* Basic and OAuth2 bearer authentication
.
* Early TLS support via the tls package
A fork of wreq <https://hackage.haskell.org/package/wreq>
that supports HTTP PATCH requests
homepage: http://www.serpentine.com/wreq
bug-reports: https://github.com/bos/wreq/issues
bug-reports: https://github.com/nitros12/wreq-patchable/issues
license: BSD3
license-file: LICENSE.md
author: Bryan O'Sullivan <bos@serpentine.com>
maintainer: bos@serpentine.com
author: Bryan O'Sullivan <bos@serpentine.com>, Ben Simms <ben@bensimms.moe>
maintainer: ben@bensimms.moe
copyright: 2014 Bryan O'Sullivan
category: Web
build-type: Custom
Expand Down Expand Up @@ -97,7 +75,7 @@ library
Network.Wreq.Internal.Types
Network.Wreq.Lens.Machinery
Network.Wreq.Lens.TH
Paths_wreq
Paths_wreq_patchable
build-depends:
psqueues >= 0.2,
aeson >= 0.7.0.3,
Expand Down Expand Up @@ -209,7 +187,7 @@ test-suite tests
unix-compat,
uuid,
vector,
wreq
wreq-patchable

test-suite doctests
type: exitcode-stdio-1.0
Expand Down