<commit_before>package trac

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

type Client struct {
	server     string // https://user:passwd@trac.example.com/login/jsonrpc
	httpClient *http.Client

	Search *Search
	System *System
	Ticket *Ticket
	Wiki   *Wiki
}

type Request struct {
	Method string   `json:"method"`
	Params []string `json:"params"`
}

type Response struct {
	Error  RPCError        `json:"error,omitempty"`
	Id     string          `json:"id,omitempty"`
	Result json.RawMessage `json:"result,omitempty"`
}

type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Name    string `json:"name"`
}

func (r *RPCError) Error() string {
	return fmt.Sprintf("%v(%d): %v", r.Name, r.Code, r.Message)
}

func NewClient(server string) *Client {
	c := &Client{
		server: server,
	}
	if c.httpClient == nil {
		c.httpClient = http.DefaultClient
	}

	// RPC exported functions
	c.Search = &Search{client: c}
	c.System = &System{client: c}
	c.Ticket = &Ticket{client: c}
	c.Wiki = &Wiki{client: c}
	return c
}

// Query sends a Request and returns a Response.
// Response.Result is unmarshaled by Client.Do
func (c *Client) Query(function string, params ...string) (Response, error) {
	var response = Response{}
	query := Request{function, params}
	body, err := json.Marshal(query)
	if err != nil {
		return response, err
	}
	res, err := http.Post(c.server, "application/json", bytes.NewReader(body))
	if err != nil {
		return response, err
	}
	defer res.Body.Close()

	resp, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return response, err
	}

	if err := json.Unmarshal(resp, &response); err != nil {
		return response, err
	}
	if response.Error.Code != 0 {
		return response, &response.Error
	}
	return response, nil
}

// Do wraps Client.Query to unmarshal Response.Result in the value pointed to
// by v
func (c *Client) Do(function string, v interface{}, params ...string) (interface{}, error) {
	r, err := c.Query(function, params...)<commit_msg>Allow any type in call params<commit_after>package trac

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
)

// Client handles communications with Trac JSONRPC.
type Client struct {
	server     string // https://user:passwd@trac.example.com/login/jsonrpc
	httpClient *http.Client

	// RPC functions
	Search *Search
	System *System
	Ticket *Ticket
	Wiki   *Wiki
}

// Request is send to Trac JSONRPC via a HTTP POST request.
type Request struct {
	Method string        `json:"method"`
	Params []interface{} `json:"params"`
}

// Response represents a response returned by Trac JSONRPC.
type Response struct {
	Error  RPCError        `json:"error,omitempty"`
	ID     string          `json:"id,omitempty"`
	Result json.RawMessage `json:"result,omitempty"`
}

// RPCError is the RPC error returned within the response.
type RPCError struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Name    string `json:"name"`
}

func (r *RPCError) Error() string {
	return fmt.Sprintf("%v(%d): %v", r.Name, r.Code, r.Message)
}

// NewClient returns a new Trac JSONRPC client.
func NewClient(server string) *Client {
	c := &Client{
		server: server,
	}
	if c.httpClient == nil {
		c.httpClient = http.DefaultClient
	}

	// RPC exported functions
	c.Search = &Search{client: c}
	c.System = &System{client: c}
	c.Ticket = &Ticket{client: c}
	c.Wiki = &Wiki{client: c}
	return c
}

// Query sends a Request and returns a Response.
// Response.Result is unmarshaled by Client.Do
func (c *Client) Query(function string, params ...interface{}) (Response, error) {
	var response = Response{}
	query := Request{function, params}
	body, err := json.Marshal(query)
	if err != nil {
		return response, err
	}

	fmt.Printf("%v\n", string(body))

	res, err := http.Post(c.server, "application/json", bytes.NewReader(body))
	if err != nil {
		return response, err
	}
	defer res.Body.Close()

	resp, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return response, err
	}

	if err := json.Unmarshal(resp, &response); err != nil {
		return response, err
	}
	if response.Error.Code != 0 {
		return response, &response.Error
	}
	return response, nil
}

// Do wraps Client.Query to unmarshal Response.Result in the value pointed to
// by v
func (c *Client) Do(function string, v interface{}, params ...interface{}) (interface{}, error) {
	r, err := c.Query(function, params...)
{'fraction_non_alphanumeric': 0.14025914980677426, 'fraction_numerical': 0.0004546487838145033, 'mean_word_length': 3.636459430979979, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '2926292', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1473, 'n_words': 455}