<commit_before>package phraseapp

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"

	"os"

	"github.com/bgentry/speakeasy"
)

var Debug bool

func EnableDebug() {
	Debug = true
}

type Client struct {
	http.Client
	Credentials *Credentials
}

type Credentials struct {
	Username string `cli:"opt --username -u desc='username used for authentication'"`
	Token    string `cli:"opt --access-token -t desc='access token used for authentication'"`
	TFA      bool   `cli:"opt --tfa desc='use Two-Factor Authentication'"`
	Host     string `cli:"opt --host desc='Host to send Request to'"`
	Debug    bool   `cli:"opt --verbose -v desc='Verbose output'"`
}

func NewClient(credentials *Credentials) (*Client, error) {
	client := &Client{Credentials: credentials}

	envToken := os.Getenv("PHRASEAPP_ACCESS_TOKEN")
	if envToken != "" && credentials.Token == "" && credentials.Username == "" {
		client.Credentials.Token = envToken
	}

	if credentials.Debug == true {
		EnableDebug()
	}

	if credentials.Host == "" {
		client.Credentials.Host = "https://api.phraseapp.com"
	}

	return client, nil
}

func (client *Client) authenticate(req *http.Request) error {
	if client.Credentials == nil {
		return fmt.Errorf("no auth handler registered")
	}

	if client.Credentials.Token != "" {
		req.Header.Set("Authorization", "token "+client.Credentials.Token)
	} else if client.Credentials.Username != "" {
		pwd, err := speakeasy.Ask("Password: ")
		if err != nil {
			return err
		}
		req.SetBasicAuth(client.Credentials.Username, pwd)

		if client.Credentials.TFA { // TFA only required for username+password based login.
			token, err := speakeasy.Ask("TFA-Token: ")
			if err != nil {
				return err
			}
			req.Header.Set("X-PhraseApp-OTP", token)
		}
	} else {
		return fmt.Errorf("either username or token must be given")
	}

	req.Header.Set("User-Agent", GetUserAgent())

	return nil
}

func (client *Client) sendRequestPaginated(urlPath, method, contentType string, body io.Reader, expectedStatus, page, perPage int) (io.ReadCloser, error) {
	endpointURL, err := url.Parse(client.Credentials.Host + urlPath)
	if err != nil {
		return nil, err
	}

	paginate(endpointURL, page, perPage)

	req, err := buildRequest(method, endpointURL, body, contentType)
	if err != nil {
		return nil, err
	}
<commit_msg>fix method signature<commit_after>package phraseapp

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"

	"os"

	"github.com/bgentry/speakeasy"
)

var Debug bool

func EnableDebug() {
	Debug = true
}

type Client struct {
	http.Client
	Credentials *Credentials
}

type Credentials struct {
	Username string `cli:"opt --username -u desc='username used for authentication'"`
	Token    string `cli:"opt --access-token -t desc='access token used for authentication'"`
	TFA      bool   `cli:"opt --tfa desc='use Two-Factor Authentication'"`
	Host     string `cli:"opt --host desc='Host to send Request to'"`
	Debug    bool   `cli:"opt --verbose -v desc='Verbose output'"`
}

func NewClient(credentials *Credentials) (*Client, error) {
	client := &Client{Credentials: credentials}

	envToken := os.Getenv("PHRASEAPP_ACCESS_TOKEN")
	if envToken != "" && credentials.Token == "" && credentials.Username == "" {
		client.Credentials.Token = envToken
	}

	if credentials.Debug == true {
		EnableDebug()
	}

	if credentials.Host == "" {
		client.Credentials.Host = "https://api.phraseapp.com"
	}

	return client, nil
}

func (client *Client) authenticate(req *http.Request) error {
	if client.Credentials == nil {
		return fmt.Errorf("no auth handler registered")
	}

	if client.Credentials.Token != "" {
		req.Header.Set("Authorization", "token "+client.Credentials.Token)
	} else if client.Credentials.Username != "" {
		pwd, err := speakeasy.Ask("Password: ")
		if err != nil {
			return err
		}
		req.SetBasicAuth(client.Credentials.Username, pwd)

		if client.Credentials.TFA { // TFA only required for username+password based login.
			token, err := speakeasy.Ask("TFA-Token: ")
			if err != nil {
				return err
			}
			req.Header.Set("X-PhraseApp-OTP", token)
		}
	} else {
		return fmt.Errorf("either username or token must be given")
	}

	req.Header.Set("User-Agent", GetUserAgent())

	return nil
}

func (client *Client) sendRequestPaginated(method, urlPath, contentType string, body io.Reader, expectedStatus, page, perPage int) (io.ReadCloser, error) {
	endpointURL, err := url.Parse(client.Credentials.Host + urlPath)
	if err != nil {
		return nil, err
	}

	paginate(endpointURL, page, perPage)

	req, err := buildRequest(method, endpointURL, body, contentType)
	if err != nil {
		return nil, err
	}

{'fraction_non_alphanumeric': 0.14055448098001289, 'fraction_numerical': 0.0, 'mean_word_length': 3.956336528221512, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '7107409', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1548, 'n_words': 404}