Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Forked from maxandersen/fetch.java
Last active August 22, 2022 12:36
Show Gist options
  • Save gastaldi/3ad56b07dcadfd7c055272e9956ba4d1 to your computer and use it in GitHub Desktop.
Save gastaldi/3ad56b07dcadfd7c055272e9956ba4d1 to your computer and use it in GitHub Desktop.
///usr/bin/env jbang "$0" "$@" ; exit $?
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import static java.lang.System.out;
public class fetch {
private static final String URL = "https://maven.pkg.github.com/gsmet/github-action-playground/io/github/gsmet/github-action-playground/1.0.0-SNAPSHOT/github-action-playground-1.0.0-20220722.093742-12.jar";
public static void main(String... args) throws IOException, InterruptedException {
if (args.length != 1) {
out.println("Usage: fetch <github PAT token with read-packages>");
return;
}
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(URL))
.header("Authorization",
"Basic " + Base64.getEncoder().encodeToString((args[0] + ":").getBytes(StandardCharsets.UTF_8)))
.build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println("java: " + response);
}
}
@gastaldi
Copy link
Author

///usr/bin/env jbang "$0" "$@" ; exit $?

import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

import static java.lang.System.out;

public class fetch {

    private static final String URL = "https://maven.pkg.github.com/gsmet/github-action-playground/io/github/gsmet/github-action-playground/1.0.0-SNAPSHOT/github-action-playground-1.0.0-20220722.093742-12.jar";

    public static void main(String... args) throws IOException, InterruptedException {
        if (args.length != 1) {
            out.println("Usage: fetch <github PAT token with read-packages>");
            return;
        }
        HttpClient httpClient = HttpClient.newHttpClient();

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(URL))
                .header("Authorization",
                        "Basic " + Base64.getEncoder().encodeToString((args[0] + ":").getBytes(StandardCharsets.UTF_8)))
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        System.out.println("java: " + response);

    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment