-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update to reflect changes in Whereby's API
Also includes some cleanup: - Remove the WithContext methods, using context in all methods by default. - Add more idiomatic error handling - Add functional tests
- Loading branch information
1 parent
b0c02ce
commit e3ece49
Showing
16 changed files
with
303 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package whereby | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var ErrNotFound = errors.New("meeting not found") | ||
var ErrInvalidCredentials = errors.New("invalid credentials") | ||
|
||
type RateLimitedError struct { | ||
timeLeft time.Duration | ||
} | ||
|
||
func (e RateLimitedError) Error() string { | ||
return "rate limited" | ||
} | ||
|
||
func (e RateLimitedError) TimeLeft() time.Duration { | ||
return e.timeLeft | ||
} | ||
|
||
func handleBadStatus(r *http.Response) error { | ||
switch r.StatusCode { | ||
case http.StatusNotFound: | ||
return ErrNotFound | ||
case http.StatusUnauthorized: | ||
return ErrInvalidCredentials | ||
case http.StatusTooManyRequests: | ||
var rlr rateLimitResponse | ||
if err := json.NewDecoder(r.Body).Decode(&rlr); err == nil && rlr.Data.MSLeft != nil { | ||
defer r.Body.Close() | ||
return RateLimitedError{timeLeft: time.Duration(*rlr.Data.MSLeft) * time.Millisecond} | ||
} | ||
return RateLimitedError{} | ||
default: | ||
return fmt.Errorf("unexpected status %d from Whereby", r.StatusCode) | ||
} | ||
} | ||
|
||
type rateLimitResponse struct { | ||
Description string `json:"error"` | ||
Data struct { | ||
MSLeft *int `json:"ms_left"` | ||
} `json:"data,omitempty"` | ||
} |
Oops, something went wrong.