<commit_before>/*
Package contextio provides a simple way to sign API requests for http://Context.IO.

The simplest usage is to use DoJSON() to return a json byte array that you can use elsewhere in your code.
For more advanced usage, you can use Do() and parse through the http.Response struct yourself. It is not
specific to an API version, so you can use it to make any request you would make through http://console.Context.IO.
*/
package contextio

import (
	"bytes"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/garyburd/go-oauth/oauth"
)

const (
	// max memory that ParseMultipartForm will use, see https://golang.org/pkg/net/http/#Request.ParseMultipartForm
	defaultMaxMemory = 32 << 21 // 64 MB
)

// the default host that the library contacts
const defaultAPIHost = "api.context.io"

// ContextIO is a struct containing the authentication information and a pointer to the oauth client
type ContextIO struct {
	key     string
	secret  string
	client  *oauth.Client
	apiHost string
}

// NewContextIO returns a ContextIO struct based on your CIO User and Secret
func NewContextIO(key, secret string) *ContextIO {
	c := &oauth.Client{
		Credentials: oauth.Credentials{
			Token:  key,
			Secret: secret,
		},
	}

	return &ContextIO{
		key:     key,
		secret:  secret,
		client:  c,
		apiHost: defaultAPIHost,
	}
}

// SetAPIHost sets the domain (i.e. "api.context.io) for the requests, useful if you are mocking the API for testing
func (c *ContextIO) SetAPIHost(h string) *ContextIO {
	c.apiHost = h
	return c
}

// NewRequest generates a request and signs it
func (c *ContextIO) NewRequest(method, q string, queryParams url.Values, body *string) (req *http.Request, err error) {
	// make sure q has a slash in front of it
	if q[0:1] != "/" {
		q = "/" + q
	}

	query := c.apiHost + q
	if len(queryParams) > 0 {
		query = query + "?" + queryParams.Encode()
	}
	req, err = http.NewRequest(method, "https://"+query, strings.NewReader(*body))
	if err != nil {
		return nil, err
	}
	req.URL.Opaque = q
	req.Header.Set("User-Agent", "GoContextIO Simple Library v. 0.2")

	v := url.Values{}
	switch method {
	case "PUT", "POST", "DELETE":
		// need form data here if uploading
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

		v, err = url.ParseQuery(*body)
		if err != nil {
			return nil, err
		}
	}

	err = c.client.SetAuthorizationHeader(req.Header, nil, req.Method, req.URL, v)
	if err != nil {
		return nil, err
	}<commit_msg>Panic fixes on nil body<commit_after>/*
Package contextio provides a simple way to sign API requests for http://Context.IO.

The simplest usage is to use DoJSON() to return a json byte array that you can use elsewhere in your code.
For more advanced usage, you can use Do() and parse through the http.Response struct yourself. It is not
specific to an API version, so you can use it to make any request you would make through http://console.Context.IO.
*/
package contextio

import (
	"bytes"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"net/url"
	"os"
	"path/filepath"
	"strings"

	"github.com/garyburd/go-oauth/oauth"
)

const (
	// max memory that ParseMultipartForm will use, see https://golang.org/pkg/net/http/#Request.ParseMultipartForm
	defaultMaxMemory = 32 << 21 // 64 MB
)

// the default host that the library contacts
const defaultAPIHost = "api.context.io"

// ContextIO is a struct containing the authentication information and a pointer to the oauth client
type ContextIO struct {
	key     string
	secret  string
	client  *oauth.Client
	apiHost string
}

// NewContextIO returns a ContextIO struct based on your CIO User and Secret
func NewContextIO(key, secret string) *ContextIO {
	c := &oauth.Client{
		Credentials: oauth.Credentials{
			Token:  key,
			Secret: secret,
		},
	}

	return &ContextIO{
		key:     key,
		secret:  secret,
		client:  c,
		apiHost: defaultAPIHost,
	}
}

// SetAPIHost sets the domain (i.e. "api.context.io) for the requests, useful if you are mocking the API for testing
func (c *ContextIO) SetAPIHost(h string) *ContextIO {
	c.apiHost = h
	return c
}

// NewRequest generates a request and signs it
func (c *ContextIO) NewRequest(method, q string, queryParams url.Values, body *string) (req *http.Request, err error) {
	// make sure q has a slash in front of it
	if q[0:1] != "/" {
		q = "/" + q
	}

	query := c.apiHost + q
	if len(queryParams) > 0 {
		query = query + "?" + queryParams.Encode()
	}
	if body != nil {
		// if body is nil, this produces a panic
		req, err = http.NewRequest(method, "https://"+query, strings.NewReader(*body))
	} else {
		req, err = http.NewRequest(method, "https://"+query, nil)
	}
	if err != nil {
		return nil, err
	}
	req.URL.Opaque = q
	req.Header.Set("User-Agent", "GoContextIO Simple Library v. 0.2")

	v := url.Values{}
	switch method {
	case "PUT", "POST", "DELETE":
		// need form data here if uploading
		req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

		if body != nil {
			v, err = url.ParseQuery(*body)
			if err != nil {
				return nil, err
			}
		}
	}

	err = c.client.SetAuthorizationHeader(req.Header, nil, req.Method, req.URL, v)
	if err != nil {
		return nil, err
	}
{'fraction_non_alphanumeric': 0.11861804222648753, 'fraction_numerical': 0.004222648752399232, 'mean_word_length': 3.4768041237113403, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 5, 'lorem ipsum': 0, 'www.': 2, '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': '5232870', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1740, 'n_words': 618}