Пример #1
0
func (self ValidationError) Respond(c httpcontext.HTTPContextLogger) error {
	if self.Fields != nil {
		c.Resp().Header().Set("Content-Type", "application/json; charset=UTF-8")
	}
	if self.Status != 0 {
		c.Resp().WriteHeader(self.Status)
	}
	return json.NewEncoder(c.Resp()).Encode(self)
}
Пример #2
0
func DoRequest(c ServiceConnector, method, service, path string, token AccessToken, body interface{}) (request *http.Request, response *http.Response, err error) {
	waitTime := time.Millisecond * 100
	for waitTime < time.Second*10 {
		buf := new(bytes.Buffer)
		if body != nil {
			if err = json.NewEncoder(buf).Encode(body); err != nil {
				return
			}
		}

		request, err = http.NewRequest(method, fmt.Sprintf("%v/%v", service, path), buf)
		if err != nil {
			return
		}

		if token != nil {
			var encoded string
			encoded, err = token.EncodeSelf()
			if err != nil {
				return
			}
			request.Header.Add("Authorization", fmt.Sprintf("Bearer %v", encoded))
		}

		if method == "POST" || method == "PUT" {
			request.Header.Add("Content-Type", "application/json")
		}

		request.Header.Add("X-API-Version", fmt.Sprint(MaxAPIVersion))

		response, err = c.Client().Do(request)
		if err == nil && response.StatusCode < 500 {
			break
		}
		time.Sleep(waitTime)
		waitTime = waitTime * 2
	}
	if err != nil {
		return
	}

	newBody := &bytes.Buffer{}
	if _, err = io.Copy(newBody, response.Body); err != nil {
		return
	}
	if err = response.Body.Close(); err != nil {
		return
	}
	response.Body = ioutil.NopCloser(newBody)
	return
}
Пример #3
0
func (self DataResp) Render(c HTTPContextLogger) error {
	if self.Data == nil {
		return nil
	}
	if self.Filename != "" {
		c.Resp().Header().Set("Content-disposition", "attachment; filename="+self.Filename)
	}
	c.Resp().Header().Set("Content-Type", self.ContentType)
	switch self.ContentType {
	case ContentExcelCSV:
		if self.Status != 0 {
			c.Resp().WriteHeader(self.Status)
		}
		fmt.Fprintf(c.Resp(), "sep=\t\n")
		writer := csv.NewWriter(c.Resp())
		writer.Comma = '\t'
		titleRow := []string{self.ReportName}
		for k, v := range self.Filters {
			titleRow = append(titleRow, fmt.Sprintf("%s=%s", k, v[0]))
		}
		writer.Write(titleRow)
		err := writer.Write(self.Headers)
		if err != nil {
			return err
		}
		for row := range self.Data {
			vals := make([]string, 0, len(self.Headers))
			for index := range self.Headers {
				switch row[index].(type) {
				default:
					vals = append(vals, fmt.Sprintf("%v", row[index]))
				case float64:
					vals = append(vals, fmt.Sprintf("%.2f", row[index]))
				}
			}
			err := writer.Write(vals)
			if err != nil {
				return err
			}
		}
		writer.Flush()
		return writer.Error()
	case ContentHTML:
		fmt.Fprintf(c.Resp(), "<html><body><table><thead><tr>")
		for _, k := range self.Headers {
			fmt.Fprintf(c.Resp(), "<th>%v</th>", k)
		}
		fmt.Fprintf(c.Resp(), "</tr></thead><tbody>")
		for row := range self.Data {
			fmt.Fprintf(c.Resp(), "<tr>")
			for _, v := range row {
				switch v.(type) {
				default:
					fmt.Fprintf(c.Resp(), "<td>%v</td>", v)
				case float64:
					fmt.Fprintf(c.Resp(), "<td>%.2f</td>", v)
				}
			}
			fmt.Fprintf(c.Resp(), "</tr>")
		}
		fmt.Fprintf(c.Resp(), "</tbody></body></html>")
	case ContentJSON:
		// I dont know a way of creating json, and streaming it to the user.
		var resp []map[string]interface{}
		for row := range self.Data {
			m := map[string]interface{}{}
			for k, v := range self.Headers {
				m[v] = row[k]
			}
			resp = append(resp, m)
		}
		return json.NewEncoder(c.Resp()).Encode(resp)

	case ContentJSONStream:
		for row := range self.Data {
			m := map[string]interface{}{}
			for k, v := range self.Headers {
				m[v] = row[k]
			}
			err := json.NewEncoder(c.Resp()).Encode(m)
			if err != nil {
				return err
			}
		}
	}
	return fmt.Errorf("Unknown content type %#v", self.ContentType)
}