mirror of
https://github.com/Xevion/banner.git
synced 2026-01-31 06:23:37 -06:00
Use DoRequest, fix response unmarshalling
This commit is contained in:
@@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -14,11 +15,14 @@ var (
|
|||||||
sessionID string = RandomString(5) + Nonce()
|
sessionID string = RandomString(5) + Nonce()
|
||||||
)
|
)
|
||||||
|
|
||||||
type Term struct {
|
// BannerTerm represents a term in the Banner system
|
||||||
|
type BannerTerm struct {
|
||||||
|
Code string `json:"code"`
|
||||||
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /classSearch/getTerms?searchTerm=&offset=1&max=10&_=1702069154094
|
// GET /classSearch/getTerms?searchTerm=&offset=1&max=10&_=1702069154094
|
||||||
func GetTerms(search string, offset int, max int) ([]Term, error) {
|
func GetTerms(search string, offset int, max int) ([]BannerTerm, error) {
|
||||||
req := BuildRequest("GET", "/classSearch/getTerms", map[string]string{
|
req := BuildRequest("GET", "/classSearch/getTerms", map[string]string{
|
||||||
"searchTerm": search,
|
"searchTerm": search,
|
||||||
"offset": strconv.Itoa(offset),
|
"offset": strconv.Itoa(offset),
|
||||||
@@ -26,20 +30,32 @@ func GetTerms(search string, offset int, max int) ([]Term, error) {
|
|||||||
"_": Nonce(),
|
"_": Nonce(),
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := doRequest(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// print the response body
|
|
||||||
// _body, _ := io.ReadAll(res.Body)
|
|
||||||
|
|
||||||
// Assert that the response is JSON
|
// Assert that the response is JSON
|
||||||
if !ContentTypeMatch(res, "application/json") {
|
if contentType := res.Header.Get("Content-Type"); !strings.Contains(contentType, JsonContentType) {
|
||||||
log.Printf("ERR Response was not JSON: %s", res.Header.Get("Content-Type"))
|
return nil, &UnexpectedContentTypeError{
|
||||||
|
Expected: JsonContentType,
|
||||||
|
Actual: contentType,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return make([]Term, 0, 0), nil
|
// print the response body
|
||||||
|
defer res.Body.Close()
|
||||||
|
body, _ := io.ReadAll(res.Body)
|
||||||
|
log.Printf("Response: %s", body)
|
||||||
|
|
||||||
|
terms := make([]BannerTerm, 0, 10)
|
||||||
|
json.Unmarshal(body, &terms)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return terms, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type TermParts struct {
|
type TermParts struct {
|
||||||
@@ -56,7 +72,7 @@ func GetPartOfTerms(search string, term int, offset int, max int) ([]TermParts,
|
|||||||
"_": Nonce(),
|
"_": Nonce(),
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -83,9 +99,8 @@ func GetInstructor(search string, term int, offset int, max int) []Instructor {
|
|||||||
"_": Nonce(),
|
"_": Nonce(),
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERR %s", err)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -107,9 +122,9 @@ func GetClassDetails(term int, crn int) *ClassDetails {
|
|||||||
"first": "first", // TODO: What is this?
|
"first": "first", // TODO: What is this?
|
||||||
})
|
})
|
||||||
req := BuildRequestWithBody("GET", "/searchResults/getClassDetails", nil, bytes.NewBuffer(body))
|
req := BuildRequestWithBody("GET", "/searchResults/getClassDetails", nil, bytes.NewBuffer(body))
|
||||||
res, err := client.Do(req)
|
|
||||||
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERR %s", err)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,9 +152,8 @@ func Search(subject string, keyword string, term string, startDate time.Time, en
|
|||||||
"sortDirection": "asc",
|
"sortDirection": "asc",
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERR %s", err)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,9 +179,8 @@ func GetSubjects(search string, term int, offset int, max int) []Subject {
|
|||||||
"_": Nonce(),
|
"_": Nonce(),
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERR %s", err)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -193,7 +206,7 @@ func GetCampuses(search string, term int, offset int, max int) []Campus {
|
|||||||
"_": Nonce(),
|
"_": Nonce(),
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERR %s", err)
|
log.Printf("ERR %s", err)
|
||||||
return nil
|
return nil
|
||||||
@@ -221,7 +234,7 @@ func GetInstructionalMethods(search string, term int, offset int, max int) ([]In
|
|||||||
"_": Nonce(),
|
"_": Nonce(),
|
||||||
})
|
})
|
||||||
|
|
||||||
res, err := client.Do(req)
|
res, err := DoRequest(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("ERR %s", err)
|
log.Printf("ERR %s", err)
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -244,8 +257,7 @@ func GetCourseMeetingTime(term int, crn int) (*MeetingTimeResponse, error) {
|
|||||||
"courseReferenceNumber": strconv.Itoa(crn),
|
"courseReferenceNumber": strconv.Itoa(crn),
|
||||||
})
|
})
|
||||||
|
|
||||||
log.Printf("GET %s", req.URL.String())
|
res, err := DoRequest(req)
|
||||||
res, err := client.Do(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -17,7 +17,7 @@ func setup() {
|
|||||||
|
|
||||||
for _, path := range request_queue {
|
for _, path := range request_queue {
|
||||||
req := BuildRequest("GET", path, nil)
|
req := BuildRequest("GET", path, nil)
|
||||||
doRequest(req)
|
DoRequest(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate that cookies were set
|
// Validate that cookies were set
|
||||||
|
|||||||
Reference in New Issue
Block a user