Last active
October 9, 2024 18:08
-
-
Save dacr/4d5b1137a6d023d48afbfa15d93aa9aa to your computer and use it in GitHub Desktop.
Various operations on gists using sttp for client http and json4s for json processing with authentication. / published by https://github.com/dacr/code-examples-manager #46fa665c-0b3f-4088-8d32-b583296947cb/91fffcc61ca9fdcd3c380e9f6c1740a02327e102
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
// summary : Various operations on gists using sttp for client http and json4s for json processing with authentication. | |
// keywords : scala, sttp, json4s, json, gists-api | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 46fa665c-0b3f-4088-8d32-b583296947cb | |
// created-on : 2020-05-31T19:54:52Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// execution : scala 2.12 ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' | |
//> using scala 2.12.20 | |
//> using dep com.softwaremill.sttp::core:1.5.17 | |
//> using dep com.softwaremill.sttp::json4s:1.5.17 | |
//> using dep com.softwaremill.sttp::okhttp-backend:1.5.17 | |
//> using dep org.json4s::json4s-native:3.6.5 | |
//> using dep org.scalatest::scalatest:3.0.6 | |
import com.softwaremill.sttp.Uri | |
import com.softwaremill.sttp.json4s._ | |
import com.softwaremill.sttp._ | |
import scala.util.{Either, Left, Right} | |
import org.json4s.JValue | |
implicit val sttpBackend = com.softwaremill.sttp.okhttp.OkHttpSyncBackend() | |
implicit val serialization = org.json4s.native.Serialization | |
implicit val formats = org.json4s.DefaultFormats | |
/* Get an authorized access to github gist API : | |
- list authorizations : curl --user "dacr" https://api.github.com/authorizations | |
- create token : curl https://api.github.com/authorizations --user "dacr" --data '{"scopes":["gist"],"note":"testoauth"}' | |
- setup GIST_TOKEN environment variable with the previously generated token | |
- get token : not possible of course | |
- interesting link : https://gist.github.com/joyrexus/85bf6b02979d8a7b0308#oauth | |
*/ | |
case class GistFileInfo( | |
filename: String, | |
`type`: String, | |
language: String, | |
raw_url: String, | |
size: Int, | |
) | |
case class GistInfo( | |
id: String, | |
description: String, | |
html_url: String, | |
public: Boolean, | |
files: Map[String, GistFileInfo], // Does not work by default | |
) | |
case class GistFile( | |
filename: String, | |
`type`: String, | |
language: String, | |
raw_url: String, | |
size: Int, | |
truncated: Boolean, | |
content: String, | |
) | |
case class Gist( | |
id: String, | |
description: String, | |
html_url: String, | |
public: Boolean, | |
files: Map[String, GistFile], // Does not work by default | |
) | |
case class GistFileSpec( | |
filename: String, | |
content: String | |
) | |
case class GistSpec( | |
description: String, | |
public: Boolean, | |
files: Map[String, GistFileSpec], | |
) | |
case class Token(value: String) { | |
override def toString: String = value | |
} | |
def getUserGists(user: String)(implicit token: Token): Stream[GistInfo] = { | |
val nextLinkRE =""".*<([^>]+)>; rel="next".*""".r | |
def worker(nextQuery: Option[Uri], currentRemaining: Iterable[GistInfo]): Stream[GistInfo] = { | |
(nextQuery, currentRemaining) match { | |
case (None, Nil) => Stream.empty | |
case (_, head :: tail) => head #:: worker(nextQuery, tail) | |
case (Some(query), Nil) => | |
val response = { | |
sttp | |
.get(query) | |
.header("Authorization", s"token $token") | |
.response(asJson[Array[GistInfo]]) | |
.send() | |
} | |
response.body match { | |
case Left(message) => | |
System.err.println(s"List gists - Something wrong has happened : $message") | |
Stream.empty | |
case Right(gistsArray) => | |
val next = response.header("Link") // it provides the link for the next & last page :) | |
val newNextQuery = next.collect { case nextLinkRE(uri) => uri"$uri" } | |
worker(newNextQuery, gistsArray.toList) | |
} | |
} | |
} | |
val count = 10 | |
val startQuery = uri"https://api.github.com/users/$user/gists?page=1&per_page=$count" | |
worker(Some(startQuery), Nil) | |
} | |
def getGist(id: String)(implicit token: Token): Option[Gist] = { | |
val query = uri"https://api.github.com/gists/$id" | |
val response = { | |
sttp | |
.get(query) | |
.header("Authorization", s"token $token") | |
.response(asJson[Gist]) | |
.send() | |
} | |
response.body match { | |
case Left(message) => | |
System.err.println(s"Get gist - Something wrong has happened : $message") | |
None | |
case Right(gist) => | |
Some(gist) | |
} | |
} | |
def addGist(gist: GistSpec)(implicit token: Token): Option[String] = { | |
val query = uri"https://api.github.com/gists" | |
val response = { | |
sttp | |
.body(gist) | |
.post(query) | |
.header("Authorization", s"token $token") | |
.response(asJson[JValue]) | |
.send() | |
} | |
response.body match { | |
case Left(message) => | |
System.err.println(s"Add gist - Something wrong has happened : $message") | |
None | |
case Right(jvalue) => | |
(jvalue \ "id").extractOpt[String] | |
} | |
} | |
def updateGist(id:String, gist: GistSpec)(implicit token: Token): Option[String] = { | |
val query = uri"https://api.github.com/gists/$id" | |
val response = { | |
sttp | |
.body(gist) | |
.patch(query) | |
.header("Authorization", s"token $token") | |
.response(asJson[JValue]) | |
.send() | |
} | |
response.body match { | |
case Left(message) => | |
System.err.println(s"Update gist - Something wrong has happened : $message") | |
None | |
case Right(jvalue) => | |
(jvalue \ "id").extractOpt[String] | |
} | |
} | |
implicit val token = Token(scala.util.Properties.envOrElse("GIST_TOKEN", "invalid-token")) | |
val user = "dacr" | |
val gists = getUserGists(user) | |
println("------ gists count : " + gists.size) | |
println("------ public gists :") | |
gists.filter(_.public).foreach(println) | |
println("------ private gists :") | |
gists.filterNot(_.public).foreach(println) | |
println("------ private gists count : " + gists.filterNot(_.public).size) | |
val gist = getGist("6057315") | |
println(s"Gotten gist :\n$gist") | |
val originalGist = GistSpec( | |
"essai", | |
true, | |
Map( | |
"file" -> GistFileSpec("file", "some content") | |
) | |
) | |
val idOpt = addGist( originalGist) | |
val updatedGist = GistSpec( | |
"essai#2", | |
true, | |
Map( | |
"file" -> GistFileSpec("file", "some new content") | |
) | |
) | |
idOpt.foreach{ id => | |
val result = updateGist(id, updatedGist) | |
System.out.println(s"Update result : $result") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment