예제 #1
0
파일: bksv.go 프로젝트: skypies/complaints
func PostComplaint3(client *http.Client, c types.Complaint) (*types.Submission, error) {
	// Initialize a new submission object, inheriting from previous
	s := types.Submission{
		Attempts: c.Submission.Attempts + 1,
		Log:      c.Submission.Log + fmt.Sprintf("\n--------=={ PostComplaint %s }==-----------\n", time.Now()),
		Key:      c.Submission.Key,
		T:        time.Now().UTC(),
		Outcome:  types.SubmissionFailed, // Be pessimistic right up until the end
	}

	s.Log += fmt.Sprintf("----{ time: %s }----\n  --{ keyless submission }--\n", s.T)

	vals := PopulateForm(c, "")
	s.Log += "Submitting these vals:-\n"
	for k, v := range vals {
		s.Log += fmt.Sprintf(" * %-20.20s: %v\n", k, v)
	}

	resp, err := client.PostForm("https://"+bksvHost+bksvPath, vals)
	s.D = time.Since(s.T)
	if err != nil {
		s.Log += fmt.Sprintf("ComplaintPOST: Posting error (dur=%s): %v\n", s.D, err)
		return &s, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	s.Log += fmt.Sprintf("ComplaintPOST: HTTP response '%s'\n", resp.Status)
	s.Response = []byte(body)
	if resp.StatusCode >= 400 {
		s.Log += fmt.Sprintf("ComplaintPOST: HTTP Body:-\n%s\n--\n", body)
		return &s, fmt.Errorf("ComplaintPOST: HTTP err %s", resp.Status)
	}

	var jsonMap map[string]interface{}
	if err := json.Unmarshal([]byte(body), &jsonMap); err != nil {
		s.Log += fmt.Sprintf("ComplaintPOST: JSON unmarshal '%v'\nBody:-\n%s\n--\n", err, body)
		return &s, 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)
		}
		*/
	}

	indentedBytes, _ := json.MarshalIndent(jsonMap, "", "  ")
	s.Log += "\n-- JsonMap:-\n" + string(indentedBytes) + "\n--\n"

	/*
	   -- JsonMap:-
	   {
	     "body": "Thank you. We have received your complaint.",
	     "details": {
	       "address_id": 1967,
	       "complaint_id": 1.120211e+06,
	       "device_id": 1,
	       "person_id": 2083
	     },
	     "result": "1",
	     "title": "Complaint Received"
	   }
	   --
	*/

	v := jsonMap["result"]
	if v == nil {
		s.Log += fmt.Sprintf("ComplaintPOST: json no 'result'\n")
		return &s, fmt.Errorf("ComplaintPOST: jsonmap had no 'result'")
	}

	result := v.(string)
	if result != "1" {
		s.Log += fmt.Sprintf("Json result not '1'\n")
		return &s, fmt.Errorf("ComplaintPOST: result='%s'", result)
	}

	s.Log += "Json Success !\n"
	s.Outcome = types.SubmissionAccepted

	// Extract the foreign key for this complaint
	detailsMap := jsonMap["details"].(map[string]interface{})
	s.Key = fmt.Sprintf("%.0f", detailsMap["complaint_id"].(float64))

	return &s, nil
}
예제 #2
0
파일: bksv.go 프로젝트: skypies/complaints
func PostComplaint2(client *http.Client, c types.Complaint) (*types.Submission, error) {
	// Initialize a new submission object, inheriting from previous
	s := types.Submission{
		Attempts: c.Submission.Attempts + 1,
		Log:      c.Submission.Log + fmt.Sprintf("\n--------=={ %s }==-----------\n", time.Now()),
		Key:      c.Submission.Key,
		T:        time.Now().UTC(),
		Outcome:  types.SubmissionFailed, // Be pessimistic right up until the end
	}

	// Usually, this is a first attempt, so we need to get a key
	if s.Key == "" {
		debug, key, err := GetSubmitkey(client)
		s.Log += debug
		if err != nil {
			s.Log += fmt.Sprintf("GetSubmitKey err: %v", err)
			return &s, err
		}
		s.Key = key
		s.Log += fmt.Sprintf("----{ time: %s }----\n  --{ key: %s }--\n", s.T, s.Key)
	} else {
		s.Log += fmt.Sprintf("----{ time: %s }----\n  --{ pre-existing key: %s }--\n", s.T, s.Key)
	}

	vals := PopulateForm(c, s.Key)
	s.Log += "Submitting these vals:-\n"
	for k, v := range vals {
		s.Log += fmt.Sprintf(" * %-20.20s: %v\n", k, v)
	}

	resp, err := client.PostForm("https://"+bksvHost+bksvPath, vals)
	if err != nil {
		return &s, err
	}

	defer resp.Body.Close()
	body, _ := ioutil.ReadAll(resp.Body)
	s.Response = []byte(body)
	if resp.StatusCode >= 400 {
		s.Log += fmt.Sprintf("ComplaintPOST: HTTP err '%s'\nBody:-\n%s\n--\n", resp.Status, body)
		return &s, fmt.Errorf("ComplaintPOST: HTTP err %s", resp.Status)
	}

	var jsonMap map[string]interface{}
	if err := json.Unmarshal([]byte(body), &jsonMap); err != nil {
		s.Log += fmt.Sprintf("ComplaintPOST: JSON unmarshal '%v'\nBody:-\n%s\n--\n", err, body)
		return &s, 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)
		}
		*/
	}

	indentedBytes, _ := json.MarshalIndent(jsonMap, "", "  ")
	s.Log += "\n-- JsonMap:-\n" + string(indentedBytes) + "\n--\n"

	v := jsonMap["result"]
	if v == nil {
		s.Log += fmt.Sprintf("ComplaintPOST: json no 'result'\n")
		return &s, fmt.Errorf("ComplaintPOST: jsonmap had no 'result'")
	}

	result := v.(string)
	if result != "1" {
		s.Log += fmt.Sprintf("Json result not '1'\n")
		return &s, fmt.Errorf("ComplaintPOST: result='%s'", result)
	}

	s.Log += "Json Success !\n"
	s.Outcome = types.SubmissionAccepted

	return &s, nil
}