<commit_before>package aws

import (
	"bytes"
	"container/list"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httputil"
	"regexp"
	"time"
)

func SendHandler(r *Request) {
	r.HTTPResponse, r.Error = r.Service.Config.HTTPClient.Do(r.HTTPRequest)
}

type Operation struct {
	Name        string
	HTTPMethod  string
	HTTPPath    string
	InPayload   string
	OutPayload  string
	Required    []string
	QueryParams []string
	URIParams   []string
	InHeaders   []string
	OutHeaders  []string
}

type Service struct {
	Config       *Config
	Handlers     Handlers
	ManualSend   bool
	ServiceName  string
	APIVersion   string
	Endpoint     string
	JSONVersion  string
	TargetPrefix string
}

var schemeRE = regexp.MustCompile("^([^:]+)://")

func (s *Service) Initialize() {
	if s.Config.HTTPClient == nil {
		s.Config.HTTPClient = http.DefaultClient
	}

	s.Handlers.Send.PushBack(SendHandler)
	s.buildEndpoint()
}

type Request struct {
	*Service
	Time         time.Time
	Operation    *Operation
	HTTPRequest  *http.Request
	HTTPResponse *http.Response
	Body         io.ReadSeeker
	Params       interface{}
	Error        error
	Data         interface{}
	RequestID    string
	Debug        uint
}

func NewRequest(service *Service, operation *Operation, params interface{}, data interface{}) *Request {
	method := operation.HTTPMethod
	if method == "" {
		method = "POST"
	}
	path := service.Endpoint + operation.HTTPPath

	httpReq, _ := http.NewRequest(method, path, nil)
	if httpReq.URL.Path == "" {
		httpReq.URL.Path = "/"
	}

	r := &Request{
		Service:     service,
		Time:        time.Now(),
		Operation:   operation,
		HTTPRequest: httpReq,
		Body:        nil,
		Params:      params,
		Error:       nil,
		Data:        data,
		Debug:       service.Config.LogLevel,
	}

	r.AddDebugHandlers()

	return r
}

func (s *Service) buildEndpoint() {
	if s.Config.Endpoint != "" {
		s.Endpoint = s.Config.Endpoint
	} else {
		s.Endpoint = s.endpointForRegion()
	}

	if !schemeRE.MatchString(s.Endpoint) {
		scheme := "https"
		if s.Config.DisableSSL {
			scheme = "http"
		}
		s.Endpoint = scheme + "://" + s.Endpoint
	}
}

func (r *Request) AddDebugHandlers() {
	if r.Debug == 0 {
		return
	}

	r.Handlers.Sign.PushBack(func(r *Request) {
		dumpedBody, _ := httputil.DumpRequest(r.HTTPRequest, true)

		fmt.Printf("=> [%s] %s.%s(%+v)\n", r.Time,
			r.Service.ServiceName, r.Operation.Name, r.Params)
		fmt.Printf("---[ REQUEST  ]--------------------------------------\n")
		fmt.Printf("%s\n", string(dumpedBody))
		fmt.Printf("-----------------------------------------------------\n\n")
	})
	r.Handlers.Send.PushBack(func(r *Request) {
		defer r.HTTPResponse.Body.Close()
		dumpedBody, _ := httputil.DumpResponse(r.HTTPResponse, true)

		fmt.Printf("---[ RESPONSE ]--------------------------------------\n")
		fmt.Printf("%s\n", string(dumpedBody))
		fmt.Printf("-----------------------------------------------------\n\n")
	})
}

func (r *Request) SetBufferBody(buf []byte) {
	r.SetReaderBody(bytes.NewReader(buf))
}

func (r *Request) SetReaderBody(reader io.ReadSeeker) {
	r.HTTPRequest.Body = ioutil.NopCloser(reader)
	r.Body = reader
}

func (r *Request) Send() error {
	r.Error = nil

	r.Handlers.Build.Run(r)
	if r.Error != nil {
		return r.Error
	}

	r.Handlers.Sign.Run(r)
	if r.Error != nil {
		return r.Error
	}

	r.Handlers.Send.Run(r)
	if r.Error != nil {
		return r.Error
	}

	r.Handlers.ValidateResponse.Run(r)
	if r.Error != nil {<commit_msg>Add helpers to check if input/output structures are filled<commit_after>package aws

import (
	"bytes"
	"container/list"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/http/httputil"
	"reflect"
	"regexp"
	"time"
)

func SendHandler(r *Request) {
	r.HTTPResponse, r.Error = r.Service.Config.HTTPClient.Do(r.HTTPRequest)
}

type Operation struct {
	Name        string
	HTTPMethod  string
	HTTPPath    string
	InPayload   string
	OutPayload  string
	Required    []string
	QueryParams []string
	URIParams   []string
	InHeaders   []string
	OutHeaders  []string
}

type Service struct {
	Config       *Config
	Handlers     Handlers
	ManualSend   bool
	ServiceName  string
	APIVersion   string
	Endpoint     string
	JSONVersion  string
	TargetPrefix string
}

var schemeRE = regexp.MustCompile("^([^:]+)://")

func (s *Service) Initialize() {
	if s.Config.HTTPClient == nil {
		s.Config.HTTPClient = http.DefaultClient
	}

	s.Handlers.Send.PushBack(SendHandler)
	s.buildEndpoint()
}

type Request struct {
	*Service
	Time         time.Time
	Operation    *Operation
	HTTPRequest  *http.Request
	HTTPResponse *http.Response
	Body         io.ReadSeeker
	Params       interface{}
	Error        error
	Data         interface{}
	RequestID    string
	Debug        uint
}

func NewRequest(service *Service, operation *Operation, params interface{}, data interface{}) *Request {
	method := operation.HTTPMethod
	if method == "" {
		method = "POST"
	}
	path := service.Endpoint + operation.HTTPPath

	httpReq, _ := http.NewRequest(method, path, nil)
	if httpReq.URL.Path == "" {
		httpReq.URL.Path = "/"
	}

	r := &Request{
		Service:     service,
		Time:        time.Now(),
		Operation:   operation,
		HTTPRequest: httpReq,
		Body:        nil,
		Params:      params,
		Error:       nil,
		Data:        data,
		Debug:       service.Config.LogLevel,
	}

	r.AddDebugHandlers()

	return r
}

func (s *Service) buildEndpoint() {
	if s.Config.Endpoint != "" {
		s.Endpoint = s.Config.Endpoint
	} else {
		s.Endpoint = s.endpointForRegion()
	}

	if !schemeRE.MatchString(s.Endpoint) {
		scheme := "https"
		if s.Config.DisableSSL {
			scheme = "http"
		}
		s.Endpoint = scheme + "://" + s.Endpoint
	}
}

func (r *Request) AddDebugHandlers() {
	if r.Debug == 0 {
		return
	}

	r.Handlers.Sign.PushBack(func(r *Request) {
		dumpedBody, _ := httputil.DumpRequest(r.HTTPRequest, true)

		fmt.Printf("=> [%s] %s.%s(%+v)\n", r.Time,
			r.Service.ServiceName, r.Operation.Name, r.Params)
		fmt.Printf("---[ REQUEST  ]--------------------------------------\n")
		fmt.Printf("%s\n", string(dumpedBody))
		fmt.Printf("-----------------------------------------------------\n\n")
	})
	r.Handlers.Send.PushBack(func(r *Request) {
		defer r.HTTPResponse.Body.Close()
		dumpedBody, _ := httputil.DumpResponse(r.HTTPResponse, true)

		fmt.Printf("---[ RESPONSE ]--------------------------------------\n")
		fmt.Printf("%s\n", string(dumpedBody))
		fmt.Printf("-----------------------------------------------------\n\n")
	})
}

func (r Request) ParamsFilled() bool {
	return reflect.ValueOf(r.Params).Elem().IsValid()
}

func (r Request) DataFilled() bool {
	return reflect.ValueOf(r.Data).Elem().IsValid()
}

func (r *Request) SetBufferBody(buf []byte) {
	r.SetReaderBody(bytes.NewReader(buf))
}

func (r *Request) SetReaderBody(reader io.ReadSeeker) {
	r.HTTPRequest.Body = ioutil.NopCloser(reader)
	r.Body = reader
}

func (r *Request) Send() error {
	r.Error = nil

	r.Handlers.Build.Run(r)
	if r.Error != nil {
		return r.Error
	}

	r.Handlers.Sign.Run(r)
	if r.Error != nil {
		return r.Error
	}

	r.Handlers.Send.Run(r)
	if r.Error != nil {
		return r.Error
	}

	r.Handlers.ValidateResponse.Run(r)
	if r.Error != nil {
{'fraction_non_alphanumeric': 0.18818411451024417, 'fraction_numerical': 0.000280662363177098, 'mean_word_length': 2.6214430894308944, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4061468', 'n_tokens_mistral': 2592, 'n_tokens_neox': 2509, 'n_words': 527}