From cc83aaf081d6da24ed7523000720f055c43fe7cf Mon Sep 17 00:00:00 2001 From: BenB196 <8277432+BenB196@users.noreply.github.com> Date: Sat, 6 Feb 2021 14:56:42 -0500 Subject: [PATCH] Fixed issues with json export Fixed added new tests --- .gitignore | 1 + VERSION | 2 +- auth_test.go | 21 +++++++++++++++++ ffsQuery.go | 2 +- jsonExport.go | 56 ++++++++++++++++++++-------------------------- jsonExport_test.go | 35 +++++++++++++++++++++++++++++ 6 files changed, 83 insertions(+), 34 deletions(-) create mode 100644 auth_test.go create mode 100644 jsonExport_test.go diff --git a/.gitignore b/.gitignore index df30947..b535fac 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .idea/ +secrets.go diff --git a/VERSION b/VERSION index 70d5b25..b300caa 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.3 \ No newline at end of file +0.4.4 \ No newline at end of file diff --git a/auth_test.go b/auth_test.go new file mode 100644 index 0000000..ae0f89a --- /dev/null +++ b/auth_test.go @@ -0,0 +1,21 @@ +package ffs + +import ( + "log" + "testing" +) + +func TestGetAuthData(t *testing.T) { + authData, err := GetAuthData(authUri, username, password) + + if err != nil { + t.Error(err) + } + + if authData.Error != "" { + t.Error(authData.Error) + } + + log.Println("Warnings: " + authData.Warnings) + log.Println("Auth Token: " + authData.Data.V3UserToken) +} \ No newline at end of file diff --git a/ffsQuery.go b/ffsQuery.go index f3b0008..d61f6f5 100644 --- a/ffsQuery.go +++ b/ffsQuery.go @@ -6,7 +6,7 @@ type Query struct { GroupClause string `json:"groupClause,omitempty"` PgNum int `json:"pgNum,omitempty"` PgSize int `json:"pgSize,omitempty"` - PgToken *string `json:"pgToken,omitempty"` + PgToken string `json:"pgToken"` SrtDir string `json:"srtDir,omitempty"` SrtKey string `json:"srtKey,omitempty"` } diff --git a/jsonExport.go b/jsonExport.go index 6abcd44..a01dcda 100644 --- a/jsonExport.go +++ b/jsonExport.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "io/ioutil" + "log" "net/http" ) @@ -64,11 +65,11 @@ type JsonFileEvent struct { RemovableMediaVolumeName []string `json:"removableMediaVolumeName,omitempty"` Sha256Checksum string `json:"sha256Checksum,omitempty"` Shared string `json:"shared,omitempty"` - SharedWith []SharedWith `json:"sharedWith,omitempty"` + SharedWith []SharedWith `json:"sharedWith,omitempty"` SharingTypeAdded []string `json:"sharingTypeAdded,omitempty"` Source string `json:"source,omitempty"` SyncDestination string `json:"syncDestination,omitempty"` - SyncDestinationUsername []string `json:"syncDestinationUsername,omitempty"` + SyncDestinationUsername []string `json:"syncDestinationUsername,omitempty"` TabUrl string `json:"tabUrl,omitempty"` Tabs []Tab `json:"tabs,omitempty"` Trusted *bool `json:"trusted,omitempty"` @@ -93,7 +94,7 @@ type Tab struct { type JsonFileEventResponse struct { FileEvents []JsonFileEvent `json:"fileEvents,omitempty"` - NextPgToken *string `json:"nextPgToken,omitempty"` + NextPgToken string `json:"nextPgToken,omitempty"` Problems []QueryProblem `json:"problems,omitempty"` TotalCount *int64 `json:"totalCount,omitempty"` } @@ -117,22 +118,22 @@ func GetJsonFileEventResponse(resp *http.Response) (*JsonFileEventResponse, erro return &eventResponse, nil } -func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken *string) (*[]JsonFileEvent, *string, error) { +func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken string) (*[]JsonFileEvent, string, error) { var jsonFileEvents []JsonFileEvent - if pgToken != nil && *pgToken != "" { + if pgToken != "" { query.PgToken = pgToken } //Validate jsonQuery is valid JSON ffsQuery, err := json.Marshal(query) if err != nil { - return nil, nil, errors.New("jsonQuery is not in a valid json format") + return nil, "", errors.New("jsonQuery is not in a valid json format") } //Make sure authData token is not "" if authData.Data.V3UserToken == "" { - return nil, nil, errors.New("authData cannot be nil") + return nil, "", errors.New("authData cannot be nil") } //Query ffsURI with authData API token and jsonQuery body @@ -140,7 +141,7 @@ func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken *s //Handle request errors if err != nil { - return nil, nil, err + return nil, "", err } //Set request headers @@ -152,7 +153,7 @@ func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken *s //Handle response errors if err != nil { - return nil, nil, err + return nil, "", err } //defer body close @@ -160,23 +161,23 @@ func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken *s //Make sure http status code is 200 if resp.StatusCode != http.StatusOK { - return nil, nil, errors.New("Error with gathering file events POST: " + resp.Status) + return nil, "", errors.New("Error with gathering file events POST: " + resp.Status) } fileEventResponse, err := GetJsonFileEventResponse(resp) if err != nil { - return nil, nil, err + return nil, "", err } if fileEventResponse.Problems != nil { problems, err := json.Marshal(fileEventResponse.Problems) if err != nil { - return nil, nil, err + return nil, "", err } - return nil, nil, errors.New(string(problems)) + return nil, "", errors.New(string(problems)) } if len(fileEventResponse.FileEvents) == 0 { @@ -185,31 +186,22 @@ func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken *s jsonFileEvents = append(jsonFileEvents, fileEventResponse.FileEvents...) } - var nextPgToken *string - - if fileEventResponse.NextPgToken != nil && *fileEventResponse.NextPgToken != "" { - nextPgToken = fileEventResponse.NextPgToken - } - var nextJsonFileEvents *[]JsonFileEvent - for { - if nextPgToken == nil || *nextPgToken == "" { - break - } else { - nextJsonFileEvents, nextPgToken, err = GetJsonFileEvents(authData, ffsURI, query, pgToken) + if fileEventResponse.NextPgToken != "" { + log.Print("Next Page Token: ") + log.Println(fileEventResponse.NextPgToken) - if err != nil { - return nil, nil, err - } + nextJsonFileEvents, _, err = GetJsonFileEvents(authData, ffsURI, query, fileEventResponse.NextPgToken) - if nextJsonFileEvents != nil && len(*nextJsonFileEvents) != 0 { - jsonFileEvents = append(jsonFileEvents, *nextJsonFileEvents...) - } + if err != nil { + return nil, "", err + } - nextJsonFileEvents = nil + if nextJsonFileEvents != nil && len(*nextJsonFileEvents) != 0 { + jsonFileEvents = append(jsonFileEvents, *nextJsonFileEvents...) } } - return &jsonFileEvents, nil, nil + return &jsonFileEvents, "", nil } diff --git a/jsonExport_test.go b/jsonExport_test.go new file mode 100644 index 0000000..d0d6bd5 --- /dev/null +++ b/jsonExport_test.go @@ -0,0 +1,35 @@ +package ffs + +import ( + "log" + "testing" +) + +func TestGetJsonFileEvents(t *testing.T) { + authData, err := GetAuthData(authUri, username, password) + + if err != nil { + t.Error(err) + } + + if authData.Error != "" { + t.Error(authData.Error) + } + + log.Println("Warnings: " + authData.Warnings) + log.Println("Auth Token: " + authData.Data.V3UserToken) + + jsonFileEvents, nextPgToken, err := GetJsonFileEvents(*authData, ffsUri, jsonQuery, "") + + if err != nil { + t.Error(err) + } + + log.Println(nextPgToken) + + if jsonFileEvents != nil && len(*jsonFileEvents) > 0 { + for _, event := range *jsonFileEvents { + log.Println(event) + } + } +}