Browse Source

initial commit

main
Riley Apeldoorn 7 months ago
commit
fb74967257
  1. 1
      .gitignore
  2. 1
      README.md
  3. 76
      src/lib.scala

1
.gitignore

@ -0,0 +1 @@
target/

1
README.md

@ -0,0 +1 @@
# Scala utils for making stuff less sucky

76
src/lib.scala

@ -0,0 +1,76 @@
import scala.util.{ Try, Success, Failure }
import java.net.{ URL, HttpURLConnection, URLConnection }
import java.io.{ BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter }
class Response ()
case class HeaderKey (str: String)
case class HeaderValue (str: String)
trait HttpMethod {
def asMethodString (): String
}
case object Put extends HttpMethod {
def asMethodString (): String = "PUT"
}
case object Get extends HttpMethod {
def asMethodString (): String = "GET"
}
case object Post extends HttpMethod {
def asMethodString (): String = "POST"
}
class BodyReader (conn: HttpURLConnection) {
val stream = conn.getInputStream
def read (): Try[String] = Try {
val remote = new BufferedReader(new InputStreamReader(stream))
Stream.continually(remote.readLine)
.takeWhile(_ != null)
.force
.mkString("\n")
}
}
class BodyWriter (conn: HttpURLConnection) {
val stream = conn.getOutputStream
def write (data: String): Try[Unit] = Try {
val remote = new OutputStreamWriter(stream, "utf-8")
remote.write(data)
remote.flush
remote.close
}
}
class Yeetable (request: Request, method: HttpMethod) {
def yeet (body: String): Try[String] = {
request.conn.setRequestMethod(method.asMethodString)
Failure (new Exception("No yeet"))
}
}
class Request (url: URL) {
val conn = url.openConnection().asInstanceOf[HttpURLConnection]
conn.setDoOutput(true)
def addHeader (key: HeaderKey, value: HeaderValue): Request = {
conn.setRequestProperty(key.toString, value.toString)
this
}
def get (): Yeetable = new Yeetable (this, Get)
def put (): Yeetable = new Yeetable (this, Put)
def post (): Yeetable = new Yeetable (this, Post)
}
Loading…
Cancel
Save