Last active
December 2, 2019 23:02
-
-
Save nafg/d1d6a8b103b73b3b6a640ec86c11c372 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.{Failure, Success} | |
import $ivy.`io.circe::circe-core:0.12.3` | |
import $ivy.`io.circe::circe-generic:0.12.3` | |
import $ivy.`io.circe::circe-parser:0.12.3` | |
import io.circe.{Decoder, Encoder} | |
import io.circe.generic.auto._ | |
import io.circe.syntax._ | |
import io.circe.parser | |
import requests.BaseSession | |
case class Response[A](response: A) | |
case class Group(id: Long, name: String) | |
case class ProtoGroup(name: String, | |
description: Option[String] = None, | |
image_url: Option[String] = None, | |
share: Boolean = false) | |
object Client { | |
val baseUrl = "https://api.groupme.com/v3/" | |
val sess = requests.Session(headers = BaseSession.defaultHeaders ++ Map("X-Access-Token" -> sys.env("GROUPME_TOKEN"))) | |
implicit class ResponseExtensionMethods(private val self: requests.Response) extends AnyVal { | |
def result = | |
if (self.is2xx) | |
Success(self.text()) | |
else | |
Failure(new RuntimeException(self.statusCode + " " + self.statusMessage)) | |
def void = result.map(_ => ()) | |
def decode[R: Decoder] = result.flatMap(parser.decode[R](_).toTry) | |
} | |
def get[R: Decoder](path: String, args: (String, String)*) = | |
sess.get(baseUrl + path, params = args).decode[R] | |
def post[P: Encoder, R: Decoder](path: String)(data: P) = | |
sess.post(baseUrl + path, data = data.asJson.noSpaces).decode[R] | |
def postU[P: Encoder](path: String)(data: P) = sess.post(baseUrl + path, data = data.asJson.noSpaces).void | |
} | |
object Groups { | |
def index = Client.get[Response[List[Group]]]("groups") | |
def create = Client.post[ProtoGroup, Response[Group]]("groups") _ | |
def delete(group: Group) = Client.postU(s"groups/${group.id}/destroy")(()) | |
} | |
println(Groups.index) | |
for { | |
test123 <- Groups.create(ProtoGroup("test123")) | |
_ = println("Created " + test123) | |
_ <- Groups.delete(test123.response) | |
_ = println("Deleted " + test123) | |
} yield () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment