<commit_before>// gostagram is an unofficial go client for
// the instagram api.
//
// note: there's two way to get the
// access token, using oauth2 golang library
// or using curl (both ways need a local server
// to get the token).
package gostagram

import (
	"encoding/json"
	"errors"
	"fmt"
	"net/url"
	"strings"

	"github.com/parnurzeal/gorequest"
)

const (
	apiUrl    = "https://api.instagram.com/v1/"
	version   = "1.0.0"
	userAgent = "gostagram/v1"
)

var (
	ClientSecretNotSetError     = errors.New("Client secret not set for signed request.")
	WrongInstagramEndpointError = errors.New("Wrong instagram endpoint.")
)

type Parameters map[string]string
type bodyData map[string]interface{}
type httpResponse map[string]interface{}

// Instagram client.
type Client struct {
	clientId      string // necesary to create subcriptions.
	clientSecret  string // necesary to send signed request.
	access_token  string
	signedRequest bool
}

func NewClient(access_token string) *Client {
	return &Client{
		clientId:      "",
		clientSecret:  "",
		access_token:  access_token,
		signedRequest: false,
	}
}

func (c *Client) SetClientId(id string) {
	c.clientSecret = id
}

func (c *Client) SetClientSecret(cs string) {
	c.clientId = cs
}

func (c *Client) SetSignedRequest(sr bool) {
	c.signedRequest = sr
}

func (c Client) newRequest(method, uri string, res *httpResponse, dataToSend ...bodyData) error {
	if c.signedRequest && len(c.clientSecret) == 0 {
		return ClientSecretNotSetError
	} else if c.signedRequest {
		// if you plan to use signed request,
		// you should check 'Enforce signed requests'
		// option at instagram client settings.
		//
		// Right here, endpoint and query parameters are extracted
		// from the url, to generate a hash and add it to the current url.
		//
		// Check this https://www.instagram.com/developer/secure-api-requests/
		// for more information, about it.
		tmpUrl, err := url.Parse(uri)

		if err != nil {
			return err
		}

		// deleting /v1 from endpoint.
		endpoint := strings.Replace(tmpUrl.EscapedPath(), "/v1", "", 1)

		// if endpoint ends in '/' character
		// should be deleted to correctly generate signed request.
		if endpoint[len(endpoint)-1] == '/' {
			endpoint = endpoint[0 : len(endpoint)-1]
		}

		params := make(Parameters)
		for paramName, paramValue := range tmpUrl.Query() {
			params[paramName] = paramValue[0]
		}

		sig, err := c.generateSignature(endpoint, params)

		if err != nil {
			return err
		}

		uri = uri + "&sig=" + sig
	}

	// set gostagram user agent.
	request := gorequest.New().Set("User-Agent", userAgent)

	switch method {
	case gorequest.POST:
		request = request.Post(uri).Type("multipart")

		// if there's some data to send,
		// add it to the body.
		if dataToSend != nil && len(dataToSend) > 0 {
			request = request.SendMap(dataToSend[0])
		}

		break
	case gorequest.GET:
		request = request.Get(uri).Type("json")
		break<commit_msg>Bug fixed.<commit_after>// gostagram is an unofficial go client for
// the instagram api.
//
// note: there's two way to get the
// access token, using oauth2 golang library
// or using curl (both ways need a local server
// to get the token).
package gostagram

import (
	"encoding/json"
	"errors"
	"fmt"
	"net/url"
	"strings"

	"github.com/parnurzeal/gorequest"
)

const (
	apiUrl    = "https://api.instagram.com/v1/"
	version   = "1.0.0"
	userAgent = "gostagram/v1"
)

var (
	ClientSecretNotSetError     = errors.New("Client secret not set for signed request.")
	WrongInstagramEndpointError = errors.New("Wrong instagram endpoint.")
)

type Parameters map[string]string
type bodyData map[string]interface{}
type httpResponse map[string]interface{}

// Instagram client.
type Client struct {
	clientId      string // necesary to create subcriptions.
	clientSecret  string // necesary to send signed request.
	access_token  string
	signedRequest bool
}

func NewClient(access_token string) *Client {
	return &Client{
		clientId:      "",
		clientSecret:  "",
		access_token:  access_token,
		signedRequest: false,
	}
}

func (c *Client) SetClientId(id string) {
	c.clientId = id
}

func (c *Client) SetClientSecret(cs string) {
	c.clientSecret = cs
}

func (c *Client) SetSignedRequest(sr bool) {
	c.signedRequest = sr
}

func (c Client) newRequest(method, uri string, res *httpResponse, dataToSend ...bodyData) error {
	if c.signedRequest && len(c.clientSecret) == 0 {
		return ClientSecretNotSetError
	} else if c.signedRequest {
		// if you plan to use signed request,
		// you should check 'Enforce signed requests'
		// option at instagram client settings.
		//
		// Right here, endpoint and query parameters are extracted
		// from the url, to generate a hash and add it to the current url.
		//
		// Check this https://www.instagram.com/developer/secure-api-requests/
		// for more information, about it.
		tmpUrl, err := url.Parse(uri)

		if err != nil {
			return err
		}

		// deleting /v1 from endpoint.
		endpoint := strings.Replace(tmpUrl.EscapedPath(), "/v1", "", 1)

		// if endpoint ends in '/' character
		// should be deleted to correctly generate signed request.
		if endpoint[len(endpoint)-1] == '/' {
			endpoint = endpoint[0 : len(endpoint)-1]
		}

		params := make(Parameters)
		for paramName, paramValue := range tmpUrl.Query() {
			params[paramName] = paramValue[0]
		}

		sig, err := c.generateSignature(endpoint, params)

		if err != nil {
			return err
		}

		uri = uri + "&sig=" + sig
	}

	// set gostagram user agent.
	request := gorequest.New().Set("User-Agent", userAgent)

	switch method {
	case gorequest.POST:
		request = request.Post(uri).Type("form")

		// if there's some data to send,
		// add it to the body.
		if dataToSend != nil && len(dataToSend) > 0 {
			request = request.SendMap(dataToSend[0])
		}

		break
	case gorequest.GET:
		request = request.Get(uri).Type("json")
		break
{'fraction_non_alphanumeric': 0.12376237623762376, 'fraction_numerical': 0.0054626152270399455, 'mean_word_length': 3.3887640449438203, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 4, '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': '288106', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1898, 'n_words': 582}