-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
134 lines (120 loc) · 4.61 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package pkg_test
import (
"context"
"fmt"
"log"
"os"
"time"
abs "github.com/microsoft/kiota-abstractions-go"
kiotaHttp "github.com/microsoft/kiota-http-go"
"github.com/octokit/go-sdk-enterprise-cloud/pkg"
auth "github.com/octokit/go-sdk-enterprise-cloud/pkg/authentication"
"github.com/octokit/go-sdk-enterprise-cloud/pkg/github"
"github.com/octokit/go-sdk-enterprise-cloud/pkg/github/octocat"
)
// ExampleApiClient_Octocat constructs an unauthenticated API client
// and makes a simple API request.
func ExampleApiClient_Octocat() {
client, err := pkg.NewApiClient(
pkg.WithUserAgent("octokit/go-sdk.example-functions"),
pkg.WithRequestTimeout(5*time.Second),
pkg.WithBaseUrl("https://api.github.com"),
pkg.WithTokenAuthentication(os.Getenv("GITHUB_TOKEN")),
)
// equally valid:
//client, err := pkg.NewApiClient()
if err != nil {
log.Fatalf("error creating client: %v", err)
}
s := "Salutations"
// create headers that accept json back; GitHub's OpenAPI definition says
// octet-stream but that's not actually what the API returns in this case
headers := abs.NewRequestHeaders()
_ = headers.TryAdd("Accept", "application/vnd.github.v3+json")
octocatRequestConfig := &abs.RequestConfiguration[octocat.OctocatRequestBuilderGetQueryParameters]{
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~~~~
// ~~~~~~==~==~~~==~==~~~~~~
// ~~~~~~==~==~==~==~~~~~~
// :~==~==~==~==~~
}
// ExampleApiClient_Octocat_withoutConvenienceConstructor shows how to
// initialize an unauthenticated API client without using the helpers
// provided in pkg/client.go, then makes a simple API request.
func ExampleApiClient_Octocat_withoutConvenienceConstructor() {
tokenProvider := auth.NewTokenProvider(
// to create an authenticated provider, uncomment the below line and pass in your token
// auth.WithTokenAuthentication("ghp_your_token"),
auth.WithUserAgent("octokit/go-sdk.example-functions"),
)
adapter, err := kiotaHttp.NewNetHttpRequestAdapter(tokenProvider)
if err != nil {
log.Fatalf("Error creating request adapter: %v", err)
}
client := github.NewApiClient(adapter)
s := "Salutations"
// create headers that accept json back; GitHub's OpenAPI definition says
// octet-stream but that's not actually what the API returns in this case
headers := abs.NewRequestHeaders()
_ = headers.TryAdd("Accept", "application/vnd.github.v3+json")
octocatRequestConfig := &abs.RequestConfiguration[octocat.OctocatRequestBuilderGetQueryParameters]{
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~~~~
// ~~~~~~==~==~~~==~==~~~~~~
// ~~~~~~==~==~==~==~~~~~~
// :~==~==~==~==~~
}