Skip to content

Commit

Permalink
Merge pull request #9 from jomla97/get-record-data-on-creation
Browse files Browse the repository at this point in the history
Get record data on creation
  • Loading branch information
jomla97 committed Jan 5, 2021
2 parents 8c30980 + e889fea commit e968657
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions v2/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,10 @@ func (r *Record) Commit() error {
return r.Create()
}

var fieldData = make(map[string]interface{})

for fieldName, value := range r.StagedChanges {
fieldData[fieldName] = value
}

var jsonData = struct {
FieldData map[string]interface{} `json:"fieldData"`
}{
fieldData,
r.StagedChanges,
}

//Create the request json body
Expand Down Expand Up @@ -127,7 +121,7 @@ func (r *Record) Commit() error {
return fmt.Errorf("failed at host: %v (%v)", jsonRes.Messages[0].Message, jsonRes.Messages[0].Code)
}

for fieldName, value := range fieldData {
for fieldName, value := range r.StagedChanges {
r.FieldData[fieldName] = value
}

Expand Down Expand Up @@ -258,16 +252,10 @@ func (r *Record) CommitFileToContainer(fieldName, filepath string) error {

//Create inserts the record into the database if it doesn't exist
func (r *Record) Create() error {
var fieldData = make(map[string]interface{})

for fieldName, value := range r.StagedChanges {
fieldData[fieldName] = value
}

var jsonData = struct {
FieldData map[string]interface{} `json:"fieldData"`
}{
fieldData,
r.StagedChanges,
}

//Create the request json body
Expand All @@ -276,7 +264,7 @@ func (r *Record) Create() error {
return fmt.Errorf("failed to marshal request body: %v", err.Error())
}

//Build and send request to the host
//Build and send request to the host to create record
req, err := http.NewRequest("POST", r.Session.Protocol+r.Session.Host+"/fmi/data/v1/databases/"+r.Session.Database+"/layouts/"+r.Layout+"/records", bytes.NewBuffer(requestBody))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+r.Session.Token)
Expand All @@ -298,12 +286,50 @@ func (r *Record) Create() error {
return fmt.Errorf("failed to decode response body as json: %v", err.Error())
}

//Check for errors in the response
if jsonRes.Messages[0].Code != "0" {
return fmt.Errorf("failed at host: %v (%v)", jsonRes.Messages[0].Message, jsonRes.Messages[0].Code)
}

//Update local record field data with staged changes
for fieldName, value := range r.StagedChanges {
r.FieldData[fieldName] = value
}

//Set the ID returned by the API
r.ID = jsonRes.Response.RecordID

//Build and send request to the host to get the default field data for the created record
req, err = http.NewRequest("GET", r.Session.Protocol+r.Session.Host+"/fmi/data/v1/databases/"+r.Session.Database+"/layouts/"+r.Layout+"/records/"+r.ID, bytes.NewBuffer([]byte{}))
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+r.Session.Token)
res, err = http.DefaultClient.Do(req)
if err != nil {
return fmt.Errorf("failed to send GET request: %v", err.Error())
}

//Read the body
resBodyBytes, err = ioutil.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("failed to read response body: %v", err.Error())
}

//Unmarshal json body
err = json.Unmarshal(resBodyBytes, &jsonRes)
if err != nil {
return fmt.Errorf("failed to decode response body as json: %v", err.Error())
}

//Check for errors in the response
if jsonRes.Messages[0].Code != "0" {
return fmt.Errorf("failed at host: %v (%v)", jsonRes.Messages[0].Message, jsonRes.Messages[0].Code)
}

//Parse the field data for the record
for fieldname, val := range jsonRes.Response.Data[0].(map[string]interface{})["fieldData"].(map[string]interface{}) {
r.FieldData[fieldname] = val
}

return nil
}

Expand Down

0 comments on commit e968657

Please sign in to comment.