subscribe-bot/osuapi/osuapi.go

168 lines
3.3 KiB
Go
Raw Normal View History

2020-10-12 13:47:28 +00:00
package osuapi
2020-10-11 19:32:58 +00:00
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"sync"
2020-10-11 19:32:58 +00:00
"time"
2021-07-20 19:37:16 +00:00
"github.com/pkg/errors"
2020-10-11 19:32:58 +00:00
"golang.org/x/sync/semaphore"
2020-10-12 13:47:28 +00:00
"subscribe-bot/config"
2020-10-11 19:32:58 +00:00
)
const BASE_URL = "https://osu.ppy.sh/api/v2"
type Osuapi struct {
2020-10-12 14:25:50 +00:00
httpClient *http.Client
lock *semaphore.Weighted
token string
expires time.Time
config *config.Config
tokenLock sync.RWMutex
isFetchingToken bool
2020-10-11 19:32:58 +00:00
}
2020-10-12 13:47:28 +00:00
func New(config *config.Config) *Osuapi {
2020-10-12 06:33:45 +00:00
client := &http.Client{
Timeout: 9 * time.Second,
2020-10-12 06:33:45 +00:00
}
2020-10-11 19:32:58 +00:00
// want to cap at around 1000 requests a minute, OSU cap is 1200
lock := semaphore.NewWeighted(1000)
return &Osuapi{
httpClient: client,
lock: lock,
expires: time.Now(),
config: config,
}
2020-10-11 19:32:58 +00:00
}
func (api *Osuapi) Token() (token string, err error) {
if time.Now().Before(api.expires) {
token = api.token
return
}
if api.isFetchingToken {
api.tokenLock.RLock()
token = api.token
api.tokenLock.RUnlock()
return
}
api.tokenLock.Lock()
api.isFetchingToken = true
2020-10-11 19:32:58 +00:00
data := fmt.Sprintf(
2020-10-14 19:08:38 +00:00
"client_id=%s&client_secret=%s&grant_type=client_credentials&scope=public",
2020-10-12 14:25:50 +00:00
api.config.Oauth.ClientId,
api.config.Oauth.ClientSecret,
2020-10-11 19:32:58 +00:00
)
2020-10-12 09:52:19 +00:00
resp, err := http.Post(
"https://osu.ppy.sh/oauth/token",
"application/x-www-form-urlencoded",
strings.NewReader(data),
)
2020-10-11 19:32:58 +00:00
if err != nil {
2021-07-20 19:37:16 +00:00
err = errors.Wrap("failed to make POST request")
2020-10-11 19:32:58 +00:00
return
}
var osuToken OsuToken
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
2021-07-20 19:37:16 +00:00
err = errors.Wrap("failed to read response body")
2020-10-11 19:32:58 +00:00
return
}
err = json.Unmarshal(respBody, &osuToken)
if err != nil {
2021-07-20 19:37:16 +00:00
err = errors.Wrap("failed to unmarshal response body as json")
2020-10-11 19:32:58 +00:00
return
}
log.Println("got new access token", osuToken.AccessToken[:12]+"...")
api.token = osuToken.AccessToken
api.expires = time.Now().Add(time.Duration(osuToken.ExpiresIn) * time.Second)
2020-10-11 19:32:58 +00:00
token = api.token
api.tokenLock.Unlock()
2020-10-11 19:32:58 +00:00
return
}
2020-10-12 04:22:47 +00:00
func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err error) {
2020-10-11 19:32:58 +00:00
err = api.lock.Acquire(context.TODO(), 1)
if err != nil {
return
}
apiUrl := BASE_URL + url
req, err := http.NewRequest(action, apiUrl, nil)
token, err := api.Token()
if err != nil {
2021-07-20 19:37:16 +00:00
err = errors.Wrap("failed to fetch token")
2020-10-11 19:32:58 +00:00
return
}
req.Header.Add("Authorization", "Bearer "+token)
2020-10-12 06:33:45 +00:00
resp, err = api.httpClient.Do(req)
2020-10-11 19:32:58 +00:00
if err != nil {
2021-07-20 19:37:16 +00:00
err = errors.Wrap("http client failed")
2020-10-11 19:32:58 +00:00
return
}
2020-10-12 04:22:47 +00:00
if resp.StatusCode != 200 {
var respBody []byte
respBody, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
err = fmt.Errorf("not 200: %s", string(respBody))
return
}
// release the lock after 1 minute
go func() {
time.Sleep(time.Minute)
api.lock.Release(1)
}()
return
}
func (api *Osuapi) Request(action string, url string, result interface{}) (err error) {
resp, err := api.Request0(action, url)
if err != nil {
2021-07-20 19:37:16 +00:00
return errors.Wrap("base request failed")
2020-10-12 04:22:47 +00:00
}
2020-10-11 19:32:58 +00:00
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
2021-07-20 19:37:16 +00:00
return errors.Wrap("failed to read http response body")
2020-10-11 19:32:58 +00:00
}
err = json.Unmarshal(data, result)
if err != nil {
2021-07-20 19:37:16 +00:00
return errors.Wrap("failed to unmarshal http response as json")
2020-10-11 19:32:58 +00:00
}
2020-10-12 04:22:47 +00:00
return
}
2020-10-11 19:32:58 +00:00
type OsuToken struct {
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
AccessToken string `json:"access_token"`
}