<commit_before>/*
Package gonexmo implements a simple client library for accessing the Nexmo API.

Usage is simple. Create a Nexmo instance with NexmoWithKeyAndSecret(), providing
your API key and API secret. Then send messages with SendTextMessage() or
SendFlashMessage(). The API will return a MessageResponse which you can
use to see if your message went through, how much it cost, etc.
*/
package nexmo

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

// MessageReport is the "status report" for a single SMS sent via the Nexmo API
type MessageReport struct {
	Status           string `json:"status"`
	MessageID        string `json:"message-id"`
	To               string `json:"to"`
	ClientReference  string `json:"client-ref"`
	RemainingBalance string `json:"remaining-balance"`
	MessagePrice     string `json:"message-price"`
	Network          string `json:"network"`
	ErrorText        string `json:"error-text"`
}

// MessageResponse contains the response from Nexmo's API after we attempt to
// send any kind of message.
// It will contain one MessageReport for every 160 chars sent.
type MessageResponse struct {
	MessageCount string          `json:"message-count"`
	Messages     []MessageReport `json:"messages"`
}

// AccountBalance represents the "balance" object we get back when calling
// GET /account/get-balance
type AccountBalance struct {
	Value float64 `json:"value"`
}

// Nexmo encapsulates the Nexmo functions - must be created with
// NexmoWithKeyAndSecret()
type Nexmo struct {
	apiKey    string
	apiSecret string
}

// NexmoWithKeyAndSecret creates a Nexmo object with the provided API key / API
// secret.
func NexmoWithKeyAndSecret(apiKey, apiSecret string) (*Nexmo, error) {
	if apiKey == "" {
		return nil, errors.New("apiKey can not be empty!")
	} else if apiSecret == "" {
		return nil, errors.New("apiSecret can not be empty!")
	}

	nexmo := &Nexmo{apiKey, apiSecret}
	return nexmo, nil
}

func (nexmo *Nexmo) sendMessage(from string, to string, text string,
	clientReference string, statusReportRequired bool, is_flash_message bool) (
	*MessageResponse, error) {
	var messageResponse *MessageResponse

	values := make(url.Values)
	values.Set("api_key", nexmo.apiKey)
	values.Set("api_secret", nexmo.apiSecret)
	values.Set("type", "text")

	values.Set("to", to)
	values.Set("from", from)
	values.Set("text", text)
	values.Set("client_ref", clientReference)

	if statusReportRequired {
		values.Set("status_report_req", string(1))

	}
	if is_flash_message {
		values.Set("message_class", "0")
	}

	client := &http.Client{}
	r, _ := http.NewRequest("POST", "https://rest.nexmo.com/sms/json",
		bytes.NewBufferString(values.Encode())) // <-- URL-encoded payload
	r.Header.Add("Accept", "application/json")
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	// r.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))

	resp, err := client.Do(r)
	defer resp.Body.Close()

	if err != nil {
		return nil, err
	}

	body, _ := ioutil.ReadAll(resp.Body)
<commit_msg>Remove erroneous comment<commit_after>/*
Package gonexmo implements a simple client library for accessing the Nexmo API.

Usage is simple. Create a Nexmo instance with NexmoWithKeyAndSecret(), providing
your API key and API secret. Then send messages with SendTextMessage() or
SendFlashMessage(). The API will return a MessageResponse which you can
use to see if your message went through, how much it cost, etc.
*/
package nexmo

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

// MessageReport is the "status report" for a single SMS sent via the Nexmo API
type MessageReport struct {
	Status           string `json:"status"`
	MessageID        string `json:"message-id"`
	To               string `json:"to"`
	ClientReference  string `json:"client-ref"`
	RemainingBalance string `json:"remaining-balance"`
	MessagePrice     string `json:"message-price"`
	Network          string `json:"network"`
	ErrorText        string `json:"error-text"`
}

// MessageResponse contains the response from Nexmo's API after we attempt to
// send any kind of message.
// It will contain one MessageReport for every 160 chars sent.
type MessageResponse struct {
	MessageCount string          `json:"message-count"`
	Messages     []MessageReport `json:"messages"`
}

// AccountBalance represents the "balance" object we get back when calling
// GET /account/get-balance
type AccountBalance struct {
	Value float64 `json:"value"`
}

// Nexmo encapsulates the Nexmo functions - must be created with
// NexmoWithKeyAndSecret()
type Nexmo struct {
	apiKey    string
	apiSecret string
}

// NexmoWithKeyAndSecret creates a Nexmo object with the provided API key / API
// secret.
func NexmoWithKeyAndSecret(apiKey, apiSecret string) (*Nexmo, error) {
	if apiKey == "" {
		return nil, errors.New("apiKey can not be empty!")
	} else if apiSecret == "" {
		return nil, errors.New("apiSecret can not be empty!")
	}

	nexmo := &Nexmo{apiKey, apiSecret}
	return nexmo, nil
}

func (nexmo *Nexmo) sendMessage(from string, to string, text string,
	clientReference string, statusReportRequired bool, is_flash_message bool) (
	*MessageResponse, error) {
	var messageResponse *MessageResponse

	values := make(url.Values)
	values.Set("api_key", nexmo.apiKey)
	values.Set("api_secret", nexmo.apiSecret)
	values.Set("type", "text")

	values.Set("to", to)
	values.Set("from", from)
	values.Set("text", text)
	values.Set("client_ref", clientReference)

	if statusReportRequired {
		values.Set("status_report_req", string(1))

	}
	if is_flash_message {
		values.Set("message_class", "0")
	}

	client := &http.Client{}
	r, _ := http.NewRequest("POST", "https://rest.nexmo.com/sms/json",
		bytes.NewBufferString(values.Encode())) // <-- URL-encoded payload
	r.Header.Add("Accept", "application/json")
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	resp, err := client.Do(r)
	defer resp.Body.Close()

	if err != nil {
		return nil, err
	}

	body, _ := ioutil.ReadAll(resp.Body)

{'fraction_non_alphanumeric': 0.12111184645929848, 'fraction_numerical': 0.002316346790205162, 'mean_word_length': 4.1843910806174955, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '589467', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1867, 'n_words': 584}