Skip to content

Commit

Permalink
Merge pull request #56 from BenB196/staging
Browse files Browse the repository at this point in the history
Made changes to how functions are executed
  • Loading branch information
BenB196 authored Feb 6, 2021
2 parents d2f2b62 + 66d4e63 commit c437d4d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 54 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.4.1
0.4.2
43 changes: 42 additions & 1 deletion csvExport.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ffs

import (
"bytes"
"encoding/csv"
"encoding/hex"
"encoding/json"
"errors"
"github.com/spkg/bom"
"log"
Expand Down Expand Up @@ -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))
Expand Down
50 changes: 0 additions & 50 deletions ffsQuery.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package ffs

import (
"bytes"
"encoding/json"
"errors"
"net/http"
)

//Structs for FFS Queries
type Query struct {
Groups []Group `json:"groups"`
Expand Down Expand Up @@ -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
}
38 changes: 36 additions & 2 deletions jsonExport.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ffs

import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c437d4d

Please sign in to comment.