Skip to content

Commit

Permalink
Create example test for unauthenticated requests (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfcampbell committed Dec 12, 2023
1 parent e82a9dd commit 86af5df
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ An "alpha" version of a generated Go SDK from [GitHub's OpenAPI spec](https://gi

## How do I use it?

For now, see instructions in [this gist](https://gist.github.com/kfcampbell/0214851ce0ee9844f957cdbfb06910d4).
See example client instantiations and requests in [example_test.go](pkg/authentication/example_test.go).

⚠️ **Note**: This SDK is not yet stable. Breaking changes may occur at any time.

Expand Down
70 changes: 70 additions & 0 deletions pkg/authentication/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package authentication_test

import (
"context"
"fmt"
"log"

abstractions "github.com/microsoft/kiota-abstractions-go"
http "github.com/microsoft/kiota-http-go"
auth "github.com/octokit/go-sdk/pkg/authentication"
"github.com/octokit/go-sdk/pkg/github"
"github.com/octokit/go-sdk/pkg/github/octocat"
)

func ExampleUnauthenticatedRequest() {
tokenProvider := auth.NewTokenProvider(
// to create an authenticated provider, uncomment the below line and pass in your token
// auth.WithAuthorizationToken("ghp_your_token"),
auth.WithUserAgent("octokit/go-sdk.example-functions"),
)
adapter, err := http.NewNetHttpRequestAdapter(tokenProvider)
if err != nil {
log.Fatalf("Error creating request adapter: %v", err)
}

client := github.NewApiClient(adapter)

// unauthenticated request
s := "Salutations"

// create headers that accept json back; our spec says octet-stream
// but that's not actually what the API returns in this case
headers := abstractions.NewRequestHeaders()
_ = headers.TryAdd("Accept", "application/vnd.github.v3+json")

octocatRequestConfig := &octocat.OctocatRequestBuilderGetRequestConfiguration{
QueryParameters: &octocat.OctocatRequestBuilderGetQueryParameters{
S: &s,
},
Headers: headers,
}
cat, err := client.Octocat().Get(context.Background(), octocatRequestConfig)
if err != nil {
log.Fatalf("error getting octocat: %v", err)
}
fmt.Printf("%v\n", string(cat))
// Output:
// MMM. .MMM
// MMMMMMMMMMMMMMMMMMM
// MMMMMMMMMMMMMMMMMMM _____________
// MMMMMMMMMMMMMMMMMMMMM | |
// MMMMMMMMMMMMMMMMMMMMMMM | Salutations |
// MMMMMMMMMMMMMMMMMMMMMMMM |_ _________|
// MMMM::- -:::::::- -::MMMM |/
// MM~:~ 00~:::::~ 00~:~MM
// .. MMMMM::.00:::+:::.00::MMMMM ..
// .MM::::: ._. :::::MM.
// MMMM;:::::;MMMM
// -MM MMMMMMM
// ^ M+ MMMMMMMMM
// MMMMMMM MM MM MM
// MM MM MM MM
// MM MM MM MM
// .~~MM~MM~MM~MM~~.
// ~~~~MM:~MM~~~MM~:MM~~~~
// ~~~~~~==~==~~~==~==~~~~~~
// ~~~~~~==~==~==~==~~~~~~
// :~==~==~==~==~~

}

0 comments on commit 86af5df

Please sign in to comment.