示例#1
0
文件: jira.go 项目: wcharczuk/jarvis
// GetJiraIssue gets the metadata for a given issueID.
func GetJiraIssue(user, password, host, issueID string) (*JiraIssue, error) {
	res, err := core.NewExternalRequest().AsGet().WithBasicAuth(user, password).WithScheme("https").WithHost(host).WithPathf("rest/api/2/issue/%s", issueID).FetchRawResponse()
	if err != nil {
		return nil, err
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)

	var je JiraError
	err = json.Unmarshal(body, &je)
	if err == nil && len(je.ErrorMessages) != 0 {
		return nil, exception.Newf("Errors returned from jira: %s\n", je.ErrorMessages[0])
	}

	var ji JiraIssue
	err = json.Unmarshal(body, &ji)
	return &ji, err
}
示例#2
0
// StockPrice returns stock price info from Yahoo for the given tickers.
func StockPrice(tickers []string) ([]StockInfo, error) {
	if len(tickers) == 0 {
		return []StockInfo{}, nil
	}

	rawResults, meta, resErr := core.NewExternalRequest().AsGet().
		WithURL("http://download.finance.yahoo.com/d/quotes.csv").
		WithQueryString("s", strings.Join(tickers, "+")).
		WithQueryString("f", stockInfoFormat()).
		FetchStringWithMeta()

	if resErr != nil {
		return []StockInfo{}, resErr
	}

	if meta.StatusCode != http.StatusOK {
		return []StockInfo{}, exception.New("Non (200) response from pricing provider.")
	}

	var results []StockInfo

	scanner := bufio.NewScanner(strings.NewReader(rawResults))
	scanner.Split(bufio.ScanLines)

	var err error
	for scanner.Scan() {
		si := &StockInfo{}
		si.Format = stockInfoFormat()
		line := scanner.Text()
		si.RawResults = line

		err = si.Parse(line)
		if err == nil && !si.IsZero() {
			results = append(results, *si)
		}
	}
	return results, nil
}