Skip to content

Commit

Permalink
moved validator function to response file
Browse files Browse the repository at this point in the history
  • Loading branch information
beebeeoii committed Jan 22, 2022
1 parent 980789d commit abada5d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 24 deletions.
2 changes: 1 addition & 1 deletion FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://github.com/beebeeoii/lominus"
Name = "Lominus"
ID = "com.beebeeoii.lominus"
Version = "1.2.0"
Build = 120
Build = 121
27 changes: 4 additions & 23 deletions pkg/api/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"time"

"github.com/beebeeoii/lominus/internal/file"
logs "github.com/beebeeoii/lominus/internal/log"
)

// Folder struct is the datapack for containing details about a Folder
Expand Down Expand Up @@ -39,13 +38,13 @@ const FILE_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/%s/file?popul
const DOWNLOAD_URL_ENDPOINT = "https://luminus.nus.edu.sg/v2/api/files/file/%s/downloadurl"

// getFolderFieldsRequired is a helper function that returns a constant array with fields that a Folder response
// returned by Luminus needs
// returned by Luminus needs.
func getFolderFieldsRequired() []string {
return []string{"access", "id", "name", "isActive", "allowUpload", "subFolderCount"}
}

// getFileFieldsRequired is a helper function that returns a constant array with fields that a File response
// returned by Luminus needs
// returned by Luminus needs.
func getFileFieldsRequired() []string {
return []string{"id", "name", "lastUpdatedDate"}
}
Expand All @@ -68,7 +67,7 @@ func (req DocumentRequest) GetAllFolders() ([]Folder, error) {
}

for _, content := range rawResponse.Data {
if !isResponseValid(getFolderFieldsRequired(), content) {
if !IsResponseValid(getFolderFieldsRequired(), content) {
continue
}

Expand Down Expand Up @@ -104,7 +103,7 @@ func (req DocumentRequest) GetAllFiles() ([]File, error) {
}

for _, content := range rawResponse.Data {
if !isResponseValid(getFileFieldsRequired(), content) {
if !IsResponseValid(getFileFieldsRequired(), content) {
continue
}

Expand Down Expand Up @@ -215,21 +214,3 @@ func (req DocumentRequest) Download(filePath string) error {

return nil
}

// isResponseValid is a helper function that checks if a Folder/File response is valid.
// It checks if the response contains the required fields required.
func isResponseValid(fieldsRequired []string, response map[string]interface{}) bool {
isValid := true
for _, field := range fieldsRequired {
_, exists := response[field]

if !exists {
isValid = false
break
}
}

logs.Logger.Debugln(isValid, fieldsRequired)

return isValid
}
16 changes: 16 additions & 0 deletions pkg/api/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,19 @@ func (req Request) GetRawResponse(res interface{}) error {

return err
}

// isResponseValid is a helper function that checks if a Folder/File response is valid.
// It checks if the response contains the required fields required.
func IsResponseValid(fieldsRequired []string, response map[string]interface{}) bool {
isValid := true
for _, field := range fieldsRequired {
_, exists := response[field]

if !exists {
isValid = false
break
}
}

return isValid
}

0 comments on commit abada5d

Please sign in to comment.