1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package com.softwaremill
import java.io.{File, InputStream}
import java.nio.ByteBuffer
import java.nio.file.Path
import com.softwaremill.sttp.model._
import scala.annotation.implicitNotFound
import scala.language.higherKinds
import scala.collection.immutable.Seq
package object sttp {
type Id[X] = X
type Empty[X] = None.type
type PartialRequest[T, +S] = RequestT[Empty, T, S]
type Request[T, +S] = RequestT[Id, T, S]
@implicitNotFound(
"This is a partial request, the method & url are not specified. Use " +
".get(...), .post(...) etc. to obtain a non-partial request.")
private[sttp] type IsIdInRequest[U[_]] = U[Unit] =:= Id[Unit]
/**
* Provide an implicit value of this type to serialize arbitrary classes into a request body.
* Handlers might also provide special logic for serializer instances which they define (e.g. to handle streaming).
*/
type BodySerializer[B] = B => BasicRequestBody
// constants
private[sttp] val ContentTypeHeader = "Content-Type"
private[sttp] val ContentLengthHeader = "Content-Length"
private[sttp] val SetCookieHeader = "Set-Cookie"
private[sttp] val CookieHeader = "Cookie"
private[sttp] val AuthorizationHeader = "Authorization"
private[sttp] val ProxyAuthorizationHeader = "Proxy-Authorization"
private[sttp] val AcceptEncodingHeader = "Accept-Encoding"
private[sttp] val ContentEncodingHeader = "Content-Encoding"
private[sttp] val Utf8 = "utf-8"
private[sttp] val ApplicationOctetStreamContentType =
"application/octet-stream"
private[sttp] val ApplicationFormContentType =
"application/x-www-form-urlencoded"
private[sttp] val TextPlainContentType = "text/plain"
private[sttp] val MultipartFormDataContentType = "multipart/form-data"
// entry points
/**
* An empty request with no headers.
*/
val emptyRequest: RequestT[Empty, String, Nothing] =
RequestT[Empty, String, Nothing](None, None, NoBody, Vector(), asString)
/**
* A starting request, with the following modifications comparing to
* `emptyRequest`:
*
* - `Accept-Encoding` set to `gzip, deflate` (handled automatically by the
* library)
*/
val sttp: RequestT[Empty, String, Nothing] =
emptyRequest.acceptEncoding("gzip, deflate")
// response specifications
def ignore: ResponseAs[Unit, Nothing] = IgnoreResponse
/**
* Uses `utf-8` encoding.
*/
def asString: ResponseAs[String, Nothing] = asString(Utf8)
def asString(encoding: String): ResponseAs[String, Nothing] =
ResponseAsString(encoding)
def asByteArray: ResponseAs[Array[Byte], Nothing] =
ResponseAsByteArray
/**
* Uses `utf-8` encoding.
*/
def asParams: ResponseAs[Seq[(String, String)], Nothing] =
asParams(Utf8)
def asParams(encoding: String): ResponseAs[Seq[(String, String)], Nothing] =
ResponseAsParams(encoding)
def asStream[S]: ResponseAs[S, S] = ResponseAsStream[S, S]()
// multi part factory methods
/**
* Content type will be set to `text/plain` with `utf-8` encoding, can be
* overridden later using the `contentType` method.
*/
def multiPart(name: String, data: String): MultiPart =
MultiPart(name,
StringBody(data, Utf8),
contentType =
Some(contentTypeWithEncoding(TextPlainContentType, Utf8)))
/**
* Content type will be set to `text/plain` with `utf-8` encoding, can be
* overridden later using the `contentType` method.
*/
def multiPart(name: String, data: String, encoding: String): MultiPart =
MultiPart(name,
StringBody(data, encoding),
contentType =
Some(contentTypeWithEncoding(TextPlainContentType, Utf8)))
/**
* Content type will be set to `application/octet-stream`, can be overridden
* later using the `contentType` method.
*/
def multiPart(name: String, data: Array[Byte]): MultiPart =
MultiPart(name,
ByteArrayBody(data),
contentType = Some(ApplicationOctetStreamContentType))
/**
* Content type will be set to `application/octet-stream`, can be overridden
* later using the `contentType` method.
*/
def multiPart(name: String, data: ByteBuffer): MultiPart =
MultiPart(name,
ByteBufferBody(data),
contentType = Some(ApplicationOctetStreamContentType))
/**
* Content type will be set to `application/octet-stream`, can be overridden
* later using the `contentType` method.
*/
def multiPart(name: String, data: InputStream): MultiPart =
MultiPart(name,
InputStreamBody(data),
contentType = Some(ApplicationOctetStreamContentType))
/**
* Content type will be set to `application/octet-stream`, can be overridden
* later using the `contentType` method.
*/
def multiPart(name: String, data: File): MultiPart =
multiPart(name, data.toPath)
/**
* Content type will be set to `application/octet-stream`, can be overridden
* later using the `contentType` method.
*/
def multiPart(name: String, data: Path): MultiPart =
MultiPart(name,
PathBody(data),
fileName = Some(data.getFileName.toString),
contentType = Some(ApplicationOctetStreamContentType))
// util
private[sttp] def contentTypeWithEncoding(ct: String, enc: String) =
s"$ct; charset=$enc"
// uri interpolator
implicit class UriContext(val sc: StringContext) extends AnyVal {
def uri(args: Any*): Uri = UriInterpolator.interpolate(sc, args: _*)
}
}
|