func PostComplaint(client *http.Client, p types.ComplainerProfile, c types.Complaint) (string, error) { first, last := p.SplitName() addr := p.GetStructuredAddress() if c.Activity == "" { c.Activity = "Loud noise" } debug, submitkey, err := GetSubmitkey(client) if err != nil { return debug, err } debug += fmt.Sprintf("We got submitkey=%s\n", submitkey) // {{{ Populate form vals := url.Values{ "response": {"json"}, "contactmethod": {"App"}, "app_key": {"TUC8uDJMooVMvf7hew93nhUGcWgw"}, "caller_code": {p.CallerCode}, "name": {first}, "surname": {last}, "address1": {addr.Street}, "address2": {""}, "zipcode": {addr.Zip}, "city": {addr.City}, "state": {addr.State}, "email": {p.EmailAddress}, "airports": {"KSFO"}, // KOAK, KSJC, KSAN "month": {date.InPdt(c.Timestamp).Format("1")}, "day": {date.InPdt(c.Timestamp).Format("2")}, "year": {date.InPdt(c.Timestamp).Format("2006")}, "hour": {date.InPdt(c.Timestamp).Format("15")}, "min": {date.InPdt(c.Timestamp).Format("4")}, "aircraftcategory": {"J"}, "eventtype": {"Loud noise"}, // perhaps map c.Activity to something ? "comments": {c.Description}, "responserequired": {"N"}, "enquirytype": {"C"}, "submit": {"Submit complaint"}, "submitkey": {submitkey}, "nowebtrak": {"1"}, "defaulttime": {"0"}, "webtraklinkback": {""}, "title": {""}, "homephone": {""}, "workphone": {""}, "cellphone": {""}, } if c.AircraftOverhead.FlightNumber != "" { vals.Add("acid", c.AircraftOverhead.Callsign) vals.Add("aacode", c.AircraftOverhead.Id2) vals.Add("tailnumber", c.AircraftOverhead.Registration) vals.Add("aircrafttype", c.AircraftOverhead.EquipType) //vals.Add("adflag", "??") // Operation type (A, D or O for Arr, Dept or Overflight) //vals.Add("beacon", "??") // SSR code (eg 210) } // }}} debug += "Submitting these vals:-\n" for k, v := range vals { debug += fmt.Sprintf(" * %-20.20s: %v\n", k, v) } if resp, err := client.PostForm("https://"+bksvHost+bksvPath, vals); err != nil { return debug, err } else { defer resp.Body.Close() body, _ := ioutil.ReadAll(resp.Body) if resp.StatusCode >= 400 { debug += fmt.Sprintf("ComplaintPOST: HTTP err '%s'\nBody:-\n%s\n--\n", resp.Status, body) return debug, fmt.Errorf("ComplaintPOST: HTTP err %s", resp.Status) } var jsonMap map[string]interface{} if err := json.Unmarshal([]byte(body), &jsonMap); err != nil { debug += fmt.Sprintf("ComplaintPOST: JSON unmarshal '%v'\nBody:-\n%s\n--\n", err, body) return debug, fmt.Errorf("ComplaintPOST: JSON unmarshal %v", err) /* Fall back ? if !regexp.MustCompile(`(?i:received your complaint)`).MatchString(string(body)) { debug += fmt.Sprintf("BKSV body ...\n%s\n------\n", string(body)) return debug,fmt.Errorf("Returned response did not say 'received your complaint'") } else { debug += "Success !\n"+string(body) } */ } else if v := jsonMap["result"]; v == nil { return debug, fmt.Errorf("ComplaintPOST: jsonmap had no 'result'.\nBody:-\n%s\n--\n", body) } else { result := v.(string) if result == "1" { debug += "Json Success !\n" } else { debug += fmt.Sprintf("Json result not '1':-\n%#v\n--\n", jsonMap) return debug, fmt.Errorf("ComplaintPOST: result='%s'", result) } } } return debug, nil }
func PopulateForm(c types.Complaint, submitkey string) url.Values { first, last := c.Profile.SplitName() addr := c.Profile.GetStructuredAddress() if c.Activity == "" { c.Activity = "Loud noise" } vals := url.Values{ "response": {"json"}, "contactmethod": {"App"}, "apiKey": {"399734e01c8cd5c21205599689cc77f2a50467f28e6f5d58a69f2b097d71b839c20e0051175107e74130ae9a3bbaccbe51ec5742e6ca3e51ff40cc1a8f401009"}, "caller_code": {c.Profile.CallerCode}, "name": {first}, "surname": {last}, "address1": {addr.Street}, "address2": {""}, "zipcode": {addr.Zip}, "city": {addr.City}, "state": {addr.State}, "email": {c.Profile.EmailAddress}, "airports": {"KSFO"}, // KOAK, KSJC, KSAN "month": {date.InPdt(c.Timestamp).Format("1")}, "day": {date.InPdt(c.Timestamp).Format("2")}, "year": {date.InPdt(c.Timestamp).Format("2006")}, "hour": {date.InPdt(c.Timestamp).Format("15")}, "min": {date.InPdt(c.Timestamp).Format("4")}, "aircraftcategory": {"J"}, "eventtype": {"Loud noise"}, // perhaps map c.Activity to something ? "comments": {c.Description}, "responserequired": {"N"}, "enquirytype": {"C"}, "submit": {"Submit complaint"}, //"submitkey": {submitkey}, "nowebtrak": {"1"}, "defaulttime": {"0"}, "webtraklinkback": {""}, "title": {""}, "homephone": {""}, "workphone": {""}, "cellphone": {""}, "browser_name": {c.Browser.Name}, "browser_version": {c.Browser.Version}, "browser_vendor": {c.Browser.Vendor}, "browser_uuid": {c.Browser.UUID}, "browser_platform": {c.Browser.Platform}, } if c.AircraftOverhead.FlightNumber != "" { vals.Add("acid", c.AircraftOverhead.Callsign) vals.Add("aacode", c.AircraftOverhead.Id2) vals.Add("tailnumber", c.AircraftOverhead.Registration) vals.Add("aircrafttype", c.AircraftOverhead.EquipType) //vals.Add("adflag", "??") // Operation type (A, D or O for Arr, Dept or Overflight) //vals.Add("beacon", "??") // Squawk SSR code (eg 2100) } return vals }