<commit_before>package glacier

import (
	"encoding/json"
	"errors"
	"github.com/rdwilliamson/aws"
	"io/ioutil"
	"net/http"
	"time"
)

// A vault is a container for storing archives.
type Vault struct {
	CreationDate      time.Time
	LastInventoryDate time.Time
	NumberOfArchives  uint
	SizeInBytes       uint64
	VaultARN          string
	VaultName         string
}

// TODO write unmarshaler instead of using these structs
type vault struct {
	CreationDate      string
	LastInventoryDate *string
	NumberOfArchives  uint
	SizeInBytes       uint64
	VaultARN          string
	VaultName         string
}
type vaultsList struct {
	Marker    *string
	VaultList []vault
}

func (c *Connection) CreateVault(name string) error {
	request, err := http.NewRequest("PUT",
		"https://"+c.Signature.Region.Glacier+"/-/vaults/"+name, nil)
	if err != nil {
		return err
	}
	request.Header.Add("x-amz-glacier-version", "2012-06-01")

	err = c.Signature.Sign(request)
	if err != nil {
		return err
	}

	response, err := c.Client.Do(request)
	if err != nil {
		return err
	}

	if response.StatusCode != 201 {
		body, err := ioutil.ReadAll(response.Body)
		if err != nil {
			return err
		}
		err = response.Body.Close()
		if err != nil {
			return err
		}
		var e aws.Error
		err = json.Unmarshal(body, &e)
		if err != nil {
			return err
		}
		return e
	}

	return nil
}

func (c *Connection) ListVaults(limit int, marker string) (string, []Vault, error) {
	if limit < 0 || limit > 1000 {
		return "", nil, errors.New("limit must be 1 through 1000")
	}

	request, err := http.NewRequest("GET",
		"https://"+c.Signature.Region.Glacier+"/-/vaults", nil)
	if err != nil {
		return "", nil, err
	}
	request.Header.Add("x-amz-glacier-version", "2012-06-01")<commit_msg>Added delete and describe for vaults.<commit_after>package glacier

import (
	"encoding/json"
	"errors"
	"github.com/rdwilliamson/aws"
	"io/ioutil"
	"net/http"
	"time"
)

// A vault is a container for storing archives.
type Vault struct {
	CreationDate      time.Time
	LastInventoryDate time.Time
	NumberOfArchives  uint
	SizeInBytes       uint64
	VaultARN          string
	VaultName         string
}

// TODO write unmarshaler instead of using these structs
type vault struct {
	CreationDate      string
	LastInventoryDate *string
	NumberOfArchives  uint
	SizeInBytes       uint64
	VaultARN          string
	VaultName         string
}
type vaultsList struct {
	Marker    *string
	VaultList []vault
}

func (c *Connection) CreateVault(name string) error {
	request, err := http.NewRequest("PUT",
		"https://"+c.Signature.Region.Glacier+"/-/vaults/"+name, nil)
	if err != nil {
		return err
	}
	request.Header.Add("x-amz-glacier-version", "2012-06-01")

	err = c.Signature.Sign(request)
	if err != nil {
		return err
	}

	response, err := c.Client.Do(request)
	if err != nil {
		return err
	}

	if response.StatusCode != 201 {
		body, err := ioutil.ReadAll(response.Body)
		if err != nil {
			return err
		}
		err = response.Body.Close()
		if err != nil {
			return err
		}
		var e aws.Error
		err = json.Unmarshal(body, &e)
		if err != nil {
			return err
		}
		return e
	}

	return nil
}

func (c *Connection) DeleteVault(name string) error {
	request, err := http.NewRequest("DELETE",
		"https://"+c.Signature.Region.Glacier+"/-/vaults/"+name, nil)
	if err != nil {
		return err
	}
	request.Header.Add("x-amz-glacier-version", "2012-06-01")

	err = c.Signature.Sign(request)
	if err != nil {
		return err
	}

	response, err := c.Client.Do(request)
	if err != nil {
		return err
	}

	if response.StatusCode != 204 {
		body, err := ioutil.ReadAll(response.Body)
		if err != nil {
			return err
		}
		err = response.Body.Close()
		if err != nil {
			return err
		}
		var e aws.Error
		err = json.Unmarshal(body, &e)
		if err != nil {
			return err
		}
		return e
	}

	return nil
}

func (c *Connection) DescribeVault(name string) (Vault, error) {
	request, err := http.NewRequest("GET",
		"https://"+c.Signature.Region.Glacier+"/-/vaults/"+name, nil)
	if err != nil {
		return Vault{}, err
	}
	request.Header.Add("x-amz-glacier-version", "2012-06-01")

	err = c.Signature.Sign(request)
	if err != nil {
		return Vault{}, err
	}

	response, err := c.Client.Do(request)
	if err != nil {
		return Vault{}, err
	}

	body, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return Vault{}, err
	}
	err = response.Body.Close()
	if err != nil {
		return Vault{}, err
	}

	if response.StatusCode != 200 {
		var e aws.Error
		err = json.Unmarshal(body, &e)
		if err != nil {
			return Vault{}, err
		}
		return Vault{}, e
	}

	var v vault
	err = json.Unmarshal(body, &v)
	if err != nil {
		return Vault{}, err
	}

	var result Vault
	result.CreationDate, err = time.Parse(time.RFC3339, v.CreationDate)
	if err != nil {
		return Vault{}, err
	}
	if v.LastInventoryDate != nil {
		result.LastInventoryDate, err =
			time.Parse(time.RFC3339, *v.LastInventoryDate)
		if err != nil {
			return Vault{}, err
		}
	}
	result.NumberOfArchives = v.NumberOfArchives
	result.SizeInBytes = v.SizeInBytes
	result.VaultARN = v.VaultARN
	result.VaultName = v.VaultName

	return result, nil
}

func (c *Connection) ListVaults(limit int, marker string) (string, []Vault, error) {
	if limit < 0 || limit > 1000 {
		return "", nil, errors.New("limit must be 1 through 1000")
	}

	request, err := http.NewRequest("GET",
		"https://"+c.Signature.Region.Glacier+"/-/vaults", nil)
	if err != nil {
		return "", nil, err
	}
	request.Header.Add("x-amz-glacier-version", "2012-06-01")
{'fraction_non_alphanumeric': 0.13661997453156266, 'fraction_numerical': 0.017464071311624522, 'mean_word_length': 3.109118086696562, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 6, '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': '7082342', 'n_tokens_mistral': 2209, 'n_tokens_neox': 2006, 'n_words': 519}