Example #1
0
func (c thriftClient) Call(ctx context.Context, reqMeta yarpc.CallReqMeta, reqBody envelope.Enveloper) (wire.Value, yarpc.CallResMeta, error) {
	// Code generated for Thrift client calls will probably be something like
	// this:
	//
	// 	func (c *MyServiceClient) someMethod(reqMeta yarpc.CallReqMeta, arg1 Arg1Type, arg2 arg2Type) (returnValue, yarpc.CallResMeta, error) {
	// 		args := myservice.SomeMethodHelper.Args(arg1, arg2)
	// 		resBody, resMeta, err := c.client.Call(reqMeta, args)
	// 		var result myservice.SomeMethodResult
	// 		if err = result.FromWire(resBody); err != nil {
	// 			return nil, resMeta, err
	// 		}
	// 		success, err := myservice.SomeMethodHelper.UnwrapResponse(&result)
	// 		return success, resMeta, err
	// 	}

	out := c.ch.GetUnaryOutbound()

	treq, proto, err := c.buildTransportRequest(reqMeta, reqBody)
	if err != nil {
		return wire.Value{}, nil, err
	}

	tres, err := out.Call(ctx, treq)
	if err != nil {
		return wire.Value{}, nil, err
	}

	defer tres.Body.Close()
	payload, err := ioutil.ReadAll(tres.Body)
	if err != nil {
		return wire.Value{}, nil, err
	}

	envelope, err := proto.DecodeEnveloped(bytes.NewReader(payload))
	if err != nil {
		return wire.Value{}, nil, encoding.ResponseBodyDecodeError(treq, err)
	}

	switch envelope.Type {
	case wire.Reply:
		return envelope.Value, meta.FromTransportResponse(tres), nil
	case wire.Exception:
		var exc internal.TApplicationException
		if err := exc.FromWire(envelope.Value); err != nil {
			return wire.Value{}, nil, encoding.ResponseBodyDecodeError(treq, err)
		}
		return wire.Value{}, nil, thriftException{
			Service:   treq.Service,
			Procedure: treq.Procedure,
			Reason:    &exc,
		}
	default:
		return wire.Value{}, nil, encoding.ResponseBodyDecodeError(
			treq, errUnexpectedEnvelopeType(envelope.Type))
	}
}
Example #2
0
func (c jsonClient) Call(ctx context.Context, reqMeta yarpc.CallReqMeta, reqBody interface{}, resBodyOut interface{}) (yarpc.CallResMeta, error) {
	treq := transport.Request{
		Caller:   c.ch.Caller(),
		Service:  c.ch.Service(),
		Encoding: Encoding,
	}
	meta.ToTransportRequest(reqMeta, &treq)

	encoded, err := json.Marshal(reqBody)
	if err != nil {
		return nil, encoding.RequestBodyEncodeError(&treq, err)
	}

	treq.Body = bytes.NewReader(encoded)
	tres, err := c.ch.GetUnaryOutbound().Call(ctx, &treq)

	if err != nil {
		return nil, err
	}

	dec := json.NewDecoder(tres.Body)
	if err := dec.Decode(resBodyOut); err != nil {
		return nil, encoding.ResponseBodyDecodeError(&treq, err)
	}

	if err := tres.Body.Close(); err != nil {
		return nil, err
	}

	return meta.FromTransportResponse(tres), nil
}