bitbucket.org/Aishee/synsec@v0.0.0-20210414005726-236fc01a153d/pkg/apiclient/client.go (about)

     1  package apiclient
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  	"encoding/json"
     7  	"fmt"
     8  	"io/ioutil"
     9  	"net/http"
    10  	"net/url"
    11  
    12  	"bitbucket.org/Aishee/synsec/pkg/models"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  var (
    17  	InsecureSkipVerify = false
    18  )
    19  
    20  type ApiClient struct {
    21  	/*The http client used to make requests*/
    22  	client *http.Client
    23  	/*Reuse a single struct instead of allocating one for each service on the heap.*/
    24  	common service
    25  	/*config stuff*/
    26  	BaseURL   *url.URL
    27  	URLPrefix string
    28  	UserAgent string
    29  	/*exposed Services*/
    30  	Decisions *DecisionsService
    31  	Alerts    *AlertsService
    32  	Auth      *AuthService
    33  	Metrics   *MetricsService
    34  	Signal    *SignalService
    35  }
    36  
    37  type service struct {
    38  	client *ApiClient
    39  }
    40  
    41  func NewClient(config *Config) (*ApiClient, error) {
    42  	t := &JWTTransport{
    43  		MachineID:      &config.MachineID,
    44  		Password:       &config.Password,
    45  		Scenarios:      config.Scenarios,
    46  		URL:            config.URL,
    47  		UserAgent:      config.UserAgent,
    48  		VersionPrefix:  config.VersionPrefix,
    49  		UpdateScenario: config.UpdateScenario,
    50  	}
    51  	http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: InsecureSkipVerify}
    52  	c := &ApiClient{client: t.Client(), BaseURL: config.URL, UserAgent: config.UserAgent, URLPrefix: config.VersionPrefix}
    53  	c.common.client = c
    54  	c.Decisions = (*DecisionsService)(&c.common)
    55  	c.Alerts = (*AlertsService)(&c.common)
    56  	c.Auth = (*AuthService)(&c.common)
    57  	c.Metrics = (*MetricsService)(&c.common)
    58  	c.Signal = (*SignalService)(&c.common)
    59  
    60  	return c, nil
    61  }
    62  
    63  func NewDefaultClient(URL *url.URL, prefix string, userAgent string, client *http.Client) (*ApiClient, error) {
    64  	if client == nil {
    65  		client = &http.Client{}
    66  	}
    67  	http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: InsecureSkipVerify}
    68  	c := &ApiClient{client: client, BaseURL: URL, UserAgent: userAgent, URLPrefix: prefix}
    69  	c.common.client = c
    70  	c.Decisions = (*DecisionsService)(&c.common)
    71  	c.Alerts = (*AlertsService)(&c.common)
    72  	c.Auth = (*AuthService)(&c.common)
    73  	c.Metrics = (*MetricsService)(&c.common)
    74  	c.Signal = (*SignalService)(&c.common)
    75  	return c, nil
    76  }
    77  
    78  func RegisterClient(config *Config, client *http.Client) (*ApiClient, error) {
    79  	if client == nil {
    80  		client = &http.Client{}
    81  	}
    82  	http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: InsecureSkipVerify}
    83  	c := &ApiClient{client: client, BaseURL: config.URL, UserAgent: config.UserAgent, URLPrefix: config.VersionPrefix}
    84  	c.common.client = c
    85  	c.Decisions = (*DecisionsService)(&c.common)
    86  	c.Alerts = (*AlertsService)(&c.common)
    87  	c.Auth = (*AuthService)(&c.common)
    88  
    89  	resp, err := c.Auth.RegisterWatcher(context.Background(), models.WatcherRegistrationRequest{MachineID: &config.MachineID, Password: &config.Password})
    90  	/*if we have http status, return it*/
    91  	if err != nil {
    92  		if resp != nil && resp.Response != nil {
    93  			return nil, errors.Wrapf(err, "api register (%s) http %s : %s", c.BaseURL, resp.Response.Status, err)
    94  		}
    95  		return nil, errors.Wrapf(err, "api register (%s) : %s", c.BaseURL, err)
    96  	}
    97  	return c, nil
    98  
    99  }
   100  
   101  type Response struct {
   102  	Response *http.Response
   103  	//add our pagination stuff
   104  	//NextPage int
   105  	//...
   106  }
   107  
   108  type ErrorResponse struct {
   109  	models.ErrorResponse
   110  }
   111  
   112  func (e *ErrorResponse) Error() string {
   113  	err := fmt.Sprintf("API error: %s", *e.Message)
   114  	if len(e.Errors) > 0 {
   115  		err += fmt.Sprintf(" (%s)", e.Errors)
   116  	}
   117  	return err
   118  }
   119  
   120  func newResponse(r *http.Response) *Response {
   121  	response := &Response{Response: r}
   122  	//response.populatePageValues()
   123  	return response
   124  }
   125  
   126  func CheckResponse(r *http.Response) error {
   127  	if c := r.StatusCode; 200 <= c && c <= 299 {
   128  		return nil
   129  	}
   130  	errorResponse := &ErrorResponse{}
   131  	data, err := ioutil.ReadAll(r.Body)
   132  	if err == nil && data != nil {
   133  		err := json.Unmarshal(data, errorResponse)
   134  		if err != nil {
   135  			return errors.Wrapf(err, "http code %d, invalid body", r.StatusCode)
   136  		}
   137  	} else {
   138  		errorResponse.Message = new(string)
   139  		*errorResponse.Message = fmt.Sprintf("http code %d, no error message", r.StatusCode)
   140  	}
   141  	return errorResponse
   142  }
   143  
   144  type ListOpts struct {
   145  	//Page    int
   146  	//PerPage int
   147  }
   148  
   149  type DeleteOpts struct {
   150  	//??
   151  }
   152  
   153  type AddOpts struct {
   154  	//??
   155  }