Beispiel #1
0
// Send helper for execute command work
func (c *Command) Send(url string) (string, error) {
	hr := httpRequest.New(url)
	if c.GetMethod() == httpRequest.CMethodGET {
		return hr.Get()
	}
	return "", nil
}
Beispiel #2
0
// Execute receive and return actual course
func (ac *ActionCourse) Execute() (string, error) {
	url := fmt.Sprintf(cCourseAPIURL, time.Now().Format(cTimeFormatLayout))
	hr := httpRequest.New(url)
	resp, err := hr.Get()
	if err != nil {
		return "", err
	}

	c := Currencies{}
	reader := bytes.NewReader([]byte(resp))
	decoder := xml.NewDecoder(reader)
	decoder.CharsetReader = charset.NewReader
	err = decoder.Decode(&c)
	if err != nil {
		return "", err
	}
	log.Infof("Receive currencies: %v", c)

	result := ""
	for _, cur := range c.Content {
		if cur.CharCode == "EUR" || cur.CharCode == "USD" {
			result += cur.CharCode + " - " + cur.Value + " руб.\n"
		}
	}

	return result, nil
}