如何将 go-vcr 与 githubv4 一起使用? - 获取httpClient传输

发布于 2025-01-10 11:08:10 字数 1060 浏览 0 评论 0原文

查看 go-vcr 的设置,

// Start our recorder
    r, err := recorder.New("fixtures/etcd")
    if err != nil {
        log.Fatal(err)
    }
    defer r.Stop() // Make sure recorder is stopped once done with it

    // Create an etcd configuration using our transport
    cfg := client.Config{
        Endpoints:               []string{"http://127.0.0.1:2379"},
        HeaderTimeoutPerRequest: time.Second,
        Transport:               r, // Inject as transport!
    }

尝试使用 githubv4 库来使用此库似乎需要一种处理 Oauth 的方法,但

import "golang.org/x/oauth2"

func main() {
    src := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
    )
    httpClient := oauth2.NewClient(context.Background(), src)

    client := githubv4.NewClient(httpClient)
    // Use client...
}

我不确定如何将记录器“r”放入 oauth2 客户端。如果可能的话。

有人在这方面取得过成功吗?我尝试过使用“r”记录器传递 httpClient,但最终结果为 401 - 看起来这个默认客户端无法执行 Oauth dance。

我想使用 GraphQL API,但如果更容易的话可以回退到 REST API,但我只是想确保这实际上不可能。还有其他人在这方面取得过成功吗?

Looking at the set up for go-vcr

// Start our recorder
    r, err := recorder.New("fixtures/etcd")
    if err != nil {
        log.Fatal(err)
    }
    defer r.Stop() // Make sure recorder is stopped once done with it

    // Create an etcd configuration using our transport
    cfg := client.Config{
        Endpoints:               []string{"http://127.0.0.1:2379"},
        HeaderTimeoutPerRequest: time.Second,
        Transport:               r, // Inject as transport!
    }

Attempting to use this library using the githubv4 library seems at though it needs a way to handle Oauth

import "golang.org/x/oauth2"

func main() {
    src := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: os.Getenv("GITHUB_TOKEN")},
    )
    httpClient := oauth2.NewClient(context.Background(), src)

    client := githubv4.NewClient(httpClient)
    // Use client...
}

I'm not sure how to get the recorder 'r' into the oauth2 client. If at all possible.

Has anyone been successful with this? I've tried passing in a httpClient with the 'r' recorder but it ends up as a 401 - looks like this default client can't do the Oauth dance.

I'd like to use the GraphQL API but can fall back to the REST API if is is easier but I just want to make sure this isn't really possible. Has anyone else been successful with this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

歌入人心 2025-01-17 11:08:10

这个问题为我解决了这个问题。
https://github.com/dnaeon/go-vcr/issues/59

下面的例子

package example_test

import (
    "context"
    "github.com/dnaeon/go-vcr/cassette"
    "github.com/dnaeon/go-vcr/recorder"
    "github.com/google/go-github/v33/github"
    "github.com/stretchr/testify/require"
    "golang.org/x/oauth2"
    "net/http"
    "path"
    "testing"
)

func TestGithub(t *testing.T) {
    //custom http.Transport, since github uses oauth2 authentication
    ts := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: "YOUR_GITHUB_TOKEN"},
    )

    tr := &oauth2.Transport{
        Base:  http.DefaultTransport,
        Source: oauth2.ReuseTokenSource(nil, ts),
    }

    // Start our recorder
    vcrRecorder, err := recorder.NewAsMode(path.Join("testdata", "fixtures", t.Name()), recorder.ModeRecording, tr)
    require.NoError(t, err)
        defer vcrRecorder.Stop() // NEWLY ADDED CODE HERE


    // Filter out dynamic & sensitive data/headers
    // Your test code will continue to see the real access token and
    // it is redacted before the recorded interactions are saved
        // =====> commenting out this section has no impact on missing recording
    vcrRecorder.AddSaveFilter(func(i *cassette.Interaction) error {
        delete(i.Request.Headers, "Authorization")
        delete(i.Request.Headers, "User-Agent")
        i.Request.Headers["Authorization"] = []string{"Basic UExBQ0VIT0xERVI6UExBQ0VIT0xERVI="} //PLACEHOLDER:PLACEHOLDER

        return nil
    })

        // custom http.client
    httpClient := &http.Client{
        Transport: vcrRecorder,
    }

    ghClient := github.NewClient(httpClient)

    // =====> actual test, should create cassettes, but does not.
    _, _, err = ghClient.Users.Get(context.Background(), "")

    require.NoError(t, err)
}

This issue resolved this question for me.
https://github.com/dnaeon/go-vcr/issues/59

Example below

package example_test

import (
    "context"
    "github.com/dnaeon/go-vcr/cassette"
    "github.com/dnaeon/go-vcr/recorder"
    "github.com/google/go-github/v33/github"
    "github.com/stretchr/testify/require"
    "golang.org/x/oauth2"
    "net/http"
    "path"
    "testing"
)

func TestGithub(t *testing.T) {
    //custom http.Transport, since github uses oauth2 authentication
    ts := oauth2.StaticTokenSource(
        &oauth2.Token{AccessToken: "YOUR_GITHUB_TOKEN"},
    )

    tr := &oauth2.Transport{
        Base:  http.DefaultTransport,
        Source: oauth2.ReuseTokenSource(nil, ts),
    }

    // Start our recorder
    vcrRecorder, err := recorder.NewAsMode(path.Join("testdata", "fixtures", t.Name()), recorder.ModeRecording, tr)
    require.NoError(t, err)
        defer vcrRecorder.Stop() // NEWLY ADDED CODE HERE


    // Filter out dynamic & sensitive data/headers
    // Your test code will continue to see the real access token and
    // it is redacted before the recorded interactions are saved
        // =====> commenting out this section has no impact on missing recording
    vcrRecorder.AddSaveFilter(func(i *cassette.Interaction) error {
        delete(i.Request.Headers, "Authorization")
        delete(i.Request.Headers, "User-Agent")
        i.Request.Headers["Authorization"] = []string{"Basic UExBQ0VIT0xERVI6UExBQ0VIT0xERVI="} //PLACEHOLDER:PLACEHOLDER

        return nil
    })

        // custom http.client
    httpClient := &http.Client{
        Transport: vcrRecorder,
    }

    ghClient := github.NewClient(httpClient)

    // =====> actual test, should create cassettes, but does not.
    _, _, err = ghClient.Users.Get(context.Background(), "")

    require.NoError(t, err)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文