diff options
author | adamw <adam@warski.org> | 2017-07-08 21:27:52 +0200 |
---|---|---|
committer | adamw <adam@warski.org> | 2017-07-08 21:27:52 +0200 |
commit | 0863a2af75583b976aee3171585685b2524d0874 (patch) | |
tree | ee5d08849bfb78268715743b67caf3c3c223c50f /core/src/main | |
parent | 8f03e817d315cda846136f04e85febf7022a1dc2 (diff) | |
download | sttp-0863a2af75583b976aee3171585685b2524d0874.tar.gz sttp-0863a2af75583b976aee3171585685b2524d0874.tar.bz2 sttp-0863a2af75583b976aee3171585685b2524d0874.zip |
Simple URI tests
Diffstat (limited to 'core/src/main')
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala | 32 | ||||
-rw-r--r-- | core/src/main/scala/com/softwaremill/sttp/package.scala | 4 |
2 files changed, 20 insertions, 16 deletions
diff --git a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala index abb2327..e329630 100644 --- a/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala +++ b/core/src/main/scala/com/softwaremill/sttp/UriInterpolator.scala @@ -1,6 +1,8 @@ package com.softwaremill.sttp -// from https://gist.github.com/teigen/5865923 +import java.net.URI + +// based on https://gist.github.com/teigen/5865923 object UriInterpolator { private val unreserved = { @@ -9,24 +11,22 @@ object UriInterpolator { alphanum ++ mark } - implicit class UriContext(val sc:StringContext) extends AnyVal { - def uri(args:String*) = { - val strings = sc.parts.iterator - val expressions = args.iterator - val sb = new StringBuffer(strings.next()) + def interpolate(sc: StringContext, args: String*): URI = { + val strings = sc.parts.iterator + val expressions = args.iterator + val sb = new StringBuffer(strings.next()) - while(strings.hasNext){ - for(c <- expressions.next()){ - if(unreserved(c)) - sb.append(c) - else for(b <- c.toString.getBytes("UTF-8")){ - sb.append("%") - sb.append("%02X".format(b)) - } + while(strings.hasNext){ + for(c <- expressions.next()){ + if(unreserved(c)) + sb.append(c) + else for(b <- c.toString.getBytes("UTF-8")){ + sb.append("%") + sb.append("%02X".format(b)) } - sb.append(strings.next()) } - sb.toString + sb.append(strings.next()) } + new URI(sb.toString) } } diff --git a/core/src/main/scala/com/softwaremill/sttp/package.scala b/core/src/main/scala/com/softwaremill/sttp/package.scala index 52d06f0..3321aa7 100644 --- a/core/src/main/scala/com/softwaremill/sttp/package.scala +++ b/core/src/main/scala/com/softwaremill/sttp/package.scala @@ -178,4 +178,8 @@ package object sttp { private val MultipartFormDataContentType = "multipart/form-data" private def contentTypeWithEncoding(ct: String, enc: String) = s"$ct; charset=$enc" + + implicit class UriContext(val sc: StringContext) extends AnyVal { + def uri(args:String*): URI = UriInterpolator.interpolate(sc, args: _*) + } } |