Skip to content

Commit

Permalink
Merge pull request #60 from BenB196/staging
Browse files Browse the repository at this point in the history
Fixed issues with json export
  • Loading branch information
BenB196 authored Feb 6, 2021
2 parents 67778ac + cc83aaf commit adabaaf
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 34 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

.idea/
secrets.go
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.3
0.4.4
21 changes: 21 additions & 0 deletions auth_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion ffsQuery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
56 changes: 24 additions & 32 deletions jsonExport.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
)

Expand Down Expand Up @@ -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"`
Expand All @@ -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"`
}
Expand All @@ -117,30 +118,30 @@ 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
req, err := http.NewRequest("POST", ffsURI, bytes.NewReader(ffsQuery))

//Handle request errors
if err != nil {
return nil, nil, err
return nil, "", err
}

//Set request headers
Expand All @@ -152,31 +153,31 @@ 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
defer resp.Body.Close()

//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 {
Expand All @@ -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
}
35 changes: 35 additions & 0 deletions jsonExport_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit adabaaf

Please sign in to comment.