<commit_before>package client

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/trendrr/goshire/dynmap"
	"github.com/trendrr/goshire/cheshire"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

var strestId int64 = int64(0)

//create a new unique strest txn id
func NewTxnId() string {
	id := atomic.AddInt64(&strestId, int64(1))
	return fmt.Sprintf("go%d", id)
}

type Client interface {
	// Does a synchronous api call.  times out after the requested timeout.
	// This will automatically set the txn accept to single
	ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error)
	// Does an api call.
	ApiCall(req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error)

	//Closes this client
	Close()
}

type HttpClient struct {
	Address string
}

// does an asynchrounous api call to the requested address.
func HttpApiCall(address string, req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) {
	cl := NewHttp(address)
	cl.ApiCall(req, responseChan, errorChan)
}

// does a synchronous api call to the requested address.
func HttpApiCallSync(address string, req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	cl := NewHttp(address)
	res, err := cl.ApiCallSync(req, timeout)
	return res, err
}

func NewHttp(address string) *HttpClient {
	return &HttpClient{
		Address: address,
	}
}

func (this *HttpClient) Close() {
	//do nothing..
}

func (this *HttpClient) ApiCall(req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) {
	go func() {
		//TODO we could do something that allows streaming http
		res, err := this.ApiCallSync(req, 4*60*time.Second)
		if err != nil {
			errorChan <- err
		} else {
			responseChan <- res
		}
	}()
}

func (this *HttpClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	uri := req.Uri()
	pms, err := req.Params().MarshalURL()
	if err != nil {
		return nil, err
	}
	reqBody := strings.NewReader("")

	if req.Method() == "GET" {
		joiner := "&"
		//add params to the uri
		if !strings.Contains(uri, "?") {
			joiner = "?"
		}
		uri = fmt.Sprintf("%s%s%s", uri, joiner, pms)
	} else {
		reqBody = strings.NewReader(pms)
	}<commit_msg>fix Client interface so api call returns an error<commit_after>package client

import (
	"bufio"
	"encoding/json"
	"fmt"
	"github.com/trendrr/goshire/dynmap"
	"github.com/trendrr/goshire/cheshire"
	"io"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"strings"
	"sync"
	"sync/atomic"
	"time"
)

var strestId int64 = int64(0)

//create a new unique strest txn id
func NewTxnId() string {
	id := atomic.AddInt64(&strestId, int64(1))
	return fmt.Sprintf("go%d", id)
}

type Client interface {
	// Does a synchronous api call.  times out after the requested timeout.
	// This will automatically set the txn accept to single
	ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error)
	// Does an api call.
	ApiCall(req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) error

	//Closes this client
	Close()
}

type HttpClient struct {
	Address string
}

// does an asynchrounous api call to the requested address.
func HttpApiCall(address string, req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) {
	cl := NewHttp(address)
	cl.ApiCall(req, responseChan, errorChan)
}

// does a synchronous api call to the requested address.
func HttpApiCallSync(address string, req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	cl := NewHttp(address)
	res, err := cl.ApiCallSync(req, timeout)
	return res, err
}

func NewHttp(address string) *HttpClient {
	return &HttpClient{
		Address: address,
	}
}

func (this *HttpClient) Close() {
	//do nothing..
}

// Make an async api call
func (this *HttpClient) ApiCall(req *cheshire.Request, responseChan chan *cheshire.Response, errorChan chan error) error {
	go func() {
		//TODO we could do something that allows streaming http
		res, err := this.ApiCallSync(req, 4*60*time.Second)
		if err != nil {
			errorChan <- err
		} else {
			responseChan <- res
		}
	}()
	return nil
}

func (this *HttpClient) ApiCallSync(req *cheshire.Request, timeout time.Duration) (*cheshire.Response, error) {
	uri := req.Uri()
	pms, err := req.Params().MarshalURL()
	if err != nil {
		return nil, err
	}
	reqBody := strings.NewReader("")

	if req.Method() == "GET" {
		joiner := "&"
		//add params to the uri
		if !strings.Contains(uri, "?") {
			joiner = "?"
		}
		uri = fmt.Sprintf("%s%s%s", uri, joiner, pms)
	} else {
		reqBody = strings.NewReader(pms)
	}
{'fraction_non_alphanumeric': 0.13287612516073724, 'fraction_numerical': 0.005572224603514788, 'mean_word_length': 4.072826086956522, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '4178127', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1628, 'n_words': 475}