示例#1
0
func (self *Federation) downloadBlob(rawurl, blobref string) (dependencies []string, err os.Error) {
	// Get the blob
	var client http.Client
	req, err := client.Get(rawurl + "?blobref=" + http.URLEscape(blobref))
	if err != nil {
		return nil, err
	}
	// TODO: Improve for large files
	blob, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Printf("Error reading request body")
		return nil, err
	}
	log.Printf("Downloaded %v\n", string(blob))
	req.Body.Close()
	self.store.StoreBlob(blob, "")
	// Check whether the retrieved blob is a schema blob
	mimetype := grapher.MimeType(blob)
	if mimetype == "application/x-lightwave-schema" {
		var schema depSchema
		err = json.Unmarshal(blob, &schema)
		if err != nil {
			log.Printf("Malformed schema blob: %v\n", err)
			return nil, err
		}
		dependencies = schema.Dependencies
	}
	return
}
示例#2
0
func GetTrendingRepositories(client *http.Client) ([]Repository, os.Error) {
	resp, e := client.Get("https://www.github.com/explore")
	if e != nil {
		return nil, e
	}
	rootnode, e := html.Parse(resp.Body)
	if e != nil {
		return nil, e
	}

	list := make([]Repository, 0, 5)
	listnode := findNextNodeWithClass(rootnode, "ranked-repositories")
	for _, item := range listnode.Child {
		repo := findNextNodeWithTag(item, "h3")
		if repo != nil && !hasClass("last", item) {
			owner := repo.Child[1].Child[0].Data
			name := repo.Child[3].Child[0].Data
			list = append(list, Repository{
				User: owner,
				Name: name,
			})
		}
	}
	return list, nil

}
示例#3
0
func fetchXMLFeed(url string) string {
	client := http.Client{}
	response, err := client.Get(url)
	if err != nil {
		return ""
	}
	defer response.Body.Close()
	return readAllContents(response.Body)
}
示例#4
0
文件: cstore_test.go 项目: emk/cstore
func assertHttpGetStatus(t *testing.T, client *http.Client, code int, url string) {
	r, err := client.Get(url)
	if err != nil {
		t.Errorf("Can't GET %v: %v", url, err)
		return
	}
	defer r.Body.Close()
	if r.StatusCode != code {
		t.Errorf("Expected status %v, got %v", code, r.Status)
	}
}
示例#5
0
文件: qman.go 项目: ryandotsmith/qman
func sickle_lookup(u, p, q_name string, q *Queue) {
	url := sickleUrl + "/lookup?username="******"&password="******"&queue_name=" + q_name
	client := new(http.Client)
	resp, _, _ := client.Get(url)
	body, _ := ioutil.ReadAll(resp.Body)
	if resp.StatusCode == 404 {
		fmt.Println("could not find queue")
	}
	json.Unmarshal(body, q)
	return
}
示例#6
0
func printNum(i int, c *http.Client, ch chan int) {
	url := fmt.Sprintf("http://www.reddit.com?count=%d", i)
	resp, _ := c.Get(url)
	defer resp.Body.Close()

	readBytes, _ := ioutil.ReadAll(resp.Body)
	fmt.Printf("Status: %s, Bytes: %d\n", resp.Status, len(readBytes))
	ch <- 1

	//fmt.Print(byteString(readBytes))
}
示例#7
0
func (this *Neo4j) send(url string, data string) (string, os.Error) {
	var (
		resp *http.Response // http response
		buf  bytes.Buffer   // contains http response body
		err  os.Error
	)
	if len(url) < 1 {
		url = this.URL + "node" // default path
	}
	client := new(http.Client)
	switch strings.ToLower(this.Method) { // which http method
	case "delete":
		req, e := http.NewRequest("DELETE", url, nil)
		if e != nil {
			err = e
			break
		}
		resp, err = client.Do(req)
	case "post":
		body := strings.NewReader(data)
		resp, err = client.Post(url,
			"application/json",
			body,
		)
	case "put":
		body := strings.NewReader(data)
		req, e := http.NewRequest("PUT", url, body)
		if e != nil {
			err = e
			break
		}
		req.Header.Set("Content-Type", "application/json")
		resp, err = client.Do(req)
	case "get":
		fallthrough
	default:
		resp, err = client.Get(url)
	}
	if err != nil {
		return "", err
	}
	defer func() {
		if resp.Body != nil {
			resp.Body.Close()
		}
	}()
	_, err = buf.ReadFrom(resp.Body)
	if err != nil {
		return "", err
	}
	this.StatusCode = resp.StatusCode // the calling method should do more inspection with chkStatusCode() method and determine if the operation was successful or not.
	return buf.String(), nil
}
示例#8
0
文件: cstore_test.go 项目: emk/cstore
func assertHttpGet(t *testing.T, client *http.Client, expected, url string) {
	r, err := client.Get(url)
	if err != nil {
		t.Errorf("Can't GET %v: %v", url, err)
		return
	}
	defer r.Body.Close()
	if r.StatusCode != http.StatusOK {
		t.Errorf("Unexpected HTTP status: %v", r.Status)
		return
	}
	assertResponseBody(t, expected, r)
}
示例#9
0
/*
récupère la vue d'un troll.
Renvoie des SoapItem pour la compatibilité avec la fonction FetchVueSoap.
Utilise le TksManager pour renseigner le nom du troll qui n'est pas renvoyé par le sp.
Paramètres avecTresors, avecLieux : 0 ou 1
*/
func FetchVueSp(numero uint, mdp_restreint string, avecTresors uint, avecLieux uint, tksManager *TksManager) (items []*SoapItem, errorDetails string) {
	httpClient := new(http.Client)
	request := fmt.Sprintf("http://sp.mountyhall.com/SP_Vue2.php?Numero=%d&Motdepasse=%s&Tresors=%d&Lieux=%d", numero, mdp_restreint, avecTresors, avecLieux)
	resp, err := httpClient.Get(request)
	if err != nil {
		errorDetails = err.String()
		return
	}
	defer resp.Body.Close()
	r := bufio.NewReader(resp.Body)
	bline, _, _ := r.ReadLine()
	line := string(bline)
	currentType := ""
	for line != "" {
		if line[0] == '#' {
			tokens := strings.Split(line, " ", 2)
			if tokens[0] == "#DEBUT" && len(tokens) > 1 {
				currentType = (tokens[1])[0 : len(tokens[1])-1]
			}
		} else {
			tokens := strings.Split(line, ";", 5)
			//fmt.Printf(" %s %+v\n", currentType, tokens)
			item := new(SoapItem)
			item.Numero, _ = strconv.Atoi(tokens[0])
			if item.Numero > 0 {
				if currentType == "TROLL" && len(tokens) > 3 {
					item.Nom, _, _ = tksManager.GetNomRaceNiveauTroll(item.Numero)
					item.PositionX, _ = strconv.Atoi(tokens[1])
					item.PositionY, _ = strconv.Atoi(tokens[2])
					item.PositionN, _ = strconv.Atoi(tokens[3])
				} else if len(tokens) > 4 {
					item.Nom = AsciiToUTF8([]uint8(tokens[1]))
					item.PositionX, _ = strconv.Atoi(tokens[2])
					item.PositionY, _ = strconv.Atoi(tokens[3])
					item.PositionN, _ = strconv.Atoi(tokens[4])
				}
				if item.Nom != "" && currentType != "" {
					item.Type = currentType
					items = append(items, item)
				}
			}
		}
		bline, _, _ = r.ReadLine()
		line = string(bline)
	}
	return
}
示例#10
0
func main() {
	logger := cabin.New()
	client := new(http.Client)
	response, error := client.Get("http://www.google.com/")
	if error != nil {
		logger.Log(error)
		return
	}

	channel := make(chan *cabin.Event)
	logger.Subscribe(channel)
	go JSONLogger(channel)

	//logger.Log(response)
	//logger.Log("Hello world")
	logger.Log(response.Header)
	//logger.Log(&cabin.Event{Message: "Hello world", Object: response})
}
示例#11
0
func (self *Federation) downloadFrontier(rawurl string, blobref string) (frontier []string, err os.Error) {
	// Get the blob
	var client http.Client
	req, err := client.Get(rawurl + "?frontier=" + http.URLEscape(blobref))
	if err != nil {
		return nil, err
	}
	// Process the returned value
	blob, err := ioutil.ReadAll(req.Body)
	if err != nil {
		log.Printf("Error reading request body")
		return nil, err
	}
	req.Body.Close()
	err = json.Unmarshal(blob, &frontier)
	if err != nil {
		log.Printf("Malformed frontier response: %v\n", err)
		return nil, err
	}
	return
}
示例#12
0
func TestHelloWorld(t *testing.T) {
	stack := new(Stack)
	testServer := httptest.NewServer(stack.HandlerFunc(helloWorld))
	defer testServer.Close()

	var client = http.Client{}

	// Request against it
	response, err := client.Get(testServer.URL)

	if err != nil {
		t.Error(err)
	}

	if response.StatusCode != 200 {
		t.Error("Expected status to equal 200, got:", response.StatusCode)
	}

	body, _ := ioutil.ReadAll(response.Body)
	if string(body) != "Hello World!" {
		t.Error("Expected body:", string(body), "to equal: \"Hello World!\"")
	}
}
示例#13
0
	fmt.Println(string(raw))
	if enabled {
		fmt.Println("Rendered message:")
		fmt.Println(string(rendered))
		return rendered
	}
	return raw
}

var GetEmbed = func() func(string) (string, bool) {
	client := new(http.Client)
	key := "83518a4c0f8f11e186fe4040d3dc5c07"
	templateString := "http://api.embed.ly/1/oembed?key=" + key + "&url=%s" + "&maxwidth=800"
	return func(url string) (string, bool) {
		requestUrl := fmt.Sprintf(templateString, url)
		res, err := client.Get(requestUrl)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error in GetEmbed: %s\n", err)
			return "", false
		}
		defer res.Body.Close()
		contents, err := ioutil.ReadAll(res.Body)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Error in GetEmbed 2: %s\n", err)
			return "", false
		}
		var raw map[string]interface{}
		json.Unmarshal(contents, &raw)
		fmt.Println(raw)
		if html, contains := raw["html"]; contains {
			return html.(string), true
示例#14
0
func fetchRunList(client *http.Client, url string) *PlusService {
	response, _ := client.Get(url)
	ps := new(PlusService)
	xml.Unmarshal(response.Body, ps)
	return ps
}