コード例 #1
0
ファイル: datastore.go プロジェクト: sfrdmn/camlistore
func call(ctx context.Context, method string, req proto.Message, resp proto.Message) error {
	payload, err := proto.Marshal(req)
	if err != nil {
		return err
	}
	url := baseUrl(ctx) + internal.ProjID(ctx) + "/" + method
	r, err := internal.HTTPClient(ctx).Post(url, "application/x-protobuf", bytes.NewReader(payload))
	if err != nil {
		return err
	}
	defer r.Body.Close()
	all, err := ioutil.ReadAll(r.Body)
	if r.StatusCode != http.StatusOK {
		e := &errHTTP{
			StatusCode: r.StatusCode,
			err:        err,
		}
		if err == nil {
			e.Body = string(all)
		}
		return e
	}
	if err != nil {
		return err
	}
	if err = proto.Unmarshal(all, resp); err != nil {
		return err
	}
	return nil
}
コード例 #2
0
ファイル: key.go プロジェクト: Jimmy99/camlistore
// DecodeKey decodes a key from the opaque representation returned by Encode.
func DecodeKey(encoded string) (*Key, error) {
	// Re-add padding.
	if m := len(encoded) % 4; m != 0 {
		encoded += strings.Repeat("=", 4-m)
	}

	b, err := base64.URLEncoding.DecodeString(encoded)
	if err != nil {
		return nil, err
	}

	pKey := new(pb.Key)
	if err := proto.Unmarshal(b, pKey); err != nil {
		return nil, err
	}

	return protoToKey(pKey), nil
}
コード例 #3
0
ファイル: query_test.go プロジェクト: Jimmy99/camlistore
func (t *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {

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

	var in pb.RunQueryRequest
	var resp http.Response
	if err = proto.Unmarshal(body, &in); err != nil {
		// Get back an error
		resp = http.Response{
			StatusCode: http.StatusBadRequest,
		}
	} else {
		// Run our fake query and serialize the response
		var out pb.RunQueryResponse
		err := t.Handler(&in, &out)
		if err != nil {

			resp = http.Response{
				StatusCode: http.StatusBadRequest,
			}
		} else {
			payload, err := proto.Marshal(&out)
			if err != nil {
				resp = http.Response{
					StatusCode: http.StatusBadRequest,
				}
			} else {
				resp = http.Response{
					StatusCode: http.StatusOK,
					Body:       ioutil.NopCloser(bytes.NewBuffer(payload)),
				}
			}

		}
	}

	// Set common response fields
	resp.Proto = "HTTP/1.0"
	resp.ProtoMajor = 1
	resp.ProtoMinor = 1

	return &resp, nil
}