<commit_before>package asana

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/manishrjain/asanawarrior/x"
	"github.com/pkg/errors"
)

var token = flag.String("token", "", "Token provided by Asana.")
var domain = flag.String("domain", "", "Workspace name, generally your domain name in Asana.")
var cache *acache = new(acache)

const (
	prefix = "https://app.asana.com/api/1.0"
	stamp  = "2006-01-02T15:04:05.999Z"
)

func runRequest(method, url string) ([]byte, error) {
RUNLOOP:
	req, err := http.NewRequest(method, url, nil)
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Add("Authorization", "Bearer "+*token)
	client := &http.Client{
		Timeout: 10 * time.Minute,
	}

	resp, err := client.Do(req)
	statusCode := resp.StatusCode
	if err != nil || statusCode != http.StatusOK {
		log.Printf("runRequest method: [%v] url: [%v] status: [%v] err: [%v]", method, url, http.StatusText(statusCode), err)
		time.Sleep(5 * time.Second)
		goto RUNLOOP
	}
	defer resp.Body.Close()
	return ioutil.ReadAll(resp.Body)
}

func runGetter(i interface{}, suffix string, fields ...string) error {
	url := fmt.Sprintf("%s/%s?opt_fields=%s", prefix, suffix, strings.Join(fields, ","))
	body, err := runRequest("GET", url)
	if err != nil {
		return errors.Wrapf(err, "runGetter: %q", body)
	}
	if err := json.Unmarshal(body, i); err != nil {
		return errors.Wrapf(err, "Unmarshal: %q", body)
	}
	return nil
}

type Basic struct {
	Id    uint64 `json:"id"`
	Name  string `json:"name"`
	Email string `json:"email"`
}
type BasicData struct {
	Data []Basic `json:"data"`<commit_msg>Fix a bug which caused asana to crash on error<commit_after>package asana

import (
	"bytes"
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"

	"github.com/manishrjain/asanawarrior/x"
	"github.com/pkg/errors"
)

var token = flag.String("token", "", "Token provided by Asana.")
var domain = flag.String("domain", "", "Workspace name, generally your domain name in Asana.")
var cache *acache = new(acache)

const (
	prefix = "https://app.asana.com/api/1.0"
	stamp  = "2006-01-02T15:04:05.999Z"
)

func runRequest(method, url string) ([]byte, error) {
RUNLOOP:
	req, err := http.NewRequest(method, url, nil)
	if err != nil {
		log.Fatal(err)
	}

	req.Header.Add("Authorization", "Bearer "+*token)
	client := &http.Client{
		Timeout: 10 * time.Minute,
	}

	resp, err := client.Do(req)
	if err != nil {
		log.Printf("runRequest method: [%v] url: [%v] err: [%v]", method, url, err)
		time.Sleep(5 * time.Second)
		goto RUNLOOP
	}
	code := resp.StatusCode
	if code != http.StatusOK {
		log.Printf("runRequest method: [%v] url: [%v] status: [%v]",
			method, url, http.StatusText(resp.StatusCode))
		time.Sleep(5 * time.Second)
		goto RUNLOOP
	}
	defer resp.Body.Close()
	return ioutil.ReadAll(resp.Body)
}

func runGetter(i interface{}, suffix string, fields ...string) error {
	url := fmt.Sprintf("%s/%s?opt_fields=%s", prefix, suffix, strings.Join(fields, ","))
	body, err := runRequest("GET", url)
	if err != nil {
		return errors.Wrapf(err, "runGetter: %q", body)
	}
	if err := json.Unmarshal(body, i); err != nil {
		return errors.Wrapf(err, "Unmarshal: %q", body)
	}
	return nil
}

type Basic struct {
	Id    uint64 `json:"id"`
	Name  string `json:"name"`
	Email string `json:"email"`
}
type BasicData struct {
	Data []Basic `json:"data"`
{'fraction_non_alphanumeric': 0.18036595991867557, 'fraction_numerical': 0.014231774615161197, 'mean_word_length': 4.178947368421053, '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': '3656445', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1331, 'n_words': 328}