tonkeeper / tonapi-go

TonAPI SDK allows developers to build decentralized applications on top of the TON blockchain without having to deal with the complexity of the underlying technology.
https://tonapi.io
98 stars 22 forks source link

The official doc is said the header is X-API-Key. but your header key is Authorization. no explain? #11

Closed yangyile1990 closed 10 months ago

yangyile1990 commented 10 months ago

func (c clientWithApiKey) Do(r http.Request) (http.Response, error) { r.Header.Set("Authorization", c.header) return http.DefaultClient.Do(r) }

截屏2024-01-02 10 04 53

so If I get the key from ton. How to use your sdk?

yangyile1990 commented 10 months ago

如果API文档要求将API密钥作为"X-API-Key"头部发送,那么你可以对代码进行修改以符合要求。

首先,修改clientWithApiKey的定义,将header字段改为X-API-Key,如下所示:

type clientWithApiKey struct {
    apiKey string
}

然后,在Do方法中,将Authorization头部修改为X-API-Key,如下所示:

func (c clientWithApiKey) Do(r *http.Request) (*http.Response, error) {
    r.Header.Set("X-API-Key", c.apiKey)
    return http.DefaultClient.Do(r)
}

接下来,修改WithTonApiKey函数,以使用X-API-Key头部而不是Authorization头部,如下所示:

func WithTonApiKey(tonApiKey string) ClientOption {
    return WithClient(&clientWithApiKey{apiKey: tonApiKey})
}

现在,你可以使用这些修改后的代码来发送带有"X-API-Key"头部的请求。示例如下:

apiKey := "YOUR_API_KEY"
url := "https://api.example.com/endpoint"

req, err := http.NewRequest("GET", url, nil)
if err != nil {
    fmt.Println("创建请求失败:", err)
    return
}

client := &httpClient{}
WithTonApiKey(apiKey)(client)

// 在这里使用 client 发送请求
// ...

通过这些修改,你将能够使用X-API-Key头部来发送API密钥。请确保将"YOUR_API_KEY"替换为你实际的API密钥,并根据你的具体情况调整请求的方法、URL和其他相关的代码。

请注意,以上修改是基于你提供的代码进行的推测。具体的实现可能因使用的SDK或API要求而有所不同。请参考相关文档以获得准确的信息。

yangyile1990 commented 10 months ago

so you should rewrite you code or doc.

yangyile1990 commented 10 months ago

I was wrong. the https://testnet.toncenter.com/ and the https://testnet.tonapi.io/ they are not same system.