Last active
October 9, 2024 18:08
-
-
Save dacr/1bcb1a15c32679277f26670ab24f0879 to your computer and use it in GitHub Desktop.
List a user github gists using sttp for client http and circe for json processing with authentication. / published by https://github.com/dacr/code-examples-manager #74347314-f8f4-402e-88ff-0765b6365135/18e3cd828a32da4a3ccd6ef6d3d29c2a53938d85
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 : List a user github gists using sttp for client http and circe for json processing with authentication. | |
// keywords : scala, sttp, circe, 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 : 74347314-f8f4-402e-88ff-0765b6365135 | |
// 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::circe:1.5.17 | |
//> using dep io.circe::circe-generic:0.11.1 // Keep in sync with sttp.circe used circe release | |
////> using dep io.circe::circe-generic-extras:0.11.1 // Keep in sync with sttp.circe used circe release | |
//> using dep org.scalatest::scalatest:3.0.6 | |
import com.softwaremill.sttp.Uri | |
import com.softwaremill.sttp.quick._ | |
import com.softwaremill.sttp.circe._ | |
//import io.circe.generic.extras._ | |
import io.circe.generic.auto._ | |
/* 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 GistFile( | |
filename:String, | |
`type`:String, | |
language:String, | |
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 Token(value:String) | |
def getUserGists(user:String)(implicit token:Token):Stream[Gist] = { | |
val nextLinkRE=""".*<([^>]+)>; rel="next".*""".r | |
def worker(nextQuery:Option[Uri], currentRemaining:Iterable[Gist]):Stream[Gist] = { | |
(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.value}") | |
.response(asJson[Array[Gist]]) | |
.send() | |
} | |
val gists = response.body.right.get.right.get.toList | |
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, gists) | |
} | |
} | |
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]= ??? | |
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("private gists :") | |
gists.filterNot(_.public).foreach{ println } | |
println("private gists count : "+gists.filterNot(_.public).size) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment