feat: interactive auth method selection with API token validation
- Add GetToken() as unified auth entry point with method selection - Prompt [1] OAuth / [2] API token on first launch, save choice - Validate API tokens against /api/v1/users/self before saving - Check saved API token validity on reuse; fallback to re-prompt on failure - Add HTTP status check to GetCourseInfo and GetEnrolledCourses - Remove --api flag, auth method is now persistent and interactive
This commit is contained in:
@@ -16,6 +16,13 @@ import (
|
||||
"git.directme.in/Joren/CanvasArchiver/internal/utils"
|
||||
)
|
||||
|
||||
func checkAPIResponse(resp *http.Response) error {
|
||||
if resp.StatusCode < 300 {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("API error: %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
HTTPClient *http.Client
|
||||
AccessToken string
|
||||
@@ -48,6 +55,10 @@ func (c *Client) GetCourseInfo() error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := checkAPIResponse(resp); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var course models.Course
|
||||
json.NewDecoder(resp.Body).Decode(&course)
|
||||
|
||||
@@ -64,6 +75,10 @@ func (c *Client) GetEnrolledCourses() ([]models.Course, error) {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if err := checkAPIResponse(resp); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var courses []models.Course
|
||||
json.NewDecoder(resp.Body).Decode(&courses)
|
||||
return courses, nil
|
||||
|
||||
Reference in New Issue
Block a user