This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ilias.go
172 lines (134 loc) · 3.52 KB
/
ilias.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package ilias
import (
"bytes"
"errors"
"io"
"log"
"mime/multipart"
"net/http"
"net/http/cookiejar"
"net/textproto"
"net/url"
"strings"
"github.com/gorilla/schema"
)
var (
decoder = schema.NewDecoder()
encoder = schema.NewEncoder()
ErrCredentials = errors.New("wrong username or password")
ErrToken = errors.New("token could not be found")
ErrFullName = errors.New("full name could not be found")
ErrUpdate = errors.New("update failed")
ErrFileHash = errors.New("file hash could not be found")
)
const (
baseUrl string = "https://ilias.hhu.de/"
defaultHost string = "ilias.hhu.de"
)
type Credentials struct {
Username string
Password string
}
type Client struct {
// The current user's login name.
User *User
// Base URL for requests. Should end with a dash.
BaseURL *url.URL
// Host field set within request headers
Host string
// HTTP Client used for making requests against the ILIAS platform.
client *http.Client
common service
Auth *AuthService
Exercise *ExerciseService
Members *MemberService
}
type service struct {
client *Client
}
func NewClient(client *http.Client, credentials *Credentials) (*Client, error) {
// Create a default client if none is specified
if client == nil {
client = http.DefaultClient
}
// Attach a cookie jar to the client
jar, err := cookiejar.New(nil)
if err != nil {
log.Fatal(err)
return nil, err
}
client.Jar = jar
// Parse the base url and create the http client
base, _ := url.Parse(baseUrl)
ret := &Client{ BaseURL: base, Host: defaultHost, client: client }
ret.common.client = ret
ret.Auth = (*AuthService)(&ret.common)
ret.Exercise = (*ExerciseService)(&ret.common)
ret.Members = (*MemberService)(&ret.common)
// Login using the client
user, err := ret.Auth.Login(credentials.Username, credentials.Password)
if err != nil {
return nil, err
}
ret.User = user
return ret, nil
}
func (c *Client) NewRequest(method string, path string, body url.Values) (*http.Request, error) {
target, err := c.BaseURL.Parse(path)
if err != nil {
return nil, err
}
// https://github.com/golang/go/issues/32897
var request *http.Request
if body != nil {
request, err = http.NewRequest(method, target.String(), strings.NewReader(body.Encode()))
} else {
request, err = http.NewRequest(method, target.String(), nil)
}
if err != nil {
return nil, err
}
if body != nil {
request.Header.Set("content-type", "application/x-www-form-urlencoded")
}
request.Host = c.Host
return request, nil
}
type UploadFile struct {
Header textproto.MIMEHeader
Content *bytes.Buffer
}
func (c *Client) NewMultipartRequest(method string, path string, body url.Values, upload *UploadFile) (*http.Request, error) {
target, err := c.BaseURL.Parse(path)
if err != nil {
return nil, err
}
var buf bytes.Buffer
writer := multipart.NewWriter(&buf)
// Create form field for the file
fieldWriter, err := writer.CreatePart(upload.Header)
if err != nil {
return nil, err
}
// Copy file contents into request
if _, err = io.Copy(fieldWriter, upload.Content); err != nil {
return nil, err
}
// Copy additional parameters
for key, array := range body {
for _, value := range array {
_ = writer.WriteField(key, value)
}
}
writer.Close()
request, err := http.NewRequest(method, target.String(), &buf)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", writer.FormDataContentType())
request.Host = c.Host
return request, nil
}
func (c *Client) Do(req *http.Request) (*http.Response, error) {
return c.client.Do(req)
}