Skip to content

Commit

Permalink
feat(rest-client): set api-key in the header
Browse files Browse the repository at this point in the history
  • Loading branch information
DipandaAser committed Nov 12, 2022
1 parent 59a202c commit ac9d63e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:
- .github/workflows/CI.yaml
- .github/workflows/lint.yaml
- .github/pull_request_template.yml
- pkg/rest-client
- README.md

env:
Expand Down
20 changes: 16 additions & 4 deletions pkg/rest-client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ func (rc *RestClient) UploadFileReader(chatId int64, fileName string, fileReader
params := url.Values{}
params.Set("chat_id", fmt.Sprintf("%d", chatId))
params.Set("file_name", fileName)
params.Set("api-key", rc.apiKey)
var finalUrl = fmt.Sprintf("%s/%s?%s", rc.getApiUrl(), "files", params.Encode())
response, err := rc.client.Post(finalUrl, "application/octet-stream", fileReader)
request, err := http.NewRequest(http.MethodPost, finalUrl, fileReader)
if err != nil {
return v1.MessageIdentifier{}, err
}
request.Header.Set("Content-Type", "application/octet-stream")
response, err := rc.do(request)
if err != nil {
return v1.MessageIdentifier{}, err
}
Expand Down Expand Up @@ -71,9 +75,12 @@ func (rc *RestClient) DownloadFileReader(identifier v1.MessageIdentifier, copyCh
params.Set("chat_id", fmt.Sprintf("%d", identifier.ChatId))
params.Set("msg_id", fmt.Sprintf("%d", identifier.MessageId))
params.Set("draft_chat_id", fmt.Sprintf("%d", copyChat))
params.Set("api-key", rc.apiKey)
var finalUrl = fmt.Sprintf("%s/%s?%s", rc.getApiUrl(), "files", params.Encode())
response, err := rc.client.Get(finalUrl)
request, err := http.NewRequest(http.MethodGet, finalUrl, nil)
if err != nil {
return nil, err
}
response, err := rc.do(request)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -119,3 +126,8 @@ func (rc *RestClient) DownloadFileBuffer(identifier v1.MessageIdentifier, copyCh
FileInfo: result.FileInfo,
}, nil
}

func (rc *RestClient) do(req *http.Request) (*http.Response, error) {
req.Header.Set("api-key", rc.apiKey)
return rc.client.Do(req)
}

0 comments on commit ac9d63e

Please sign in to comment.