<commit_before>package streamable

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
)

const uploadUrl string = apiUrl + "/upload"

func Upload(filePath string) (VideoResponse, error) {
	return upload(Credentials{}, filePath)
}

func UploadAuthenticated(creds Credentials, filePath string) (VideoResponse, error) {
	return upload(creds, filePath)
}

func upload(creds Credentials, filePath string) (VideoResponse, error) {
	if _, err := os.Stat(filePath); os.IsNotExist(err) {
		return VideoResponse{}, err
	}

	var buf bytes.Buffer

	multipartWriter := multipart.NewWriter(&buf)

	fileHandle, err := os.Open(filePath)
	if err != nil {
		return VideoResponse{}, err
	}

	fileWriter, err := multipartWriter.CreateFormFile("file", filePath)
	if err != nil {
		return VideoResponse{}, err
	}

	_, err = io.Copy(fileWriter, fileHandle)
	if err != nil {
		return VideoResponse{}, err
	}

	multipartWriter.Close()

	req, err := http.NewRequest("POST", uploadUrl, &buf)
	if err != nil {
		return VideoResponse{}, err
	}

	if creds.Username != "" && creds.Password != "" {
		req.SetBasicAuth(creds.Username, creds.Password)
	}

	req.Header.Set("Content-Type", multipartWriter.FormDataContentType())

	client := &http.Client{}
	res, err := client.Do(req)
	defer res.Body.Close()
	if err != nil {
		return VideoResponse{}, err
	}

	if res.StatusCode != http.StatusOK {
		return VideoResponse{}, fmt.Errorf("upload failed")
	}

	jsonRes, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return VideoResponse{}, err
	}

	videoRes := VideoResponse{}
	err = json.Unmarshal(jsonRes, &videoRes)
	if err != nil {
		return VideoResponse{}, err
	}

	return videoRes, nil
}<commit_msg>use helper to convert json to VideoResponse<commit_after>package streamable

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"mime/multipart"
	"net/http"
	"os"
)

const uploadUrl string = apiUrl + "/upload"

func Upload(filePath string) (VideoResponse, error) {
	return upload(Credentials{}, filePath)
}

func UploadAuthenticated(creds Credentials, filePath string) (VideoResponse, error) {
	return upload(creds, filePath)
}

func upload(creds Credentials, filePath string) (VideoResponse, error) {
	if _, err := os.Stat(filePath); os.IsNotExist(err) {
		return VideoResponse{}, err
	}

	var buf bytes.Buffer

	multipartWriter := multipart.NewWriter(&buf)

	fileHandle, err := os.Open(filePath)
	if err != nil {
		return VideoResponse{}, err
	}

	fileWriter, err := multipartWriter.CreateFormFile("file", filePath)
	if err != nil {
		return VideoResponse{}, err
	}

	_, err = io.Copy(fileWriter, fileHandle)
	if err != nil {
		return VideoResponse{}, err
	}

	multipartWriter.Close()

	req, err := http.NewRequest("POST", uploadUrl, &buf)
	if err != nil {
		return VideoResponse{}, err
	}

	if creds.Username != "" && creds.Password != "" {
		req.SetBasicAuth(creds.Username, creds.Password)
	}

	req.Header.Set("Content-Type", multipartWriter.FormDataContentType())

	client := &http.Client{}
	res, err := client.Do(req)
	defer res.Body.Close()
	if err != nil {
		return VideoResponse{}, err
	}

	if res.StatusCode != http.StatusOK {
		return VideoResponse{}, fmt.Errorf("upload failed")
	}

	bodyBytes, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return VideoResponse{}, err
	}

	body := bytesToString(bodyBytes)

	videoRes, err := videoResponseFromJson(body)
	if err != nil {
		return VideoResponse{}, err
	}

	return videoRes, nil
}
{'fraction_non_alphanumeric': 0.14124457308248914, 'fraction_numerical': 0.0, 'mean_word_length': 4.451104100946372, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '5465109', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1174, 'n_words': 291}