コード例 #1
0
ファイル: bgproute_test.go プロジェクト: JReyLBC/jresponse
func TestWriteCLITo(t *testing.T) {

	modelBuf := bytes.Buffer{}

	if err := bgpRouteXMLModel.WriteCLITo(&modelBuf); err != nil {
		t.Error(err)
	}

	fileBuf := bytes.Buffer{}
	if file, err := os.Open(BGP_CLI_FILE); err != nil {
		t.Error(err)
	} else if _, err := fileBuf.ReadFrom(file); err != nil {
		t.Error(err)
	}

	if !bytes.Equal(modelBuf.Bytes(), fileBuf.Bytes()) {
		f1, f2 := "model.cli", "current.cli"
		if file1, err := os.Create(f1); err != nil {
			t.Error(err)
		} else if file2, err := os.Create(f2); err != nil {
			t.Error(err)
		} else {
			t.Logf("writing output files for diff: %s, %s", f1, f2)
			file1.Write(modelBuf.Bytes())
			file2.Write(fileBuf.Bytes())
			t.Error("model for CLI parse does not match")
		}
	}
}
コード例 #2
0
ファイル: hue.go プロジェクト: keep94/gohue
// Get gets the properties of a light. lightId is the ID of the light.
// properties is the returned properties.
// response is the raw response from the hue bridge or nil if communication
// failed. This function may return both a non-nil response and an error
// if the response from the hue bridge indicates an error. For most
// applications, it is enough just to look at properties and err.
func (c *Context) Get(lightId int) (
	properties *LightProperties, response []byte, err error) {
	request := &http.Request{
		Method: "GET",
		URL:    c.getLightUrl(lightId),
	}
	client := c.client
	var resp *http.Response
	if resp, err = client.Do(request); err != nil {
		return
	}
	defer resp.Body.Close()
	var respBuffer bytes.Buffer
	if _, err = respBuffer.ReadFrom(resp.Body); err != nil {
		return
	}
	response = respBuffer.Bytes()
	var jsonProps json_structs.LightState
	if err = json.Unmarshal(response, &jsonProps); err != nil {
		err = toError(response)
		return
	}
	if jsonProps.State != nil && len(jsonProps.State.XY) == 2 {
		state := jsonProps.State
		jsonColor := state.XY
		properties = &LightProperties{
			C:   NewMaybeColor(NewColor(jsonColor[0], jsonColor[1])),
			Bri: maybe.NewUint8(state.Bri),
			On:  maybe.NewBool(state.On)}
	} else {
		err = GeneralError
	}
	return
}
コード例 #3
0
func TestProgressReader(t *testing.T) {
	filename := "progress_test.go"
	f, err := os.Open(filename)
	defer f.Close()
	if err != nil {
		log.Fatalln(err)
	}
	fs, err := os.Stat(filename)
	if err != nil {
		log.Fatalln(err)
	}

	p := New()
	p.Total = fs.Size()
	p.Progress = func(current, total, expected int64) {
		log.Println("Reading", current, total, expected)
		assert.Equal(t, true, current <= total)
	}

	b := new(bytes.Buffer)
	r := syncreader.New(f, p)
	_, err = b.ReadFrom(r)
	if err != nil {
		log.Fatalln(err)
	}
	assert.Equal(t, fs.Size(), int64(b.Len()))
}
コード例 #4
0
ファイル: request_test.go プロジェクト: Clarifai/kubernetes
func TestStream(t *testing.T) {
	expectedBody := "expected body"

	testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		flusher, ok := w.(http.Flusher)
		if !ok {
			panic("need flusher!")
		}
		w.Header().Set("Transfer-Encoding", "chunked")
		w.WriteHeader(http.StatusOK)
		w.Write([]byte(expectedBody))
		flusher.Flush()
	}))
	defer testServer.Close()

	s := testRESTClient(t, testServer)
	readCloser, err := s.Get().Prefix("path/to/stream/thing").Stream()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
	defer readCloser.Close()
	buf := new(bytes.Buffer)
	buf.ReadFrom(readCloser)
	resultBody := buf.String()

	if expectedBody != resultBody {
		t.Errorf("Expected %s, got %s", expectedBody, resultBody)
	}
}
コード例 #5
0
ファイル: util.go プロジェクト: ARodri/hume
func marshallConfigReader(in io.Reader, c map[string]interface{}, configType string) error {
	buf := new(bytes.Buffer)
	buf.ReadFrom(in)

	switch strings.ToLower(configType) {
	case "yaml", "yml":
		if err := yaml.Unmarshal(buf.Bytes(), &c); err != nil {
			return ConfigParseError{err}
		}

	case "json":
		if err := json.Unmarshal(buf.Bytes(), &c); err != nil {
			return ConfigParseError{err}
		}

	case "toml":
		if _, err := toml.Decode(buf.String(), &c); err != nil {
			return ConfigParseError{err}
		}

	case "properties", "props", "prop":
		var p *properties.Properties
		var err error
		if p, err = properties.Load(buf.Bytes(), properties.UTF8); err != nil {
			return ConfigParseError{err}
		}
		for _, key := range p.Keys() {
			value, _ := p.Get(key)
			c[key] = value
		}
	}

	insensitiviseMap(c)
	return nil
}
コード例 #6
0
func httpRequest(endPoint string, method string, path string, headers map[string][]string, bodyString string) (Response, error) {
	fullUrl := endPoint + path
	v := url.Values{}
	if len(headers) > 0 {
		v = headers
		fullUrl += "?" + v.Encode()
	}
	httpClient := &http.Client{}
	req, err := http.NewRequest(method, fullUrl, strings.NewReader(bodyString))
	if err != nil {
		return Response{}, err
	}
	req.Header = headers
	resp, err := httpClient.Do(req)
	if err != nil {
		return Response{}, err
	}
	buf := new(bytes.Buffer)
	buf.ReadFrom(resp.Body)
	body := buf.String()
	response := Response{
		Code:    resp.StatusCode,
		Body:    body,
		Headers: resp.Header,
	}
	return response, nil
}
コード例 #7
0
ファイル: http_test.go プロジェクト: joegross/terraform
func TestRequestToEndpoint(t *testing.T) {
	ac, err := makeAuthConfig()
	server := createServer()
	defer server.Close()

	requestBody := strings.NewReader("somecoolbodytext")
	request, err := client.NewRequest("GET", server.URL+"/clients", requestBody)

	err = ac.SignRequest(request)
	if err != nil {
		t.Fatal("failed to generate RequestHeaders")
	}

	client := &http.Client{}
	response, err := client.Do(request)
	if err != nil {
		t.Error(err)
	}

	if response.StatusCode != 200 {
		t.Error("Non 200 return code: " + response.Status)
	}

	buf := new(bytes.Buffer)
	buf.ReadFrom(response.Body)
	bodyStr := buf.String()

	if bodyStr != "" {
		t.Error(bodyStr)
	}
}
コード例 #8
0
ファイル: fs_test.go プロジェクト: kunalkushwaha/afero
func TestWriteAt(t *testing.T) {
	for _, fs := range Fss {
		f := newFile("TestWriteAt", fs, t)
		defer fs.Remove(f.Name())
		defer f.Close()

		const data = "hello, world\n"
		io.WriteString(f, data)

		n, err := f.WriteAt([]byte("WORLD"), 7)
		if err != nil || n != 5 {
			t.Fatalf("WriteAt 7: %d, %v", n, err)
		}

		f2, err := fs.Open(f.Name())
		defer f2.Close()
		buf := new(bytes.Buffer)
		buf.ReadFrom(f2)
		b := buf.Bytes()
		if err != nil {
			t.Fatalf("%v: ReadFile %s: %v", fs.Name(), f.Name(), err)
		}
		if string(b) != "hello, WORLD\n" {
			t.Fatalf("after write: have %q want %q", string(b), "hello, WORLD\n")
		}

	}
}
コード例 #9
0
ファイル: barrister.go プロジェクト: coopernurse/barrister-go
// ServeHTTP handles HTTP requests for the server.
func (s *Server) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	buf := bytes.Buffer{}
	_, err := buf.ReadFrom(req.Body)
	if err != nil {
		// TODO: log and return JSON-RPC response?
		panic(err)
	}

	headers := Headers{
		Request:  req.Header,
		Cookies:  req.Cookies(),
		Response: make(map[string][]string),
	}

	resp := s.InvokeBytes(headers, buf.Bytes())
	w.Header().Set("Content-Type", s.ser.MimeType())

	for k, v := range headers.Response {
		for _, s := range v {
			w.Header().Add(k, s)
		}
	}

	// TODO: log err?
	_, err = w.Write(resp)
}
コード例 #10
0
ファイル: api.go プロジェクト: brianstarke/chef-golang
// also from goiardi calc and encodebody data
func calcBodyHash(r *http.Request) (string, error) {
	var bodyStr string
	var err error
	if r.Body == nil {
		bodyStr = ""
	} else {
		save := r.Body
		save, r.Body, err = drainBody(r.Body)
		if err != nil {
			return "", err
		}
		mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
		if err != nil {
			return "", err
		}
		if strings.HasPrefix(mediaType, "multipart/form-data") {
			bodyStr, err = readFileFromRequest(r, params["boundary"])
			if err != nil {
				return "", err
			}
		} else {
			buf := new(bytes.Buffer)
			buf.ReadFrom(r.Body)
			bodyStr = buf.String()
		}
		r.Body = save
	}
	chkHash := hashStr(bodyStr)
	return chkHash, err
}
コード例 #11
0
ファイル: page.go プロジェクト: CowPanda/hugo
func extractContent(r io.Reader) (content []byte, err error) {
	wr := new(bytes.Buffer)
	if _, err = wr.ReadFrom(r); err != nil {
		return
	}
	return wr.Bytes(), nil
}
コード例 #12
0
ファイル: td4.go プロジェクト: silky/rct
func readRaptor() Ride {
	encodedBits, err := ioutil.ReadFile("rides/raptor.td4")
	if err != nil {
		panic(err)
	}
	z := rle.NewReader(bytes.NewReader(encodedBits))
	if err != nil {
		panic(err)
	}
	var bitbuffer bytes.Buffer
	bitbuffer.ReadFrom(z)
	decrypted := bitbuffer.Bytes()
	for i := 0; i < 40; i++ {
		// encode the value of i as hex
		ds := hex.EncodeToString([]byte{byte(i)})
		bitValueInHex := hex.EncodeToString([]byte{decrypted[i]})
		fmt.Printf("%s: %s\n", ds, bitValueInHex)
	}

	// r is a pointer
	r := new(Ride)
	Unmarshal(decrypted, r)
	bits, err := Marshal(r)
	fmt.Println(bits)
	return *r
}
コード例 #13
0
ファイル: parse_test.go プロジェクト: shitfSign/akino
func assertAst(t *testing.T, root ast.Node, case_name string) {
	var out []byte
	var err error
	if out, err = json.Marshal(root); err != nil {
		t.Fatal(err)
	}

	var buf bytes.Buffer
	if err = json.Indent(&buf, out, "", "\t"); err != nil {
		t.Fatal(err)
	}

	var file *os.File
	jsn := buf.String()
	if file, err = os.Open(kTestingPrefix + case_name + ".json"); err != nil {
		t.Logf("[%s] not found: %v, now dump: \n%s", case_name, err, jsn)
		if file, err = os.Create(kTestingPrefix + case_name + ".json_"); err != nil {
			panic(err)
		}
		file.Write(buf.Bytes())
		file.Close()
		return
	}
	defer file.Close()

	buf.Reset()
	if _, err = buf.ReadFrom(file); err != nil {
		t.Fatal(err)
	}
	if buf.String() != jsn {
		t.Fatalf("[%s] Assert failed, Expected: ----------\n%s\nValue is: ----------\n%s",
			case_name, buf.String(), jsn)
	}
}
コード例 #14
0
ファイル: bgproute_test.go プロジェクト: JReyLBC/jresponse
func TestWriteJSONTo(t *testing.T) {

	modelBuf := bytes.Buffer{}
	if _, err := bgpRouteJSONModel.WriteJSONTo(&modelBuf); err != nil {
		t.Error(err)
	}

	fileBuf := bytes.Buffer{}
	if file, err := os.Open(BGP_JSON_FILE); err != nil {
		t.Error(err)
	} else if _, err := fileBuf.ReadFrom(file); err != nil {
		t.Error(err)
	}

	b := new(BGPRoute)
	b.ReadJSONFrom(&fileBuf)
	fileBuf.Reset()
	b.WriteJSONTo(&fileBuf)

	if !bytes.Equal(modelBuf.Bytes(), fileBuf.Bytes()) {
		t.Log(b)
		t.Log(bgpRouteJSONModel)
		t.Error("JSON bytes not equal")
	}
}
コード例 #15
0
// Compare the output of the C-based peg-markdown, which
// is, for each test, available in either a .html or a .mm file accompanying
// the .text file, with the output of this package's Markdown processor.
func compareOutput(w *bytes.Buffer, f Formatter, ext string, textPath string, p *Parser) (err error) {
	var bOrig bytes.Buffer

	r, err := os.Open(textPath)
	if err != nil {
		return
	}
	defer r.Close()

	w.Reset()
	p.Markdown(r, f)

	// replace .text extension by `ext'
	base := textPath[:len(textPath)-len(".text")]
	refPath := base + ext

	r, err = os.Open(refPath)
	if err != nil {
		return
	}
	defer r.Close()
	bOrig.ReadFrom(r)
	if bytes.Compare(bOrig.Bytes(), w.Bytes()) != 0 {
		err = fmt.Errorf("test %q failed", refPath)
	}
	return
}
コード例 #16
0
ファイル: users_test.go プロジェクト: bancek/go-copy
// Checks if the credentials for the integration tests are set in the env vars
func TestUpdateUser(t *testing.T) {
	setupUserService(t)
	defer tearDownUserService()

	//Our update data
	name := "Chuck"
	surname := "Norris"
	changeFlag := "Changed"

	perfectUser := User{
		FirstName: name,
		LastName:  surname,
	}

	mux.HandleFunc("/user",
		func(w http.ResponseWriter, r *http.Request) {
			testMethod(t, r, "PUT")

			// Convert body to values
			buf := new(bytes.Buffer)
			buf.ReadFrom(r.Body)
			values, _ := url.ParseQuery(buf.String())

			// Get the data
			reqName := values["first_name"][0]
			reqSurname := values["last_name"][0]

			// Check if is the same data and if, seth the flag up
			if reqName != name {
				w.Header().Set("Status", "400 Bad Request")
			} else {
				reqName = reqName + changeFlag
			}

			if reqSurname != surname {
				w.Header().Set("Status", "400 Bad Request")
			} else {
				reqSurname = reqSurname + changeFlag
			}

			fmt.Fprintf(w,
				`{
                  "first_name": "%s",
                  "last_name": "%s"
              }`, reqName, reqSurname)
		},
	)

	userService.Update(&perfectUser)

	if perfectUser.FirstName != name+changeFlag || perfectUser.LastName != surname+changeFlag {
		t.Errorf("Could not update the user")
	}

	// Test bad request
	server.Close()
	if err := userService.Update(&perfectUser); err == nil {
		t.Errorf("No server up, should be an error")
	}
}
コード例 #17
0
ファイル: encodedword.go プロジェクト: reillywatson/go
func (d *WordDecoder) convert(buf *bytes.Buffer, charset string, content []byte) error {
	switch {
	case strings.EqualFold("utf-8", charset):
		buf.Write(content)
	case strings.EqualFold("iso-8859-1", charset):
		for _, c := range content {
			buf.WriteRune(rune(c))
		}
	case strings.EqualFold("us-ascii", charset):
		for _, c := range content {
			if c >= utf8.RuneSelf {
				buf.WriteRune(unicode.ReplacementChar)
			} else {
				buf.WriteByte(c)
			}
		}
	default:
		if d.CharsetReader == nil {
			return fmt.Errorf("mime: unhandled charset %q", charset)
		}
		r, err := d.CharsetReader(strings.ToLower(charset), bytes.NewReader(content))
		if err != nil {
			return err
		}
		if _, err = buf.ReadFrom(r); err != nil {
			return err
		}
	}
	return nil
}
コード例 #18
0
ファイル: xgpush.go プロジェクト: sadlyads/xgpush
func (conn *XGPushConn) run() {
	for {
		msg := <-conn.queue
		// Send msg
		resp, err := conn.xgpush.Post(msg)
		if err != nil {
			log.Println("post err", err.Error())
			continue
		}
		buf := new(bytes.Buffer)
		_, err = buf.ReadFrom(resp.Body)
		resp.Body.Close()
		if err != nil {
			log.Printf("read response err: %s\n", err.Error())
			continue
		}
		log.Printf("Response: %s\n", string(buf.Bytes()))
		jsondec := json.NewDecoder(buf)
		var pushResp XGPushResponse
		err = jsondec.Decode(&pushResp)
		if err != nil {
			log.Printf("Decode push response err: %s\n", err.Error())
			continue
		}
		if pushResp.RetCode != 0 {
			log.Printf("Push response: %d - %s\n", pushResp.RetCode, pushResp.ErrMsg)
			continue
		}
		// Todo: push_id check
	}
}
コード例 #19
0
ファイル: packet.go プロジェクト: jrossi/netflow
func (dr *DataRecord) Unmarshal(r io.Reader, fss FieldSpecifiers, t *Translate) error {
	// We don't know how many records there are in a Data Set, so we'll keep
	// reading until we exhausted the buffer.
	buffer := new(bytes.Buffer)
	if _, err := buffer.ReadFrom(r); err != nil {
		return err
	}

	dr.Fields = make(Fields, 0)
	var err error
	for i := 0; buffer.Len() > 0 && i < len(fss); i++ {
		f := Field{
			Type:   fss[i].Type,
			Length: fss[i].Length,
		}
		if err = f.Unmarshal(buffer); err != nil {
			return err
		}
		dr.Fields = append(dr.Fields, f)
	}

	if t != nil && len(dr.Fields) > 0 {
		if err := t.Record(dr); err != nil {
			return err
		}
	}

	return nil
}
コード例 #20
0
ファイル: compress_test.go プロジェクト: orktes/captainhub
func TestGzip(t *testing.T) {
	// Empty Accept-Encoding header
	req, _ := http.NewRequest(echo.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := echo.NewContext(req, echo.NewResponse(rec), echo.New())
	h := func(c *echo.Context) error {
		return c.String(http.StatusOK, "test")
	}
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "test", rec.Body.String())

	// With Accept-Encoding header
	req, _ = http.NewRequest(echo.GET, "/", nil)
	req.Header.Set(echo.AcceptEncoding, "gzip")
	rec = httptest.NewRecorder()
	c = echo.NewContext(req, echo.NewResponse(rec), echo.New())
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding))
	r, err := gzip.NewReader(rec.Body)
	defer r.Close()
	if assert.NoError(t, err) {
		buf := new(bytes.Buffer)
		buf.ReadFrom(r)
		assert.Equal(t, "test", buf.String())
	}
}
コード例 #21
0
ファイル: configreader.go プロジェクト: markgustetic/confer
// Retuns the configuration data into a generic object for for us.
func (cr *ConfigReader) Export() (interface{}, error) {
	var config interface{}
	buf := new(bytes.Buffer)
	buf.ReadFrom(cr.reader)

	switch cr.Format {
	case "yaml":
		if err := yaml.Unmarshal(buf.Bytes(), &config); err != nil {
			jww.ERROR.Fatalf("Error parsing config: %s", err)
		}

	case "json":
		if err := json.Unmarshal(buf.Bytes(), &config); err != nil {
			jww.ERROR.Fatalf("Error parsing config: %s", err)
		}

	case "toml":
		if _, err := toml.Decode(buf.String(), &config); err != nil {
			jww.ERROR.Fatalf("Error parsing config: %s", err)
		}
	default:
		return nil, err.UnsupportedConfigError(cr.Format)
	}

	return config, nil
}
コード例 #22
0
func domainHandler(handler PostHandlerFunc) ExecDomainHandlerFunc {
	return func(ctx *web.Context) string {
		buf := new(bytes.Buffer)
		buf.ReadFrom(ctx.Request.Body)
		return handler(buf.Bytes())
	}
}
コード例 #23
0
ファイル: driver.go プロジェクト: gitter-badger/sift
// getXMLFromGateway queries a ConnectedByTCP Gateway API for XML representing
// the current state of connected devices
func getXMLFromGateway(context *ipv4.ServiceContext) (string, error) {
	token, err := loginWRetry(context, numLoginRetries)
	if err != nil {
		return "", fmt.Errorf("could not log in to Connected By TCP hub")
	}
	// These values form a query to the ConnectedByTCP API
	values := make(url.Values)
	values.Set("cmd", "GWRBatch")
	values.Set("fmt", "xml")
	values.Set("data", "<gwrcmds><gwrcmd><gcmd>RoomGetCarousel</gcmd><gdata><gip><version>1</version><token>"+token+"</token><fields>name,image,imageurl,control,power,product,class,realtype,status</fields></gip></gdata></gwrcmd></gwrcmds>")

	// BUG(donald): The ConnectedByTCP Gateway produces a x509 certificate
	// which is not recognized by the default HTTP client.  This adapter
	// currently ignores the certificate check (!!!)
	client := getCertificateIgnoringClient()
	response, err := client.PostForm("https://"+context.IP.String()+"/gwr/gop.php", values)
	if err != nil {
		return "", err
	}
	defer func() { response.Close = true }()

	buf := new(bytes.Buffer)
	buf.ReadFrom(response.Body)
	return buf.String(), nil
}
コード例 #24
0
ファイル: compress_test.go プロジェクト: hiproz/vodka
func TestGzip(t *testing.T) {
	req, _ := http.NewRequest(vodka.GET, "/", nil)
	rec := httptest.NewRecorder()
	c := vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())
	h := func(c *vodka.Context) error {
		c.Response().Write([]byte("test")) // For Content-Type sniffing
		return nil
	}

	// Skip if no Accept-Encoding header
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "test", rec.Body.String())

	req, _ = http.NewRequest(vodka.GET, "/", nil)
	req.Header.Set(vodka.AcceptEncoding, "gzip")
	rec = httptest.NewRecorder()
	c = vodka.NewContext(req, vodka.NewResponse(rec), vodka.New())

	// Gzip
	Gzip()(h)(c)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "gzip", rec.Header().Get(vodka.ContentEncoding))
	assert.Contains(t, rec.Header().Get(vodka.ContentType), vodka.TextPlain)
	r, err := gzip.NewReader(rec.Body)
	defer r.Close()
	if assert.NoError(t, err) {
		buf := new(bytes.Buffer)
		buf.ReadFrom(r)
		assert.Equal(t, "test", buf.String())
	}
}
コード例 #25
0
// readPayload reads the HTTP response  in chunks, making the read buffer available
// to MeekConn.Read() calls after each chunk; the intention is to allow bytes to
// flow back to the reader as soon as possible instead of buffering the entire payload.
func (meek *MeekConn) readPayload(receivedPayload io.ReadCloser) (totalSize int64, err error) {
	defer receivedPayload.Close()
	totalSize = 0
	for {
		reader := io.LimitReader(receivedPayload, READ_PAYLOAD_CHUNK_LENGTH)
		// Block until there is capacity in the receive buffer
		var receiveBuffer *bytes.Buffer
		select {
		case receiveBuffer = <-meek.emptyReceiveBuffer:
		case receiveBuffer = <-meek.partialReceiveBuffer:
		case <-meek.broadcastClosed:
			return 0, nil
		}
		// Note: receiveBuffer size may exceed FULL_RECEIVE_BUFFER_LENGTH by up to the size
		// of one received payload. The FULL_RECEIVE_BUFFER_LENGTH value is just a threshold.
		n, err := receiveBuffer.ReadFrom(reader)
		meek.replaceReceiveBuffer(receiveBuffer)
		if err != nil {
			return 0, ContextError(err)
		}
		totalSize += n
		if n == 0 {
			break
		}
	}
	return totalSize, nil
}
コード例 #26
0
ファイル: client.go プロジェクト: marsmensch/blobstash
func (kvs *KvStore) Put(key, value string, version int) (*response.KeyValue, error) {
	data := url.Values{}
	data.Set("value", value)
	if version != -1 {
		data.Set("version", strconv.Itoa(version))
	}
	request, err := http.NewRequest("PUT", kvs.ServerAddr+"/api/v1/vkv/key/"+key, strings.NewReader(data.Encode())) //data.Encode()))
	if err != nil {
		return nil, err
	}
	resp, err := kvs.client.Do(request)
	if err != nil {
		return nil, err
	}
	var body bytes.Buffer
	body.ReadFrom(resp.Body)
	resp.Body.Close()
	switch resp.StatusCode {
	case 200:
		kv := &response.KeyValue{}
		if err := json.Unmarshal(body.Bytes(), kv); err != nil {
			return nil, err
		}
		return kv, nil
	default:
		return nil, fmt.Errorf("failed to put key %v: %v", key, body.String())
	}
}
コード例 #27
0
ファイル: main.go プロジェクト: japettyjohn/layoutcsv
func main() {
	var (
		config Config
	)

	configFileName := flag.String("config", "", "Config file")
	headerReport := flag.Bool("headerReport", false, "Produce a report of header mappings")
	flag.Parse()

	configFile, err := os.Open(*configFileName)
	if err != nil {
		fmt.Println("Error opening config:", err)
		return
	}
	defer configFile.Close()
	configBuf := new(bytes.Buffer)
	configBuf.ReadFrom(configFile)
	xml.Unmarshal(configBuf.Bytes(), &config)

	// Parse templates
	textTemplates, err := makeTemplates(&config.TemplateConfig)
	if err != nil {
		panic(err)
	}

	// Process each input file config
	for _, fileConfig := range config.FileConfig {
		if *headerReport {
			processHeader(fileConfig, textTemplates)
			continue
		}
		processFile(fileConfig, textTemplates)
	}
}
コード例 #28
0
ファイル: main.go プロジェクト: sr-ix/andrasta
func (w weatherUnderground) temperature(city string) (float64, error) {
	resp, err := http.Get("http://api.wunderground.com/api/" + w.apiKey + "/conditions/q/" + city + ".json")
	if err != nil {
		return 0, err
	}

	defer resp.Body.Close()

	var d struct {
		Observation struct {
			Celsius float64 `json:"temp_c"`
		} `json:"current_observation"`
	}

	if err := json.NewDecoder(resp.Body).Decode(&d); err != nil {
		buf := new(bytes.Buffer)
		buf.ReadFrom(resp.Body)
		s := buf.String()
		log.Printf(s)
		return 0, err
	}

	kelvin := d.Observation.Celsius + 273.15
	log.Printf("weatherUnderground: %s: %.2f", city, kelvin)
	return kelvin, nil
}
コード例 #29
0
ファイル: gpg_cli.go プロジェクト: mark-adams/client
func (g *GpgCLI) Sign(fp PGPFingerprint, payload []byte) (string, error) {
	g.outputVersion()
	arg := RunGpg2Arg{
		Arguments: []string{"--armor", "--sign", "-u", fp.String()},
		Stdout:    true,
		Stdin:     true,
	}

	res := g.Run2(arg)
	if res.Err != nil {
		return "", res.Err
	}

	res.Stdin.Write(payload)
	res.Stdin.Close()

	buf := new(bytes.Buffer)
	buf.ReadFrom(res.Stdout)
	armored := buf.String()

	// Convert to posix style on windows
	armored = PosixLineEndings(armored)

	if err := res.Wait(); err != nil {
		return "", err
	}

	return armored, nil
}
コード例 #30
0
ファイル: collections.go プロジェクト: dt/gohfile
func fetch(cfg *CollectionConfig) error {
	log.Printf("[FetchRemote] Fetching %s: %s -> %s.", cfg.Name, cfg.SourcePath, cfg.LocalPath)

	fp, err := os.Create(cfg.LocalPath)
	if err != nil {
		return err
	}
	defer fp.Close()

	resp, err := http.Get(cfg.SourcePath)
	if err != nil {
		return err
	}
	if resp.StatusCode >= 400 {
		buf := new(bytes.Buffer)
		buf.ReadFrom(resp.Body)
		return fmt.Errorf("HTTP error fetching (%s): %s\n", resp.Status, buf.String())
	}
	defer resp.Body.Close()

	sz, err := io.Copy(fp, resp.Body)
	if err != nil {
		return err
	}

	sz = sz / (1024 * 1024)
	log.Printf("[FetchRemote] Fetched %s (%dmb).", cfg.Name, sz)
	return nil
}