1
1
package mtcapi
2
2
3
3
import (
4
+ "encoding/json"
4
5
"fmt"
5
6
6
7
"github.com/morethancertified/mtc-cli/internal/types"
@@ -24,43 +25,102 @@ func (c *MtcApiClient) GetLabInfo(userLessonID string) (types.LabInfo, error) {
24
25
25
26
// GetLabFiles fetches all files for a lab (public, bootstrap, and other)
26
27
func (c * MtcApiClient ) GetLabFiles (userLessonID string ) ([]types.LabFile , error ) {
27
- res , err := c . httpClient . R ().
28
- SetResult ( & types. LabFiles {} ).
28
+ // Make a single request and print the raw response for debugging
29
+ rawRes , err := c . httpClient . R ( ).
29
30
Get ("/labs/" + userLessonID + "/files" )
30
31
if err != nil {
31
32
return nil , err
32
33
}
33
34
34
- if res .IsError () {
35
- return nil , fmt .Errorf ("API error: %s" , res .String ())
35
+ if rawRes .IsError () {
36
+ return nil , fmt .Errorf ("API error: %s" , rawRes .String ())
36
37
}
37
38
38
- result := res .Result ().(* types.LabFiles )
39
- var files []types.LabFile
39
+ // No need for debug printing in production code
40
+
41
+ // Check for empty files response: {"files":[]}
42
+ if string (rawRes .Body ()) == "{\" files\" :[]}" {
43
+ return []types.LabFile {}, nil
44
+ }
45
+
46
+ // Try to unmarshal as a direct array of LabFile
47
+ var directFiles []types.LabFile
48
+ err = json .Unmarshal (rawRes .Body (), & directFiles )
49
+ if err == nil {
50
+ // If this succeeds, return the files directly
51
+ return directFiles , nil
52
+ }
53
+
54
+ // Try to unmarshal as a simple wrapper with files array
55
+ var simpleFiles struct {
56
+ Files []types.LabFile `json:"files"`
57
+ }
58
+ err = json .Unmarshal (rawRes .Body (), & simpleFiles )
59
+ if err == nil && simpleFiles .Files != nil {
60
+ return simpleFiles .Files , nil
61
+ }
62
+
63
+ // If simple unmarshaling fails, try the full structured approach
64
+ var labFiles types.LabFiles
65
+ err = json .Unmarshal (rawRes .Body (), & labFiles )
66
+ if err != nil {
67
+ return nil , fmt .Errorf ("failed to parse response: %v" , err )
68
+ }
40
69
41
70
// Combine all file types
42
- files = append (files , result .Files .Public ... )
43
- files = append (files , result .Files .Bootstrap ... )
44
- files = append (files , result .Files .Other ... )
71
+ var files []types.LabFile
72
+ files = append (files , labFiles .Files .Public ... )
73
+ files = append (files , labFiles .Files .Bootstrap ... )
74
+ files = append (files , labFiles .Files .Other ... )
45
75
46
76
return files , nil
47
77
}
48
78
49
79
// GetLabPublicFiles fetches only public files for a lab
50
80
func (c * MtcApiClient ) GetLabPublicFiles (userLessonID string ) ([]types.LabFile , error ) {
51
- res , err := c . httpClient . R ().
52
- SetResult ( & types. LabPublicFiles {} ).
81
+ // Make a single request and print the raw response for debugging
82
+ rawRes , err := c . httpClient . R ( ).
53
83
Get ("/labs/" + userLessonID + "/files/public" )
54
84
if err != nil {
55
85
return nil , err
56
86
}
57
87
58
- if res .IsError () {
59
- return nil , fmt .Errorf ("API error: %s" , res .String ())
88
+ if rawRes .IsError () {
89
+ return nil , fmt .Errorf ("API error: %s" , rawRes .String ())
90
+ }
91
+
92
+ // No need for debug printing in production code
93
+
94
+ // Check for empty files response: {"files":[]}
95
+ if string (rawRes .Body ()) == "{\" files\" :[]}" {
96
+ return []types.LabFile {}, nil
97
+ }
98
+
99
+ // Try to unmarshal as a direct array of LabFile
100
+ var directFiles []types.LabFile
101
+ err = json .Unmarshal (rawRes .Body (), & directFiles )
102
+ if err == nil {
103
+ // If this succeeds, return the files directly
104
+ return directFiles , nil
105
+ }
106
+
107
+ // Try to unmarshal as a simple wrapper with files array
108
+ var simpleFiles struct {
109
+ Files []types.LabFile `json:"files"`
110
+ }
111
+ err = json .Unmarshal (rawRes .Body (), & simpleFiles )
112
+ if err == nil && simpleFiles .Files != nil {
113
+ return simpleFiles .Files , nil
114
+ }
115
+
116
+ // If simple unmarshaling fails, try the structured approach
117
+ var labFiles types.LabPublicFiles
118
+ err = json .Unmarshal (rawRes .Body (), & labFiles )
119
+ if err != nil {
120
+ return nil , fmt .Errorf ("failed to parse public files response: %v" , err )
60
121
}
61
122
62
- result := res .Result ().(* types.LabPublicFiles )
63
- return result .Files , nil
123
+ return labFiles .Files , nil
64
124
}
65
125
66
126
// GetLabFileURL fetches a pre-signed URL for a specific file
0 commit comments