Skip to content

Commit

Permalink
Example of how to download a release asset
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed Jul 25, 2022
1 parent ce18bbb commit 16a4bc0
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions example_gh_test.go
Expand Up @@ -2,6 +2,7 @@ package gh

import (
"fmt"
"io"
"log"
"os"
"time"
Expand Down Expand Up @@ -56,6 +57,42 @@ func ExampleRESTClient_advanced() {
fmt.Println(response)
}

// Get release asset from cli/cli repository using REST API.
func ExampleRESTClient_request() {
opts := api.ClientOptions{
Headers: map[string]string{"Accept": "application/octet-stream"},
}
client, err := RESTClient(&opts)
if err != nil {
log.Fatal(err)
}

// URL to cli/cli release v2.14.2 checksums.txt
assetURL := "repos/cli/cli/releases/assets/71589494"
resp, err := client.Request("GET", assetURL, nil)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode > 299 {
log.Fatal("server error")
}

f, err := os.CreateTemp("", "*_checksums.txt")
if err != nil {
log.Fatal(err)
}
defer f.Close()

_, err = io.Copy(f, resp.Body)
if err != nil {
log.Fatal(err)
}

fmt.Printf("Asset downloaded to %s\n", f.Name())
}

// Query tags from cli/cli repository using GQL API.
func ExampleGQLClient_simple() {
client, err := GQLClient(nil)
Expand Down

0 comments on commit 16a4bc0

Please sign in to comment.