Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
refactor: export the models package
Browse files Browse the repository at this point in the history
Since Go's re-export pattern is broken (re-exported types
can't retain their associated function's), this commit:
- Removes the re-export layer
- Moves the models package from the `internal` package
  to the base amizone package.

Implications are easier handling of new models, less
duplicated code and logic etc.
  • Loading branch information
ditsuke committed Jan 14, 2023
1 parent 97e9671 commit 5e4a751
Show file tree
Hide file tree
Showing 28 changed files with 84 additions and 106 deletions.
31 changes: 16 additions & 15 deletions amizone/amizone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/ditsuke/go-amizone/amizone/internal"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/models"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -163,7 +164,7 @@ func (a *Client) login() error {

// GetAttendance retrieves, parses and returns attendance data from Amizone for courses the client user is enrolled in
// for their latest semester.
func (a *Client) GetAttendance() (AttendanceRecords, error) {
func (a *Client) GetAttendance() (models.AttendanceRecords, error) {
response, err := a.doRequest(true, http.MethodGet, attendancePageEndpoint, nil)
if err != nil {
klog.Warningf("request (attendance): %s", err.Error())
Expand All @@ -176,13 +177,13 @@ func (a *Client) GetAttendance() (AttendanceRecords, error) {
return nil, fmt.Errorf("%s: %w", ErrInternalFailure, err)
}

return AttendanceRecords(attendanceRecord), nil
return models.AttendanceRecords(attendanceRecord), nil
}

// GetClassSchedule retrieves, parses and returns class schedule data from Amizone.
// Getmodels.ClassSchedule retrieves, parses and returns class schedule data from Amizone.
// The date parameter is used to determine which schedule to retrieve, but Amizone imposes arbitrary limits on the
// date range, so we have no way of knowing if a request will succeed.
func (a *Client) GetClassSchedule(year int, month time.Month, date int) (ClassSchedule, error) {
func (a *Client) ClassSchedule(year int, month time.Month, date int) (models.ClassSchedule, error) {
timeFrom := time.Date(year, month, date, 0, 0, 0, 0, time.UTC)
timeTo := timeFrom.Add(time.Hour * 24)

Expand All @@ -201,13 +202,13 @@ func (a *Client) GetClassSchedule(year int, month time.Month, date int) (ClassSc
}
filteredSchedule := classSchedule.FilterByDate(timeFrom)

return ClassSchedule(filteredSchedule), nil
return models.ClassSchedule(filteredSchedule), nil
}

// GetExamSchedule retrieves, parses and returns exam schedule data from Amizone.
// Amizone only allows to retrieve the exam schedule for the current semester, and only close to the exam
// dates once the date sheets are out, so we don't take a parameter here.
func (a *Client) GetExamSchedule() (*ExamSchedule, error) {
func (a *Client) GetExamSchedule() (*models.ExaminationSchedule, error) {
response, err := a.doRequest(true, http.MethodGet, examScheduleEndpoint, nil)
if err != nil {
klog.Warningf("request (exam schedule): %s", err.Error())
Expand All @@ -220,12 +221,12 @@ func (a *Client) GetExamSchedule() (*ExamSchedule, error) {
return nil, fmt.Errorf("%s: %w", ErrInternalFailure, err)
}

return (*ExamSchedule)(examSchedule), nil
return (*models.ExaminationSchedule)(examSchedule), nil
}

// GetSemesters retrieves, parses and returns a SemesterList from Amizone. This list includes all semesters for which
// information can be retrieved through other semester-specific methods like GetCourses.
func (a *Client) GetSemesters() (SemesterList, error) {
func (a *Client) GetSemesters() (models.SemesterList, error) {
response, err := a.doRequest(true, http.MethodGet, currentCoursesEndpoint, nil)
if err != nil {
klog.Warningf("request (get semesters): %s", err.Error())
Expand All @@ -238,13 +239,13 @@ func (a *Client) GetSemesters() (SemesterList, error) {
return nil, fmt.Errorf("%s: %w", ErrInternalFailure, err)
}

return (SemesterList)(semesters), nil
return (models.SemesterList)(semesters), nil
}

// GetCourses retrieves, parses and returns a SemesterList from Amizone for the semester referred by
// semesterRef. Semester references should be retrieved through GetSemesters, which returns a list of valid
// semesters with names and references.
func (a *Client) GetCourses(semesterRef string) (Courses, error) {
func (a *Client) GetCourses(semesterRef string) (models.Courses, error) {
payload := url.Values{
"sem": []string{semesterRef},
}.Encode()
Expand All @@ -261,11 +262,11 @@ func (a *Client) GetCourses(semesterRef string) (Courses, error) {
return nil, fmt.Errorf("%s: %w", ErrInternalFailure, err)
}

return Courses(courses), nil
return models.Courses(courses), nil
}

// GetCurrentCourses retrieves, parses and returns a SemesterList from Amizone for the most recent semester.
func (a *Client) GetCurrentCourses() (Courses, error) {
func (a *Client) GetCurrentCourses() (models.Courses, error) {
response, err := a.doRequest(true, http.MethodGet, currentCoursesEndpoint, nil)
if err != nil {
klog.Warningf("request (get current courses): %s", err.Error())
Expand All @@ -278,11 +279,11 @@ func (a *Client) GetCurrentCourses() (Courses, error) {
return nil, fmt.Errorf("%s: %w", ErrInternalFailure, err)
}

return Courses(courses), nil
return models.Courses(courses), nil
}

// GetProfile retrieves, parsed and returns the current user's profile from Amizone.
func (a *Client) GetProfile() (*Profile, error) {
func (a *Client) GetProfile() (*models.Profile, error) {
response, err := a.doRequest(true, http.MethodGet, profileEndpoint, nil)
if err != nil {
klog.Warningf("request (get profile): %s", err.Error())
Expand All @@ -295,5 +296,5 @@ func (a *Client) GetProfile() (*Profile, error) {
return nil, fmt.Errorf("%s: %w", ErrInternalFailure, err)
}

return (*Profile)(profile), nil
return (*models.Profile)(profile), nil
}
5 changes: 3 additions & 2 deletions amizone/amizone_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
package amizone_test

import (
"github.com/ditsuke/go-amizone/amizone"
. "github.com/onsi/gomega"
"os"
"testing"

"github.com/ditsuke/go-amizone/amizone"
. "github.com/onsi/gomega"
)

func TestIntegrateNewClient(t *testing.T) {
Expand Down
38 changes: 19 additions & 19 deletions amizone/amizone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/ditsuke/go-amizone/amizone"
"github.com/ditsuke/go-amizone/amizone/internal/mock"
"github.com/ditsuke/go-amizone/amizone/models"
. "github.com/onsi/gomega"
"gopkg.in/h2non/gock.v1"
)
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestAmizoneClient_GetAttendance(t *testing.T) {
name string
amizoneClient *amizone.Client
setup func(g *WithT)
attendanceMatcher func(g *WithT, attendance amizone.AttendanceRecords)
attendanceMatcher func(g *WithT, attendance models.AttendanceRecords)
errorMatcher func(g *WithT, err error)
}{
{
Expand All @@ -74,7 +75,7 @@ func TestAmizoneClient_GetAttendance(t *testing.T) {
err := mock.GockRegisterHomePageLoggedIn()
g.Expect(err).ToNot(HaveOccurred())
},
attendanceMatcher: func(g *WithT, attendance amizone.AttendanceRecords) {
attendanceMatcher: func(g *WithT, attendance models.AttendanceRecords) {
g.Expect(len(attendance)).To(Equal(8))
},
errorMatcher: func(g *WithT, err error) {
Expand All @@ -88,7 +89,7 @@ func TestAmizoneClient_GetAttendance(t *testing.T) {
err := mock.GockRegisterUnauthenticatedGet("/Home")
g.Expect(err).ToNot(HaveOccurred())
},
attendanceMatcher: func(g *WithT, attendance amizone.AttendanceRecords) {
attendanceMatcher: func(g *WithT, attendance models.AttendanceRecords) {
g.Expect(attendance).To(BeEmpty())
},
errorMatcher: func(g *WithT, err error) {
Expand Down Expand Up @@ -125,7 +126,7 @@ func TestClient_GetSemesters(t *testing.T) {
name string
client *amizone.Client
setup func(g *WithT)
semestersMatcher func(g *WithT, semesters amizone.SemesterList)
semestersMatcher func(g *WithT, semesters models.SemesterList)
errMatcher func(g *WithT, err error)
}{
{
Expand All @@ -135,7 +136,7 @@ func TestClient_GetSemesters(t *testing.T) {
err := mock.GockRegisterCurrentCoursesPage()
g.Expect(err).ToNot(HaveOccurred())
},
semestersMatcher: func(g *WithT, semesters amizone.SemesterList) {
semestersMatcher: func(g *WithT, semesters models.SemesterList) {
g.Expect(semesters).To(HaveLen(4))
},
errMatcher: func(g *WithT, err error) {
Expand All @@ -149,7 +150,7 @@ func TestClient_GetSemesters(t *testing.T) {
err := mock.GockRegisterLoginPage()
g.Expect(err).ToNot(HaveOccurred())
},
semestersMatcher: func(g *WithT, semesters amizone.SemesterList) {
semestersMatcher: func(g *WithT, semesters models.SemesterList) {
g.Expect(semesters).To(HaveLen(0))
},
errMatcher: func(g *WithT, err error) {
Expand Down Expand Up @@ -185,7 +186,7 @@ func TestClient_GetCourses(t *testing.T) {
client *amizone.Client
semesterRef string
setup func(g *WithT)
coursesMatcher func(g *WithT, courses amizone.Courses)
coursesMatcher func(g *WithT, courses models.Courses)
errMatcher func(g *WithT, err error)
}{
{
Expand All @@ -196,7 +197,7 @@ func TestClient_GetCourses(t *testing.T) {
err := mock.GockRegisterSemesterCoursesRequest("1")
g.Expect(err).ToNot(HaveOccurred())
},
coursesMatcher: func(g *WithT, courses amizone.Courses) {
coursesMatcher: func(g *WithT, courses models.Courses) {
g.Expect(courses).To(HaveLen(8))
},
errMatcher: func(g *WithT, err error) {
Expand All @@ -211,7 +212,7 @@ func TestClient_GetCourses(t *testing.T) {
err := mock.GockRegisterSemesterCoursesRequest("2")
g.Expect(err).ToNot(HaveOccurred())
},
coursesMatcher: func(g *WithT, courses amizone.Courses) {
coursesMatcher: func(g *WithT, courses models.Courses) {
g.Expect(courses).To(HaveLen(8))
},
errMatcher: func(g *WithT, err error) {
Expand All @@ -229,7 +230,7 @@ func TestClient_GetCourses(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
mock.GockRegisterUnauthenticatedPost("/CourseListSemWise", url.Values{"sem": []string{"3"}}.Encode(), strings.NewReader("<no></no>"))
},
coursesMatcher: func(g *WithT, courses amizone.Courses) {
coursesMatcher: func(g *WithT, courses models.Courses) {
g.Expect(courses).To(HaveLen(0))
},
errMatcher: func(g *WithT, err error) {
Expand Down Expand Up @@ -265,7 +266,7 @@ func TestClient_GetCurrentCourses(t *testing.T) {
name string
client *amizone.Client
setup func(g *WithT)
coursesMatcher func(g *WithT, courses amizone.Courses)
coursesMatcher func(g *WithT, courses models.Courses)
errMatcher func(g *WithT, err error)
}{
{
Expand All @@ -275,7 +276,7 @@ func TestClient_GetCurrentCourses(t *testing.T) {
err := mock.GockRegisterCurrentCoursesPage()
g.Expect(err).ToNot(HaveOccurred())
},
coursesMatcher: func(g *WithT, courses amizone.Courses) {
coursesMatcher: func(g *WithT, courses models.Courses) {
g.Expect(courses).To(HaveLen(8))
},
errMatcher: func(g *WithT, err error) {
Expand All @@ -289,7 +290,7 @@ func TestClient_GetCurrentCourses(t *testing.T) {
err := mock.GockRegisterSemWiseCoursesPage()
g.Expect(err).ToNot(HaveOccurred())
},
coursesMatcher: func(g *WithT, courses amizone.Courses) {
coursesMatcher: func(g *WithT, courses models.Courses) {
g.Expect(courses).To(HaveLen(8))
},
errMatcher: func(g *WithT, err error) {
Expand All @@ -303,7 +304,7 @@ func TestClient_GetCurrentCourses(t *testing.T) {
err := mock.GockRegisterUnauthenticatedGet("/")
g.Expect(err).ToNot(HaveOccurred())
},
coursesMatcher: func(g *WithT, courses amizone.Courses) {
coursesMatcher: func(g *WithT, courses models.Courses) {
g.Expect(courses).To(HaveLen(0))
},
errMatcher: func(g *WithT, err error) {
Expand Down Expand Up @@ -338,7 +339,7 @@ func TestClient_GetProfile(t *testing.T) {
name string
client *amizone.Client
setup func(g *WithT)
profileMatcher func(g *WithT, profile *amizone.Profile)
profileMatcher func(g *WithT, profile *models.Profile)
errMatcher func(g *WithT, err error)
}{
{
Expand All @@ -348,8 +349,8 @@ func TestClient_GetProfile(t *testing.T) {
err := mock.GockRegisterProfilePage()
g.Expect(err).ToNot(HaveOccurred())
},
profileMatcher: func(g *WithT, profile *amizone.Profile) {
g.Expect(profile).To(Equal(&amizone.Profile{
profileMatcher: func(g *WithT, profile *models.Profile) {
g.Expect(profile).To(Equal(&models.Profile{
Name: mock.StudentName,
EnrollmentNumber: mock.StudentEnrollmentNumber,
EnrollmentValidity: mock.StudentIDValidity.Time(),
Expand All @@ -371,7 +372,7 @@ func TestClient_GetProfile(t *testing.T) {
setup: func(g *WithT) {
_ = mock.GockRegisterUnauthenticatedGet("/IDCard")
},
profileMatcher: func(g *WithT, profile *amizone.Profile) {
profileMatcher: func(g *WithT, profile *models.Profile) {
g.Expect(profile).To(BeNil())
},
errMatcher: func(g *WithT, err error) {
Expand All @@ -394,7 +395,6 @@ func TestClient_GetProfile(t *testing.T) {
}
}

// Test utilities

// setupNetworking tears down any existing network mocks and sets up gock anew to intercept network
// calls and disable real network calls.
Expand Down
8 changes: 5 additions & 3 deletions amizone/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package amizone
import (
"net/http"
"time"

"github.com/ditsuke/go-amizone/amizone/models"
)

// ClientInterface is an exported interface for client to make mocking and testing more convenient.
type ClientInterface interface {
DidLogin() bool
GetAttendance() (AttendanceRecords, error)
GetClassSchedule(year int, month time.Month, date int) (ClassSchedule, error)
GetExamSchedule() (*ExamSchedule, error)
GetAttendance() (models.AttendanceRecords, error)
ClassSchedule(year int, month time.Month, date int) (models.ClassSchedule, error)
GetExamSchedule() (*models.ExaminationSchedule, error)
}

// Interface compliance constraint for Client
Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/attendance.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/PuerkitoBio/goquery"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/models"
"k8s.io/klog/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/attendance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/ditsuke/go-amizone/amizone/internal/mock"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/models"
. "github.com/onsi/gomega"
)

Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/class_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"io"
"time"

"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/models"
"k8s.io/klog/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/class_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/ditsuke/go-amizone/amizone/internal/mock"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/models"
. "github.com/onsi/gomega"
)

Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/courses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"

"github.com/PuerkitoBio/goquery"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/models"
"k8s.io/klog/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/courses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/ditsuke/go-amizone/amizone/internal/mock"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/models"
. "github.com/onsi/gomega"
)

Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/examination_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"time"

"github.com/PuerkitoBio/goquery"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/models"
"golang.org/x/text/cases"
"golang.org/x/text/language"
"k8s.io/klog/v2"
Expand Down
2 changes: 1 addition & 1 deletion amizone/internal/parse/examination_schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"testing"

"github.com/ditsuke/go-amizone/amizone/internal/mock"
"github.com/ditsuke/go-amizone/amizone/internal/models"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/models"
. "github.com/onsi/gomega"
)

Expand Down
Loading

0 comments on commit 5e4a751

Please sign in to comment.