Пример #1
0
Файл: main.go Проект: khaf/rest
func main() {

	var err error

	// You may want to see the full client debug.
	// rest.Debug = true

	// Destination variable.
	buf := rest.Response{}

	// A nice gopher image.
	requestURL := "https://api.twitter.com/v1/foo.json"

	// We don't need any GET vars.
	requestVariables := url.Values{}

	// Let's pass buf's address ad first argument.
	err = rest.Get(&buf, requestURL, requestVariables)

	// Was there any error?
	if err == nil {

		// Printing response dump.
		log.Printf("Got response!")
		log.Printf("Response code: %d", buf.StatusCode)
		log.Printf("Response protocol version: %s", buf.Proto)
		log.Printf("Response length: %d", buf.ContentLength)
		log.Printf("Response header: %v", buf.Header)
		log.Printf("Response body: %s", string(buf.Body))
	} else {
		// Yes, we had an error.
		log.Printf("Request failed: %s", err.Error())
	}

}
Пример #2
0
func main() {
	var opts Options
	parser := flags.NewParser(&opts, flags.Default)

	parser.Name = "mymine"
	parser.Usage = "[OPTIONS]"

	_, err := parser.Parse()
	if err != nil {
		os.Exit(0)
	}

	if opts.Version != nil {
		showVersion()
		os.Exit(0)
	}

	redmineUrl := getEnvVar("REDMINE_URL")
	if redmineUrl == "" {
		fmt.Println("REDMINE_URL is not specified.")
		os.Exit(0)
	}

	if opts.Open != nil {
		url := redmineUrl + "issues/" + strconv.Itoa(opts.Open[0])
		openUrlByBrowser(url)
		os.Exit(0)
	}

	redmineApiKey := getEnvVar("REDMINE_API_KEY")
	if redmineApiKey == "" {
		fmt.Println("REDMINE_API_KEY is not specified.")
		os.Exit(0)
	}

	project_id := 946 // TODO: should be an option
	request := redmineUrl + "issues.json?key=" + redmineApiKey + "&limit=100&project_id=" + strconv.Itoa(project_id) + "&status_id=open"
	fmt.Println("request =", request)
	fmt.Println("fetching information...")
	var buf map[string]interface{}
	rest.Get(&buf, request, nil)

	issues := buf["issues"].([]interface{})
	for _, v := range issues {
		issue := v.(map[string]interface{})

		var assigned_to string
		tracker := issue["tracker"].(map[string]interface{})
		if issue["assigned_to"] != nil {
			assigned_to_map := issue["assigned_to"].(map[string]interface{})
			assigned_to = assigned_to_map["name"].(string)
		} else {
			assigned_to = "not_assigned"
		}
		id := int(issue["id"].(float64))
		status := issue["status"].(map[string]interface{})
		fmt.Printf("%s #%d %s %s %s\n", tracker["name"], id, status["name"], issue["subject"], assigned_to)
	}
}
Пример #3
0
func main() {
	var opts Options
	parser := flags.NewParser(&opts, flags.Default)

	parser.Name = "mymine"
	parser.Usage = "[OPTIONS]"

	_, err := parser.Parse()
	if err != nil {
		os.Exit(0)
	}

	if opts.Version != nil {
		showVersion()
		os.Exit(0)
	}

	redmineUrl := getEnvVar("REDMINE_URL")
	if redmineUrl == "" {
		fmt.Println("REDMINE_URL is not specified.")
		os.Exit(0)
	}

	if opts.Open != nil {
		url := redmineUrl + "issues/" + strconv.Itoa(opts.Open[0])
		openUrlByBrowser(url)
		os.Exit(0)
	}

	redmineApiKey := getEnvVar("REDMINE_API_KEY")
	if redmineApiKey == "" {
		fmt.Println("REDMINE_API_KEY is not specified.")
		os.Exit(0)
	}

	request := redmineUrl + "issues.json?key=" + redmineApiKey + "&status_id=open&assigned_to_id=me&limit=100"
	fmt.Println("request =", request)
	fmt.Println("fetching information...")
	var buf map[string]interface{}
	rest.Get(&buf, request, nil)

	issues := buf["issues"].([]interface{})
	for _, v := range issues {
		issue := v.(map[string]interface{})

		id := int(issue["id"].(float64))
		status := issue["status"].(map[string]interface{})
		fmt.Printf("[#%d] %11s %s\n", id, status["name"], issue["subject"])
	}
}
Пример #4
0
Файл: main.go Проект: khaf/rest
func main() {

	var err error

	// You may want to see the full client debug.
	// rest.Debug = true

	// Destination variable (an empty bytes.Buffer).
	buf := bytes.NewBuffer(nil)

	// A nice gopher image.
	requestURL := "http://talks.golang.org/2012/splash/appenginegophercolor.jpg"

	// We don't need any GET vars.
	requestVariables := url.Values{}

	// Let's pass buf's address ad first argument.
	err = rest.Get(&buf, requestURL, requestVariables)

	// Was there any error?
	if err == nil {

		// Printing some response data.
		log.Printf("Got response with size %d\n", buf.Len())

		// Trying to open the buffer with image/jpeg.
		log.Printf("Trying to decode JPEG file.\n")

		img, err := jpeg.Decode(buf)

		if err == nil {
			log.Printf("JPEG decoded correctly!")
			log.Printf("-> bounds: %v", img.Bounds())
		} else {
			log.Printf("Error decoding PNG file: %s", err.Error())
		}

	} else {
		// Yes, we had an error.
		log.Printf("Request failed: %s", err.Error())
	}

}
Пример #5
0
Файл: main.go Проект: khaf/rest
func main() {

	// You may want to see the full client debug.
	// rest.Debug = true

	// Destination variable.
	buf := map[string]interface{}{}

	// This service returns a JSON string containing your IP, like:
	// {"ip": "173.194.64.141"}
	requestURL := "http://ip.jsontest.com/"

	// We don't need any GET vars.
	requestVariables := url.Values{}

	// Let's pass buf's address ad first argument.
	err := rest.Get(&buf, requestURL, requestVariables)

	// Was there any error?
	if err == nil {

		// Printing response dump.
		log.Printf("Got response: buf = %v\n", buf)

		// Expecting a map with a single "ip" key.
		if ip, ok := buf["ip"].(string); ok {
			// What is my IP?
			log.Printf("According to ip.jsontest.com, your IP is %s\n", ip)
		}

	} else {
		// Yes, we had an error.
		log.Printf("Request failed: %s", err.Error())
	}

}