diff --git a/VERSION b/VERSION index 44bb5d1..f7abe27 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.4.1 \ No newline at end of file +0.4.2 \ No newline at end of file diff --git a/csvExport.go b/csvExport.go index 659776e..25f62f6 100644 --- a/csvExport.go +++ b/csvExport.go @@ -1,8 +1,10 @@ package ffs import ( + "bytes" "encoding/csv" "encoding/hex" + "encoding/json" "errors" "github.com/spkg/bom" "log" @@ -507,7 +509,46 @@ getCsvFileEvents - Function to get the actual event records from FFS This function contains a panic if the csv columns do not match the currently specified list. This is to prevent data from being messed up during parsing. */ -func GetCsvFileEvents(resp *http.Response) (*[]CsvFileEvent, error) { +func GetCsvFileEvents(authData AuthData, ffsURI string, query Query) (*[]CsvFileEvent, error) { + //Validate jsonQuery is valid JSON + ffsQuery, err := json.Marshal(query) + if err != nil { + return nil, errors.New("jsonQuery is not in a valid json format") + } + + //Make sure authData token is not "" + if authData.Data.V3UserToken == "" { + 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, err + } + + //Set request headers + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "v3_user_token "+authData.Data.V3UserToken) + + //Get Response + resp, err := http.DefaultClient.Do(req) + + //Handle response errors + if err != nil { + return nil, err + } + + //defer body close + defer resp.Body.Close() + + //Make sure http status code is 200 + if resp.StatusCode != http.StatusOK { + return nil, errors.New("Error with gathering file events POST: " + resp.Status) + } + //Read Response Body as CSV //reader := csv.NewReader(resp.Body) reader := csv.NewReader(bom.NewReader(resp.Body)) diff --git a/ffsQuery.go b/ffsQuery.go index f0f4822..f3b0008 100644 --- a/ffsQuery.go +++ b/ffsQuery.go @@ -1,12 +1,5 @@ package ffs -import ( - "bytes" - "encoding/json" - "errors" - "net/http" -) - //Structs for FFS Queries type Query struct { Groups []Group `json:"groups"` @@ -34,46 +27,3 @@ type QueryProblem struct { Description string `json:"description,omitempty"` Type string `json:"type,omitempty"` } - -func ExecQuery(authData AuthData, ffsURI string, query Query) (*http.Response, error) { - //Validate jsonQuery is valid JSON - ffsQuery, err := json.Marshal(query) - if err != nil { - return nil, errors.New("jsonQuery is not in a valid json format") - } - - //Make sure authData token is not "" - if authData.Data.V3UserToken == "" { - 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, err - } - - //Set request headers - req.Header.Set("Content-Type", "application/json") - req.Header.Set("Authorization", "v3_user_token "+authData.Data.V3UserToken) - - //Get Response - resp, err := http.DefaultClient.Do(req) - - //Handle response errors - if err != nil { - return nil, err - } - - //defer body close - defer resp.Body.Close() - - //Make sure http status code is 200 - if resp.StatusCode != http.StatusOK { - return nil, errors.New("Error with gathering file events POST: " + resp.Status) - } - - return resp, nil -} diff --git a/jsonExport.go b/jsonExport.go index 2d8a703..5784d66 100644 --- a/jsonExport.go +++ b/jsonExport.go @@ -1,6 +1,7 @@ package ffs import ( + "bytes" "encoding/json" "errors" "io/ioutil" @@ -123,13 +124,46 @@ func GetJsonFileEvents(authData AuthData, ffsURI string, query Query, pgToken *s query.PgToken = pgToken } - eventQuery, err := ExecQuery(authData, ffsURI, query) + //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") + } + + //Make sure authData token is not "" + if authData.Data.V3UserToken == "" { + return nil, 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 + } + + //Set request headers + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "v3_user_token "+authData.Data.V3UserToken) + + //Get Response + resp, err := http.DefaultClient.Do(req) + //Handle response errors if err != nil { return nil, nil, err } - fileEventResponse, err := GetJsonFileEventResponse(eventQuery) + //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) + } + + fileEventResponse, err := GetJsonFileEventResponse(resp) if err != nil { return nil, nil, err