Skip to content

Commit

Permalink
new: support for context.Context
Browse files Browse the repository at this point in the history
  • Loading branch information
primalmotion committed Jan 3, 2018
1 parent 6b34307 commit 7f5734d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package elemental

import (
"context"
"crypto/tls"
"encoding/base64"
"encoding/json"
Expand Down Expand Up @@ -51,17 +52,25 @@ type Request struct {

span opentracing.Span
wireContext opentracing.SpanContext
context context.Context
}

// NewRequest returns a new Request.
func NewRequest() *Request {

return NewRequestWithContext(context.Background())
}

// NewRequestWithContext returns a new Request with the given context.Context.
func NewRequestWithContext(ctx context.Context) *Request {

return &Request{
RequestID: uuid.NewV4().String(),
Parameters: url.Values{},
Headers: http.Header{},
Metadata: map[string]interface{}{},
TrackingData: opentracing.TextMapCarrier{},
context: ctx,
}
}

Expand Down Expand Up @@ -214,6 +223,7 @@ func NewRequestFromHTTPRequest(req *http.Request) (*Request, error) {
wireContext: wireContext,
Order: req.URL.Query()["order"],
ClientIP: req.RemoteAddr,
context: req.Context(),
}, nil
}

Expand Down Expand Up @@ -330,6 +340,7 @@ func (r *Request) Duplicate() *Request {
req.ExternalTrackingType = r.ExternalTrackingType
req.ClientIP = r.ClientIP
req.Order = append([]string{}, r.Order...)
req.context = r.context

for k, v := range r.Headers {
req.Headers[k] = v
Expand Down Expand Up @@ -369,6 +380,11 @@ func (r *Request) Decode(dst interface{}) error {
return UnmarshalJSON(r.Data, &dst)
}

// Context returns the request context.Context.
func (r *Request) Context() context.Context {
return r.context
}

func (r *Request) String() string {

return fmt.Sprintf("<request id:%s operation:%s namespace:%s recursive:%v identity:%s objectid:%s parentidentity:%s parentid:%s version:%d>",
Expand Down

0 comments on commit 7f5734d

Please sign in to comment.