package osuapi import ( "context" "encoding/json" "fmt" "io/ioutil" "log" "net/http" "strings" "sync" "time" "github.com/pkg/errors" "golang.org/x/sync/semaphore" "subscribe-bot/config" ) const BASE_URL = "https://osu.ppy.sh/api/v2" type Osuapi struct { httpClient *http.Client lock *semaphore.Weighted token string expires time.Time config *config.Config tokenLock sync.RWMutex isFetchingToken bool } func New(config *config.Config) *Osuapi { client := &http.Client{ Timeout: 9 * time.Second, } // 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, } } func (api *Osuapi) Token() (token string, err error) { return api.tokenFetch(false) } func (api *Osuapi) tokenFetch(force bool) (token string, err error) { if !force { 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 data := fmt.Sprintf( "client_id=%s&client_secret=%s&grant_type=client_credentials&scope=public", api.config.Oauth.ClientId, api.config.Oauth.ClientSecret, ) resp, err := http.Post( "https://osu.ppy.sh/oauth/token", "application/x-www-form-urlencoded", strings.NewReader(data), ) if err != nil { err = errors.Wrap(err, "failed to make POST request") return } var osuToken OsuToken respBody, err := ioutil.ReadAll(resp.Body) if err != nil { err = errors.Wrap(err, "failed to read response body") return } err = json.Unmarshal(respBody, &osuToken) if err != nil { err = errors.Wrap(err, "failed to unmarshal response body as json") 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) token = api.token api.tokenLock.Unlock() return } func (api *Osuapi) Request0(action string, url string) (resp *http.Response, err error) { 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 { err = errors.Wrap(err, "failed to fetch token") return } req.Header.Add("Authorization", "Bearer "+token) resp, err = api.httpClient.Do(req) if err != nil { err = errors.Wrap(err, "http client failed") return } if resp.StatusCode != 200 { if resp.StatusCode == 401 { // force fetch token again api.tokenFetch(true) log.Println("re-fetched token cus expired") return api.Request0(action, url) } 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 { return errors.Wrap(err, "base request failed") } data, err := ioutil.ReadAll(resp.Body) if err != nil { return errors.Wrap(err, "failed to read http response body") } err = json.Unmarshal(data, result) if err != nil { return errors.Wrap(err, "failed to unmarshal http response as json") } return } type OsuToken struct { TokenType string `json:"token_type"` ExpiresIn int `json:"expires_in"` AccessToken string `json:"access_token"` }