Codebase list golang-github-bluebreezecf-opentsdb-goclient / c286fb4
Import upstream version 0.0~git20190921.0.7961383 Debian Janitor 1 year, 3 months ago
1 changed file(s) with 33 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
2323 package client
2424
2525 import (
26 "context"
2627 "encoding/json"
2728 "errors"
2829 "fmt"
426427 return &clientImpl, nil
427428 }
428429
430 // ClientContext implements the Client interface and additionally provides a
431 // way to return a client that is associated with the given context.
432 type ClientContext interface {
433 // WithContext returns a Client that is associated with the given context.
434 // Use this to pass a context to underlying transport (e.g. to specify a
435 // deadline).
436 WithContext(ctx context.Context) Client
437 Client
438 }
439
440 func NewClientContext(opentsdbCfg config.OpenTSDBConfig) (Client, error) {
441 client, err := NewClient(opentsdbCfg)
442 if err != nil {
443 return nil, err
444 }
445 // We know this is actually clientImpl and implements this interface.
446 return client.(ClientContext), nil
447 }
448
429449 // The private implementation of Client interface.
430450 type clientImpl struct {
431451 tsdbEndpoint string
432452 client *http.Client
453 ctx context.Context
433454 opentsdbCfg config.OpenTSDBConfig
434455 }
435456
452473 // Return the contents of the specific Response instance with
453474 // the string format
454475 String() string
476 }
477
478 func (c *clientImpl) WithContext(ctx context.Context) Client {
479 return &clientImpl{
480 tsdbEndpoint: c.tsdbEndpoint,
481 client: c.client,
482 ctx: ctx,
483 opentsdbCfg: c.opentsdbCfg,
484 }
455485 }
456486
457487 // sendRequest dispatches the http request with the given method name, url and body contents.
460490 // response with the specific type. Otherwise, the returned error is not nil.
461491 func (c *clientImpl) sendRequest(method, url, reqBodyCnt string, parsedResp Response) error {
462492 req, err := http.NewRequest(method, url, strings.NewReader(reqBodyCnt))
493 if c.ctx != nil {
494 req = req.WithContext(c.ctx)
495 }
463496 if err != nil {
464497 return errors.New(fmt.Sprintf("Failed to create request for %s %s: %v", method, url, err))
465498 }