Skip to content

Commit 255375e

Browse files
committed
improved auth required error handling
1 parent e1ab7c8 commit 255375e

File tree

2 files changed

+110
-9
lines changed

2 files changed

+110
-9
lines changed

tmc-langs-cli/src/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ fn solve_error_kind(e: &anyhow::Error) -> Kind {
6868
return Kind::AuthorizationError;
6969
}
7070
}
71+
if let Some(CoreError::AuthRequired) = cause.downcast_ref::<CoreError>() {
72+
return Kind::AuthorizationError;
73+
}
7174
// check for connection error
7275
if let Some(CoreError::ConnectionError(..)) = cause.downcast_ref::<CoreError>() {
7376
return Kind::ConnectionError;

tmc-langs-core/src/tmc_core/api.rs

Lines changed: 107 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,17 @@ impl TmcCore {
151151
}
152152

153153
pub(super) fn user(&self, user_id: usize) -> Result<User, CoreError> {
154+
if self.token.is_none() {
155+
return Err(CoreError::AuthRequired);
156+
}
154157
let url_tail = format!("users/{}", user_id);
155158
self.get_json(&url_tail)
156159
}
157160

158161
pub(super) fn user_current(&self) -> Result<User, CoreError> {
162+
if self.token.is_none() {
163+
return Err(CoreError::AuthRequired);
164+
}
159165
let url_tail = "users/current";
160166
self.get_json(url_tail)
161167
}
@@ -169,6 +175,9 @@ impl TmcCore {
169175
}
170176

171177
pub(super) fn course(&self, course_id: usize) -> Result<CourseData, CoreError> {
178+
if self.token.is_none() {
179+
return Err(CoreError::AuthRequired);
180+
}
172181
let url_tail = format!("courses/{}", course_id);
173182
self.get_json(&url_tail)
174183
}
@@ -178,6 +187,9 @@ impl TmcCore {
178187
organization_slug: &str,
179188
course_name: &str,
180189
) -> Result<CourseData, CoreError> {
190+
if self.token.is_none() {
191+
return Err(CoreError::AuthRequired);
192+
}
181193
let url_tail = format!(
182194
"org/{}/courses/{}",
183195
percent_encode(organization_slug),
@@ -186,16 +198,22 @@ impl TmcCore {
186198
self.get_json(&url_tail)
187199
}
188200

189-
pub(super) fn course_points(&self, course_id: usize) -> Result<(), CoreError> {
190-
let _url_tail = format!("courses/{}/points", course_id);
191-
todo!("times out")
201+
pub(super) fn course_points(
202+
&self,
203+
course_id: usize,
204+
) -> Result<CourseDataExercisePoint, CoreError> {
205+
let url_tail = format!("courses/{}/points", course_id);
206+
self.get_json(&url_tail)
192207
}
193208

194209
pub(super) fn exercise_points(
195210
&self,
196211
course_id: usize,
197212
exercise_name: &str,
198213
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
214+
if self.token.is_none() {
215+
return Err(CoreError::AuthRequired);
216+
}
199217
let url_tail = format!(
200218
"courses/{}/exercises/{}/points",
201219
course_id,
@@ -210,6 +228,9 @@ impl TmcCore {
210228
exercise_name: &str,
211229
user_id: usize,
212230
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
231+
if self.token.is_none() {
232+
return Err(CoreError::AuthRequired);
233+
}
213234
let url_tail = format!(
214235
"courses/{}/exercises/{}/users/{}/points",
215236
course_id,
@@ -224,6 +245,9 @@ impl TmcCore {
224245
course_id: usize,
225246
exercise_name: &str,
226247
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
248+
if self.token.is_none() {
249+
return Err(CoreError::AuthRequired);
250+
}
227251
let url_tail = format!(
228252
"courses/{}/exercises/{}/users/current/points",
229253
course_id,
@@ -254,6 +278,9 @@ impl TmcCore {
254278
organization_slug: &str,
255279
course_name: &str,
256280
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
281+
if self.token.is_none() {
282+
return Err(CoreError::AuthRequired);
283+
}
257284
let url_tail = format!(
258285
"org/{}/courses/{}/points",
259286
percent_encode(organization_slug),
@@ -267,6 +294,9 @@ impl TmcCore {
267294
organization_slug: &str,
268295
course_name: &str,
269296
) -> Result<(), CoreError> {
297+
if self.token.is_none() {
298+
return Err(CoreError::AuthRequired);
299+
}
270300
let _url_tail = format!(
271301
"org/{}/courses/{}/eligible_students",
272302
percent_encode(organization_slug),
@@ -281,6 +311,9 @@ impl TmcCore {
281311
course_name: &str,
282312
exercise_name: &str,
283313
) -> Result<(), CoreError> {
314+
if self.token.is_none() {
315+
return Err(CoreError::AuthRequired);
316+
}
284317
let url_tail = format!(
285318
"org/{}/courses/{}/exercises/{}/points",
286319
percent_encode(organization_slug),
@@ -296,6 +329,9 @@ impl TmcCore {
296329
course_name: &str,
297330
exercise_name: &str,
298331
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
332+
if self.token.is_none() {
333+
return Err(CoreError::AuthRequired);
334+
}
299335
let url_tail = format!(
300336
"org/{}/courses/{}/exercises/{}/users/current/points",
301337
percent_encode(organization_slug),
@@ -312,6 +348,9 @@ impl TmcCore {
312348
exercise_name: &str,
313349
user_id: usize,
314350
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
351+
if self.token.is_none() {
352+
return Err(CoreError::AuthRequired);
353+
}
315354
let url_tail = format!(
316355
"org/{}/courses/{}/exercises/{}/users/{}/points",
317356
percent_encode(organization_slug),
@@ -328,6 +367,9 @@ impl TmcCore {
328367
course_name: &str,
329368
user_id: usize,
330369
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
370+
if self.token.is_none() {
371+
return Err(CoreError::AuthRequired);
372+
}
331373
let url_tail = format!(
332374
"org/{}/courses/{}/users/{}/points",
333375
percent_encode(organization_slug),
@@ -342,6 +384,9 @@ impl TmcCore {
342384
organization_slug: &str,
343385
course_name: &str,
344386
) -> Result<Vec<CourseDataExercisePoint>, CoreError> {
387+
if self.token.is_none() {
388+
return Err(CoreError::AuthRequired);
389+
}
345390
let url_tail = format!(
346391
"org/{}/courses/{}/users/current/points",
347392
percent_encode(organization_slug),
@@ -354,6 +399,9 @@ impl TmcCore {
354399
&self,
355400
course_id: usize,
356401
) -> Result<Vec<Submission>, CoreError> {
402+
if self.token.is_none() {
403+
return Err(CoreError::AuthRequired);
404+
}
357405
let url_tail = format!("courses/{}/submissions", course_id);
358406
self.get_json(&url_tail)
359407
}
@@ -362,6 +410,9 @@ impl TmcCore {
362410
&self,
363411
course_id: usize,
364412
) -> Result<Vec<Submission>, CoreError> {
413+
if self.token.is_none() {
414+
return Err(CoreError::AuthRequired);
415+
}
365416
let url_tail = format!("courses/{}/submissions/last_hour", course_id);
366417
self.get_json(&url_tail)
367418
}
@@ -371,6 +422,9 @@ impl TmcCore {
371422
course_id: usize,
372423
user_id: usize,
373424
) -> Result<Vec<Submission>, CoreError> {
425+
if self.token.is_none() {
426+
return Err(CoreError::AuthRequired);
427+
}
374428
let url_tail = format!("courses/{}/users/{}/submissions", course_id, user_id);
375429
self.get_json(&url_tail)
376430
}
@@ -379,6 +433,9 @@ impl TmcCore {
379433
&self,
380434
course_id: usize,
381435
) -> Result<Vec<Submission>, CoreError> {
436+
if self.token.is_none() {
437+
return Err(CoreError::AuthRequired);
438+
}
382439
let url_tail = format!("courses/{}/users/current/submissions", course_id);
383440
self.get_json(&url_tail)
384441
}
@@ -388,6 +445,9 @@ impl TmcCore {
388445
exercise_id: usize,
389446
user_id: usize,
390447
) -> Result<Vec<Submission>, CoreError> {
448+
if self.token.is_none() {
449+
return Err(CoreError::AuthRequired);
450+
}
391451
let url_tail = format!("exercises/{}/users/{}/submissions", exercise_id, user_id);
392452
self.get_json(&url_tail)
393453
}
@@ -396,6 +456,9 @@ impl TmcCore {
396456
&self,
397457
exercise_id: usize,
398458
) -> Result<Vec<Submission>, CoreError> {
459+
if self.token.is_none() {
460+
return Err(CoreError::AuthRequired);
461+
}
399462
let url_tail = format!("exercises/{}/users/current/submissions", exercise_id);
400463
self.get_json(&url_tail)
401464
}
@@ -405,6 +468,9 @@ impl TmcCore {
405468
organization_slug: &str,
406469
course_name: &str,
407470
) -> Result<Vec<Submission>, CoreError> {
471+
if self.token.is_none() {
472+
return Err(CoreError::AuthRequired);
473+
}
408474
let url_tail = format!(
409475
"org/{}/courses/{}/submissions",
410476
percent_encode(organization_slug),
@@ -419,6 +485,9 @@ impl TmcCore {
419485
course_name: &str,
420486
user_id: usize,
421487
) -> Result<Vec<Submission>, CoreError> {
488+
if self.token.is_none() {
489+
return Err(CoreError::AuthRequired);
490+
}
422491
let url_tail = format!(
423492
"org/{}/courses/{}/users/{}/submissions",
424493
percent_encode(organization_slug),
@@ -433,6 +502,9 @@ impl TmcCore {
433502
organization_slug: &str,
434503
course_name: &str,
435504
) -> Result<Vec<Submission>, CoreError> {
505+
if self.token.is_none() {
506+
return Err(CoreError::AuthRequired);
507+
}
436508
let url_tail = format!(
437509
"org/{}/courses/{}/users/current/submissions",
438510
percent_encode(organization_slug),
@@ -451,6 +523,9 @@ impl TmcCore {
451523
organization_slug: &str,
452524
course_name: &str,
453525
) -> Result<Vec<CourseDataExercise>, CoreError> {
526+
if self.token.is_none() {
527+
return Err(CoreError::AuthRequired);
528+
}
454529
let url_tail = format!(
455530
"org/{}/courses/{}/exercises",
456531
percent_encode(organization_slug),
@@ -466,6 +541,9 @@ impl TmcCore {
466541
exercise_name: &str,
467542
target: &Path,
468543
) -> Result<(), CoreError> {
544+
if self.token.is_none() {
545+
return Err(CoreError::AuthRequired);
546+
}
469547
let url_tail = format!(
470548
"org/{}/courses/{}/exercises/{}/download",
471549
percent_encode(organization_slug),
@@ -486,11 +564,17 @@ impl TmcCore {
486564
}
487565

488566
pub(super) fn core_course(&self, course_id: usize) -> Result<CourseDetails, CoreError> {
567+
if self.token.is_none() {
568+
return Err(CoreError::AuthRequired);
569+
}
489570
let url_tail = format!("core/courses/{}", course_id);
490571
self.get_json(&url_tail)
491572
}
492573

493574
pub(super) fn reviews(&self, course_id: usize) -> Result<Vec<Review>, CoreError> {
575+
if self.token.is_none() {
576+
return Err(CoreError::AuthRequired);
577+
}
494578
let url_tail = format!("core/courses/{}/reviews", course_id);
495579
self.get_json(&url_tail)
496580
}
@@ -500,8 +584,12 @@ impl TmcCore {
500584
course_id: usize,
501585
review_id: usize,
502586
) -> Result<Vec<Review>, CoreError> {
503-
let url_tail = format!("core/courses/{}/reviews/{}", course_id, review_id);
504-
self.get_json(&url_tail)
587+
if self.token.is_none() {
588+
return Err(CoreError::AuthRequired);
589+
}
590+
let _url_tail = format!("core/courses/{}/reviews/{}", course_id, review_id);
591+
// self.get_json(&url_tail)
592+
todo!("does not appear to function")
505593
}
506594

507595
pub(super) fn unlock(&self, course_id: usize) -> Result<(), CoreError> {
@@ -519,6 +607,9 @@ impl TmcCore {
519607
}
520608

521609
pub(super) fn core_exercise(&self, exercise_id: usize) -> Result<ExerciseDetails, CoreError> {
610+
if self.token.is_none() {
611+
return Err(CoreError::AuthRequired);
612+
}
522613
let url_tail = format!("core/exercises/{}", exercise_id);
523614
self.get_json(&url_tail)
524615
}
@@ -528,6 +619,9 @@ impl TmcCore {
528619
exercise_id: usize,
529620
target: &Path,
530621
) -> Result<(), CoreError> {
622+
if self.token.is_none() {
623+
return Err(CoreError::AuthRequired);
624+
}
531625
let url_tail = format!("core/exercises/{}/solution/download", exercise_id);
532626
self.download(&url_tail, target)
533627
}
@@ -577,10 +671,6 @@ impl TmcCore {
577671
params: Option<HashMap<String, String>>,
578672
locale: Option<Language>,
579673
) -> Result<NewSubmission, CoreError> {
580-
if self.token.is_none() {
581-
return Err(CoreError::AuthRequired);
582-
}
583-
584674
/*
585675
let url = self
586676
.api_url
@@ -629,6 +719,9 @@ impl TmcCore {
629719
&self,
630720
organization_slug: &str,
631721
) -> Result<Vec<Course>, CoreError> {
722+
if self.token.is_none() {
723+
return Err(CoreError::AuthRequired);
724+
}
632725
let url_tail = format!("core/org/{}/courses", organization_slug);
633726
self.get_json(&url_tail)
634727
}
@@ -638,6 +731,9 @@ impl TmcCore {
638731
submission_id: usize,
639732
target: &Path,
640733
) -> Result<(), CoreError> {
734+
if self.token.is_none() {
735+
return Err(CoreError::AuthRequired);
736+
}
641737
let url_tail = format!("core/submissions/{}/download", submission_id);
642738
self.download(&url_tail, target)
643739
}
@@ -676,6 +772,7 @@ impl TmcCore {
676772
review_body: &str,
677773
review_points: &str,
678774
) -> Result<(), CoreError> {
775+
// needs auth?
679776
let url_tail = format!("core/submissions/{}/reviews", submission_id);
680777
let url = self
681778
.api_url
@@ -702,6 +799,7 @@ impl TmcCore {
702799
review_update_url: String,
703800
read: bool,
704801
) -> Result<(), CoreError> {
802+
// needs auth?
705803
let url = format!("{}.json", review_update_url);
706804
let url = Url::parse(&url).map_err(|e| CoreError::UrlParse(url, e))?;
707805

0 commit comments

Comments
 (0)