octokit / go-sdk

A generated Go SDK from GitHub's OpenAPI specification. Built on Kiota.
MIT License
64 stars 7 forks source link

Is there any documentation on how to make a request? #86

Closed onnttf closed 1 month ago

onnttf commented 2 months ago

Hello, I want to get the release list of a repo. Is there any relevant document?

Here is my code:

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)
}

var p *int32 = new(int32)
*p = 1

var p1 *int32 = new(int32)
*p1 = 1

resp, err := repos.
    NewItemItemReleasesRequestBuilder("https://github.com/octokit/go-sdk", adapter).
    Get(context.Background(), &abstractions.RequestConfiguration[repos.ItemItemReleasesRequestBuilderGetQueryParameters]{
        QueryParameters: &repos.ItemItemReleasesRequestBuilderGetQueryParameters{
            Page:     p,
            Per_page: p1,
        },
    })
if err != nil {
    panic(err)
}
fmt.Print(resp)

Here is the error message:

panic: content type application/json does not have a factory registered to be parsed

How should I modify it?

github-actions[bot] commented 2 months ago

👋 Hi! Thank you for this contribution! Just to let you know, our GitHub SDK team does a round of issue and PR reviews twice a week, every Monday and Friday! We have a process in place for prioritizing and responding to your input. Because you are a part of this community please feel free to comment, add to, or pick up any issues/PRs that are labeled with Status: Up for grabs. You & others like you are the reason all of this works! So thank you & happy coding! 🚀

kfcampbell commented 1 month ago

It's definitely a bit confusing, since our OpenAPI spec isn't always perfectly normalized and we have a whole bunch of models to use, and there's a lot of Kiota helpers generated as well.

Here's the approach I've found helpful (and maybe we should add this to the docs somewhere):


        client, err := pkg.NewApiClient(
        pkg.WithUserAgent("my-user-agent"),
        pkg.WithRequestTimeout(5*time.Second),
        pkg.WithBaseUrl("https://api.github.com"),
        pkg.WithTokenAuthentication(os.Getenv("GITHUB_TOKEN")),
    )

    if err != nil {
        log.Fatalf("error creating client: %v", err)
    }

    releases, err := client.Repos().ByOwnerId("octokit").ByRepoId("go-sdk").Releases().Get(context.Background(), nil)
    if err != nil {
        log.Fatalf("error getting releases: %v", err)
    }
    log.Printf("Releases:\n")
    for _, release := range releases {
        log.Printf("Tag: %v\n", *release.GetTagName())
        log.Printf("Notes: %v\n", *release.GetBody())
    }

I hope that helps!

onnttf commented 1 month ago

Thank you very much for your help, it solved my problem.