|
| 1 | +module Data.HTTP.Method |
| 2 | + ( Method(..) |
| 3 | + , CustomMethod() |
| 4 | + , fromString |
| 5 | + , print |
| 6 | + ) where |
| 7 | + |
| 8 | +import Prelude |
| 9 | + |
| 10 | +import Data.Either (Either(..), either) |
| 11 | +import Data.Generic (Generic, gCompare) |
| 12 | +import Data.String as Str |
| 13 | + |
| 14 | +data Method |
| 15 | + -- HTTP/1.1 |
| 16 | + = OPTIONS |
| 17 | + | GET |
| 18 | + | HEAD |
| 19 | + | POST |
| 20 | + | PUT |
| 21 | + | DELETE |
| 22 | + | TRACE |
| 23 | + | CONNECT |
| 24 | + |
| 25 | + -- RFC 2518 |
| 26 | + | PROPFIND |
| 27 | + | PROPPATCH |
| 28 | + | MKCOL |
| 29 | + | COPY |
| 30 | + | MOVE |
| 31 | + | LOCK |
| 32 | + | UNLOCK |
| 33 | + |
| 34 | + -- RFC5789 |
| 35 | + | PATCH |
| 36 | + |
| 37 | +derive instance genericMethod :: Generic Method |
| 38 | + |
| 39 | +instance eqMethod :: Eq Method where |
| 40 | + eq OPTIONS OPTIONS = true |
| 41 | + eq GET GET = true |
| 42 | + eq HEAD HEAD = true |
| 43 | + eq POST POST = true |
| 44 | + eq PUT PUT = true |
| 45 | + eq DELETE DELETE = true |
| 46 | + eq TRACE TRACE = true |
| 47 | + eq CONNECT CONNECT = true |
| 48 | + eq PROPFIND PROPFIND = true |
| 49 | + eq PROPPATCH PROPPATCH = true |
| 50 | + eq MKCOL MKCOL = true |
| 51 | + eq COPY COPY = true |
| 52 | + eq MOVE MOVE = true |
| 53 | + eq LOCK LOCK = true |
| 54 | + eq UNLOCK UNLOCK = true |
| 55 | + eq PATCH PATCH = true |
| 56 | + eq _ _ = false |
| 57 | + |
| 58 | +instance ordMethod :: Ord Method where |
| 59 | + compare = gCompare |
| 60 | + |
| 61 | +instance showMethod :: Show Method where |
| 62 | + show OPTIONS = "OPTIONS" |
| 63 | + show GET = "GET" |
| 64 | + show HEAD = "HEAD" |
| 65 | + show POST = "POST" |
| 66 | + show PUT = "PUT" |
| 67 | + show DELETE = "DELETE" |
| 68 | + show TRACE = "TRACE" |
| 69 | + show CONNECT = "CONNECT" |
| 70 | + show PROPFIND = "PROPFIND" |
| 71 | + show PROPPATCH = "PROPPATCH" |
| 72 | + show MKCOL = "MKCOL" |
| 73 | + show COPY = "COPY" |
| 74 | + show MOVE = "MOVE" |
| 75 | + show LOCK = "LOCK" |
| 76 | + show UNLOCK = "UNLOCK" |
| 77 | + show PATCH = "PATCH" |
| 78 | + |
| 79 | +newtype CustomMethod = CustomMethod String |
| 80 | + |
| 81 | +runCustomMethod :: CustomMethod -> String |
| 82 | +runCustomMethod (CustomMethod m) = m |
| 83 | + |
| 84 | +derive instance genericCustomMethod :: Generic CustomMethod |
| 85 | + |
| 86 | +instance eqCustomMethod :: Eq CustomMethod where |
| 87 | + eq (CustomMethod m1) (CustomMethod m2) = m1 == m2 |
| 88 | + |
| 89 | +instance ordCustomMethod :: Ord CustomMethod where |
| 90 | + compare = gCompare |
| 91 | + |
| 92 | +instance showCustomMethod :: Show CustomMethod where |
| 93 | + show (CustomMethod m) = "CustomMethod " <> show m |
| 94 | + |
| 95 | +fromString :: String -> Either Method CustomMethod |
| 96 | +fromString s = |
| 97 | + case Str.toUpper s of |
| 98 | + "OPTIONS" -> Left OPTIONS |
| 99 | + "GET" -> Left GET |
| 100 | + "HEAD" -> Left HEAD |
| 101 | + "POST" -> Left POST |
| 102 | + "PUT" -> Left PUT |
| 103 | + "DELETE" -> Left DELETE |
| 104 | + "TRACE" -> Left TRACE |
| 105 | + "CONNECT" -> Left CONNECT |
| 106 | + "PROPFIND" -> Left PROPFIND |
| 107 | + "PROPPATCH" -> Left PROPPATCH |
| 108 | + "MKCOL" -> Left MKCOL |
| 109 | + "COPY" -> Left COPY |
| 110 | + "MOVE" -> Left MOVE |
| 111 | + "LOCK" -> Left LOCK |
| 112 | + "UNLOCK" -> Left UNLOCK |
| 113 | + m -> Right (CustomMethod m) |
| 114 | + |
| 115 | +print :: Either Method CustomMethod -> String |
| 116 | +print = either show runCustomMethod |
0 commit comments