Last active
February 3, 2026 20:20
-
-
Save dacr/c6ff56a0ec06227812e85380fc75b863 to your computer and use it in GitHub Desktop.
List a user github gists using sttp for client http API. / published by https://github.com/dacr/code-examples-manager #e7bea979-ad9e-4b86-b3fb-2182d07b3032/cc61e04f9dd1481c3e399e513a9c022ccc628c0e
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
| //#!/usr/bin/env amm | |
| // summary : List a user github gists using sttp for client http API. | |
| // keywords : scala, sttp, circe, gists-api | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : e7bea979-ad9e-4b86-b3fb-2182d07b3032 | |
| // created-on : 2020-05-31T19:54:52Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| //> 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 org.scalatest::scalatest:3.0.6 | |
| import com.softwaremill.sttp.quick._ | |
| import com.softwaremill.sttp.circe._ | |
| 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 Gist( | |
| id:String, | |
| description:String, | |
| html_url:String, | |
| public:Boolean, | |
| ) | |
| val user = "dacr" | |
| val token = scala.util.Properties.envOrElse("GIST_TOKEN", "invalid-token") | |
| val query = uri"https://api.github.com/users/$user/gists?page=1&per_page=1000" | |
| val response = { | |
| sttp | |
| .get(query) | |
| .header("Authorization", s"token $token") | |
| .response(asJson[Array[Gist]]).send() | |
| } | |
| val next = response.header("Link") // it provides the link for the next & last page :) | |
| val gists = response.body.right.get.right.get | |
| 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