aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/com/softwaremill/sttp/package.scala
blob: f9ee9c61e8c8a9b073435703cd433275c2cf6516 (plain) (blame)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
package com.softwaremill

import java.io.{File, InputStream}
import java.net.{URI, URLEncoder}
import java.nio.ByteBuffer
import java.nio.file.Path
import java.util.Base64

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

  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]()

  /**
    * Use the factory methods `multiPart` to conveniently create instances of
    * this class. A part can be then further customised using `fileName`,
    * `contentType` and `header` methods.
    */
  case class MultiPart(name: String,
                       data: BasicRequestBody,
                       fileName: Option[String] = None,
                       contentType: Option[String] = None,
                       additionalHeaders: Map[String, String] = Map()) {
    def fileName(v: String): MultiPart = copy(fileName = Some(v))
    def contentType(v: String): MultiPart = copy(contentType = Some(v))
    def header(k: String, v: String): MultiPart =
      copy(additionalHeaders = additionalHeaders + (k -> v))
  }

  /**
    * 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))

  /**
    * @tparam U Specifies if the method & uri are specified. By default can be
    *           either:
    *           * `Empty`, which is a type constructor which always resolves to
    *           `None`. This type of request is aliased to `PartialRequest`:
    *           there's no method and uri specified, and the request cannot be
    *           sent.
    *           * `Id`, which is an identity type constructor. This type of
    *           request is aliased to `Request`: the method and uri are
    *           specified, and the request can be sent.
    */
  case class RequestT[U[_], T, +S](
      method: U[Method],
      uri: U[URI],
      body: RequestBody[S],
      headers: Seq[(String, String)],
      responseAs: ResponseAs[T, S]
  ) {
    def get(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.GET)
    def head(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.HEAD)
    def post(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.POST)
    def put(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.PUT)
    def delete(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.DELETE)
    def options(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.OPTIONS)
    def patch(uri: URI): Request[T, S] =
      this.copy[Id, T, S](uri = uri, method = Method.PATCH)

    def contentType(ct: String): RequestT[U, T, S] =
      header(ContentTypeHeader, ct, replaceExisting = true)
    def contentType(ct: String, encoding: String): RequestT[U, T, S] =
      header(ContentTypeHeader,
             contentTypeWithEncoding(ct, encoding),
             replaceExisting = true)
    def header(k: String,
               v: String,
               replaceExisting: Boolean = false): RequestT[U, T, S] = {
      val current =
        if (replaceExisting)
          headers.filterNot(_._1.equalsIgnoreCase(k))
        else headers
      this.copy(headers = current :+ (k -> v))
    }
    def headers(hs: Map[String, String]): RequestT[U, T, S] =
      this.copy(headers = headers ++ hs.toSeq)
    def headers(hs: (String, String)*): RequestT[U, T, S] =
      this.copy(headers = headers ++ hs)
    def cookie(nv: (String, String)): RequestT[U, T, S] = cookies(nv)
    def cookie(n: String, v: String): RequestT[U, T, S] = cookies((n, v))
    def cookies(r: Response[_]): RequestT[U, T, S] =
      cookies(r.cookies.map(c => (c.name, c.value)): _*)
    def cookies(cs: Seq[Cookie]): RequestT[U, T, S] =
      cookies(cs.map(c => (c.name, c.value)): _*)
    def cookies(nvs: (String, String)*): RequestT[U, T, S] =
      header(CookieHeader, nvs.map(p => p._1 + "=" + p._2).mkString("; "))
    def auth: SpecifyAuthScheme[U, T, S] =
      new SpecifyAuthScheme[U, T, S](AuthorizationHeader, this)
    def proxyAuth: SpecifyAuthScheme[U, T, S] =
      new SpecifyAuthScheme[U, T, S](ProxyAuthorizationHeader, this)
    def acceptEncoding(encoding: String): RequestT[U, T, S] =
      header(AcceptEncodingHeader, encoding)

    /**
      * Uses the `utf-8` encoding.
      * If content type is not yet specified, will be set to `text/plain`
      * with `utf-8` encoding.
      */
    def body(b: String): RequestT[U, T, S] = body(b, Utf8)

    /**
      * If content type is not yet specified, will be set to `text/plain`
      * with the given encoding.
      */
    def body(b: String, encoding: String): RequestT[U, T, S] =
      setContentTypeIfMissing(
        contentTypeWithEncoding(TextPlainContentType, encoding))
        .copy(body = StringBody(b, encoding))

    /**
      * If content type is not yet specified, will be set to
      * `application/octet-stream`.
      */
    def body(b: Array[Byte]): RequestT[U, T, S] =
      setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(
        body = ByteArrayBody(b))

    /**
      * If content type is not yet specified, will be set to
      * `application/octet-stream`.
      */
    def body(b: ByteBuffer): RequestT[U, T, S] =
      setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(
        body = ByteBufferBody(b))

    /**
      * If content type is not yet specified, will be set to
      * `application/octet-stream`.
      */
    def body(b: InputStream): RequestT[U, T, S] =
      setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(
        body = InputStreamBody(b))

    /**
      * If content type is not yet specified, will be set to
      * `application/octet-stream`.
      */
    def body(b: File): RequestT[U, T, S] = body(b.toPath)

    /**
      * If content type is not yet specified, will be set to
      * `application/octet-stream`.
      */
    def body(b: Path): RequestT[U, T, S] =
      setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(
        body = PathBody(b))

    /**
      * Encodes the given parameters as form data using `utf-8`.
      * If content type is not yet specified, will be set to
      * `application/x-www-form-urlencoded`.
      */
    def body(fs: Map[String, String]): RequestT[U, T, S] =
      formDataBody(fs.toList, Utf8)

    /**
      * Encodes the given parameters as form data.
      * If content type is not yet specified, will be set to
      * `application/x-www-form-urlencoded`.
      */
    def body(fs: Map[String, String], encoding: String): RequestT[U, T, S] =
      formDataBody(fs.toList, encoding)

    /**
      * Encodes the given parameters as form data using `utf-8`.
      * If content type is not yet specified, will be set to
      * `application/x-www-form-urlencoded`.
      */
    def body(fs: (String, String)*): RequestT[U, T, S] =
      formDataBody(fs.toList, Utf8)

    /**
      * Encodes the given parameters as form data.
      * If content type is not yet specified, will be set to
      * `application/x-www-form-urlencoded`.
      */
    def body(fs: Seq[(String, String)], encoding: String): RequestT[U, T, S] =
      formDataBody(fs, encoding)

    /**
      * If content type is not yet specified, will be set to
      * `application/octet-stream`.
      */
    def body[B: BodySerializer](b: B): RequestT[U, T, S] =
      setContentTypeIfMissing(ApplicationOctetStreamContentType).copy(
        body = SerializableBody(implicitly[BodySerializer[B]], b))

    //def multipartData(parts: MultiPart*): RequestTemplate[U] = ???

    def streamBody[S2 >: S](b: S2): RequestT[U, T, S2] =
      this.copy[U, T, S2](body = StreamBody(b))

    /**
      * What's the target type to which the response body should be read.
      * Needs to be specified upfront so that the response is always consumed
      * and hence there are no requirements on client code to consume it. An
      * exception to this are streaming responses, which need to fully
      * consumed by the client if such a response type is requested.
      */
    def response[T2, S2 >: S](ra: ResponseAs[T2, S2]): RequestT[U, T2, S2] =
      this.copy(responseAs = ra)

    def mapResponse[T2](f: T => T2): RequestT[U, T2, S] =
      this.copy(responseAs = responseAs.map(f))

    def send[R[_]]()(implicit handler: SttpHandler[R, S],
                     isIdInRequest: IsIdInRequest[U]): R[Response[T]] = {
      // we could avoid the asInstanceOf by creating an artificial copy
      // changing the method & url fields using `isIdInRequest`, but that
      // would be only to satisfy the type checker, and a needless copy at
      // runtime.
      handler.send(this.asInstanceOf[RequestT[Id, T, S]])
    }

    private def hasContentType: Boolean =
      headers.exists(_._1.toLowerCase.contains(ContentTypeHeader))
    private def setContentTypeIfMissing(ct: String): RequestT[U, T, S] =
      if (hasContentType) this else contentType(ct)

    private def formDataBody(fs: Seq[(String, String)],
                             encoding: String): RequestT[U, T, S] = {
      val b = fs
        .map(
          p =>
            URLEncoder.encode(p._1, encoding) + "=" + URLEncoder
              .encode(p._2, encoding))
        .mkString("&")
      setContentTypeIfMissing(ApplicationFormContentType).copy(
        body = StringBody(b, encoding))
    }
  }

  class SpecifyAuthScheme[U[_], T, +S](hn: String, rt: RequestT[U, T, S]) {
    def basic(user: String, password: String): RequestT[U, T, S] = {
      val c = new String(
        Base64.getEncoder.encode(s"$user:$password".getBytes(Utf8)),
        Utf8)
      rt.header(hn, s"Basic $c")
    }

    def bearer(token: String): RequestT[U, T, S] =
      rt.header(hn, s"Bearer $token")
  }

  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 type IsIdInRequest[U[_]] = U[Unit] =:= Id[Unit]

  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 val Utf8 = "utf-8"

  private val ApplicationOctetStreamContentType = "application/octet-stream"
  private val ApplicationFormContentType = "application/x-www-form-urlencoded"
  private val TextPlainContentType = "text/plain"
  private val MultipartFormDataContentType = "multipart/form-data"

  /**
    * 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")

  private def contentTypeWithEncoding(ct: String, enc: String) =
    s"$ct; charset=$enc"

  implicit class UriContext(val sc: StringContext) extends AnyVal {
    def uri(args: Any*): URI = UriInterpolator.interpolate(sc, args: _*)
  }
}