<commit_before>// (C) 2014 Cybozu.  All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.

package kintone

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"mime"
	"mime/multipart"
	"net/http"
	"net/http/cookiejar"
	"net/textproto"
	"net/url"
	"os"
	"strings"
	"time"
)

const (
	NAME            = "go-kintone"
	VERSION         = "0.1.2"
	DEFAULT_TIMEOUT = time.Second * 600 // Default value for App.Timeout
)

// Library internal errors.
var (
	ErrTimeout         = errors.New("Timeout")
	ErrInvalidResponse = errors.New("Invalid Response")
	ErrTooMany         = errors.New("Too many records")
)

// Server-side errors.
type AppError struct {
	HttpStatus     string `json:"-"`       // e.g. "404 NotFound"
	HttpStatusCode int    `json:"-"`       // e.g. 404
	Message        string `json:"message"` // Human readable message.
	Id             string `json:"id"`      // A unique error ID.
	Code           string `json:"code"`    // For machines.
	Errors         string `json:"errors"`  // Error Description.
}

func (e *AppError) Error() string {
	if len(e.Message) == 0 {
		return "HTTP error: " + e.HttpStatus
	}
	return fmt.Sprintf("AppError: %d [%s] %s (%s) %s",
		e.HttpStatusCode, e.Code, e.Message, e.Id, e.Errors)
}

type UpdateKeyField interface {
	JSONValue() interface{}
}
type UpdateKey struct {
	FieldCode string
	Field     UpdateKeyField
}

func (f UpdateKey) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]interface{}{
		"field": f.FieldCode,
		"value": f.Field.JSONValue(),
	})
}

// App provides kintone application API client.
//
// You need to provide Domain, User, Password, and AppId.
// You can also use an API token instead of user/password.
// If you require specialized client settings, for instance
// need to specify a client certificate, proxy settings, or
// are using Google AppEngine, you can build an *http.Client
// instance and supply it to Client.
//
// ex: Google AppEngine
//
//	import (
//		"appengine"
//		"appengine/urlfetch"
//		"github.com/kintone/go-kintone"
//		"net/http"
//	)
//
//	func handler(w http.ResponseWriter, r *http.Request) {
//		c := appengine.NewContext(r)
//		app := &kintone.App{urlfetch.Client(c)}
//		...
//	}
//
// ex: proxy
//
//	import (
//		"net/http"
//		"net/url"
//		"github.com/kintone/go-kintone"
//	)
//
//	func main() {
//		proxyURL, _ := url.Parse("https://proxy.example.com")
//		transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
//		client := &http.Client(Transport: transport)
//		app := &kintone.App{Client: client}
//		...
//	}
//
// Errors returned by the methods of App may be one of *AppError,
// ErrTimeout, ErrInvalidResponse, or ErrTooMany.
type App struct {
	Domain            string        // domain name.  ex: "sample.cybozu.com", "sample.kintone.com", "sample.cybozu.cn"
	User              string        // User account for API.
	Password          string        // User password for API.
	AppId             uint64        // application ID.
	Client            *http.Client  // Specialized client.
	Timeout           time.Duration // Timeout for API responses.
	ApiToken          string        // API token.
	GuestSpaceId      uint64        // guest space ID.
	token             string        // auth token.
	basicAuth         bool          // true to use Basic Authentication.
	basicAuthUser     string        // User name for Basic Authentication.
	basicAuthPassword string        // Password for Basic Authentication.
	userAgentHeader   string        // User-agent request header string
}

// SetBasicAuth enables use of HTTP basic authentication for access
// to kintone.
func (app *App) SetBasicAuth(user, password string) {
	app.basicAuth = true
	app.basicAuthUser = user
	app.basicAuthPassword = password
}

// HasBasicAuth indicate authentication is basic or not
func (app *App) HasBasicAuth() bool {
	return app.basicAuth
}

// GetBasicAuthUser return username string for basic authentication
func (app *App) GetBasicAuthUser() string {
	return app.basicAuthUser
}

// GetBasicAuthPassword return password string for basic authentication
func (app *App) GetBasicAuthPassword() string {
	return app.basicAuthPassword
}

// SetUserAgentHeader set custom user-agent header for http request
func (app *App) SetUserAgentHeader(userAgentHeader string) {
	app.userAgentHeader = userAgentHeader
}

// GetUserAgentHeader get user-agent header string
func (app *App) GetUserAgentHeader() string {
	goUserAgent := NAME + "/" + VERSION
	if len(app.userAgentHeader) > 0 {
		return goUserAgent + " " + app.userAgentHeader
	}
	return goUserAgent
}

func (app *App) createUrl(api string, query string) url.URL {
	path := fmt.Sprintf("/k/v1/%s.json", api)
	if app.GuestSpaceId > 0 {
		path = fmt.Sprintf("/k/guest/%d/v1/%s.json", app.GuestSpaceId, api)
	}

	resultUrl := url.URL{
		Scheme: "https",<commit_msg>change variable name user agent<commit_after>// (C) 2014 Cybozu.  All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file.

package kintone

import (
	"bytes"
	"encoding/base64"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"mime"
	"mime/multipart"
	"net/http"
	"net/http/cookiejar"
	"net/textproto"
	"net/url"
	"os"
	"strings"
	"time"
)

const (
	NAME            = "go-kintone"
	VERSION         = "0.1.2"
	DEFAULT_TIMEOUT = time.Second * 600 // Default value for App.Timeout
)

// Library internal errors.
var (
	ErrTimeout         = errors.New("Timeout")
	ErrInvalidResponse = errors.New("Invalid Response")
	ErrTooMany         = errors.New("Too many records")
)

// Server-side errors.
type AppError struct {
	HttpStatus     string `json:"-"`       // e.g. "404 NotFound"
	HttpStatusCode int    `json:"-"`       // e.g. 404
	Message        string `json:"message"` // Human readable message.
	Id             string `json:"id"`      // A unique error ID.
	Code           string `json:"code"`    // For machines.
	Errors         string `json:"errors"`  // Error Description.
}

func (e *AppError) Error() string {
	if len(e.Message) == 0 {
		return "HTTP error: " + e.HttpStatus
	}
	return fmt.Sprintf("AppError: %d [%s] %s (%s) %s",
		e.HttpStatusCode, e.Code, e.Message, e.Id, e.Errors)
}

type UpdateKeyField interface {
	JSONValue() interface{}
}
type UpdateKey struct {
	FieldCode string
	Field     UpdateKeyField
}

func (f UpdateKey) MarshalJSON() ([]byte, error) {
	return json.Marshal(map[string]interface{}{
		"field": f.FieldCode,
		"value": f.Field.JSONValue(),
	})
}

// App provides kintone application API client.
//
// You need to provide Domain, User, Password, and AppId.
// You can also use an API token instead of user/password.
// If you require specialized client settings, for instance
// need to specify a client certificate, proxy settings, or
// are using Google AppEngine, you can build an *http.Client
// instance and supply it to Client.
//
// ex: Google AppEngine
//
//	import (
//		"appengine"
//		"appengine/urlfetch"
//		"github.com/kintone/go-kintone"
//		"net/http"
//	)
//
//	func handler(w http.ResponseWriter, r *http.Request) {
//		c := appengine.NewContext(r)
//		app := &kintone.App{urlfetch.Client(c)}
//		...
//	}
//
// ex: proxy
//
//	import (
//		"net/http"
//		"net/url"
//		"github.com/kintone/go-kintone"
//	)
//
//	func main() {
//		proxyURL, _ := url.Parse("https://proxy.example.com")
//		transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
//		client := &http.Client(Transport: transport)
//		app := &kintone.App{Client: client}
//		...
//	}
//
// Errors returned by the methods of App may be one of *AppError,
// ErrTimeout, ErrInvalidResponse, or ErrTooMany.
type App struct {
	Domain            string        // domain name.  ex: "sample.cybozu.com", "sample.kintone.com", "sample.cybozu.cn"
	User              string        // User account for API.
	Password          string        // User password for API.
	AppId             uint64        // application ID.
	Client            *http.Client  // Specialized client.
	Timeout           time.Duration // Timeout for API responses.
	ApiToken          string        // API token.
	GuestSpaceId      uint64        // guest space ID.
	token             string        // auth token.
	basicAuth         bool          // true to use Basic Authentication.
	basicAuthUser     string        // User name for Basic Authentication.
	basicAuthPassword string        // Password for Basic Authentication.
	extUserAgent      string        // User-agent request header string
}

// SetBasicAuth enables use of HTTP basic authentication for access
// to kintone.
func (app *App) SetBasicAuth(user, password string) {
	app.basicAuth = true
	app.basicAuthUser = user
	app.basicAuthPassword = password
}

// HasBasicAuth indicate authentication is basic or not
func (app *App) HasBasicAuth() bool {
	return app.basicAuth
}

// GetBasicAuthUser return username string for basic authentication
func (app *App) GetBasicAuthUser() string {
	return app.basicAuthUser
}

// GetBasicAuthPassword return password string for basic authentication
func (app *App) GetBasicAuthPassword() string {
	return app.basicAuthPassword
}

// SetUserAgentHeader set custom user-agent header for http request
func (app *App) SetUserAgentHeader(userAgentHeader string) {
	app.extUserAgent = userAgentHeader
}

// GetUserAgentHeader get user-agent header string
func (app *App) GetUserAgentHeader() string {
	userAgent := NAME + "/" + VERSION
	if len(app.extUserAgent) > 0 {
		return userAgent + " " + app.extUserAgent
	}
	return userAgent
}

func (app *App) createUrl(api string, query string) url.URL {
	path := fmt.Sprintf("/k/v1/%s.json", api)
	if app.GuestSpaceId > 0 {
		path = fmt.Sprintf("/k/guest/%d/v1/%s.json", app.GuestSpaceId, api)
	}

	resultUrl := url.URL{
		Scheme: "https",
{'fraction_non_alphanumeric': 0.13076533198175366, 'fraction_numerical': 0.0054738976178408515, 'mean_word_length': 2.771406727828746, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 7, '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': '990047', 'n_tokens_mistral': 3236, 'n_tokens_neox': 3169, 'n_words': 933}