<commit_before>package gapi

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io/ioutil"
)

type PlaylistItem struct {
	Type  string `json:"type"`
	Value string `json:"value"`
	Order int    `json:"order"`
	Title string `json:"title"`
}

type Playlist struct {
	Id       int            `json:"id"`
	Name     string         `json:"name"`
	Interval string         `json:"interval"`
	Items    []PlaylistItem `json:"items"`
}

func (c *Client) Playlist(id int) (*Playlist, error) {
	path := fmt.Sprintf("/api/playlists/%d", id)
	req, err := c.newRequest("GET", path, nil, nil)
	if err != nil {
		return nil, err
	}

	resp, err := c.Do(req)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != 200 {
		return nil, errors.New(resp.Status)
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	playlist := &Playlist{}

	err = json.Unmarshal(data, &playlist)
	if err != nil {
		return nil, err
	}

	return playlist, nil
}

func (c *Client) NewPlaylist(playlist Playlist) (int, error) {
	data, err := json.Marshal(playlist)
	if err != nil {
		return 0, err
	}

	req, err := c.newRequest("POST", "/api/playlists", nil, bytes.NewBuffer(data))
	if err != nil {
		return 0, err
	}

	resp, err := c.Do(req)
	if err != nil {
		return 0, err
	}

	if resp.StatusCode != 200 {
		return 0, errors.New(resp.Status)
	}

	data, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		return 0, err
	}

	result := struct {
		Id int
	}{}

	err = json.Unmarshal(data, &result)
	if err != nil {
		return 0, err
	}

	return result.Id, nil
}

func (c *Client) UpdatePlaylist(playlist Playlist) error {
	path := fmt.Sprintf("/api/playlists/%d", playlist.Id)
	data, err := json.Marshal(playlist)
	if err != nil {
		return err
	}

	req, err := c.newRequest("PUT", path, nil, bytes.NewBuffer(data))
	if err != nil {
		return err
	}

	resp, err := c.Do(req)
	if err != nil {
		return err
	}

	if resp.StatusCode != 200 {
		return errors.New(resp.Status)
	}

	return nil
}

func (c *Client) DeletePlaylist(id int) error {
	path := fmt.Sprintf("/api/playlists/%d", id)
	req, err := c.newRequest("DELETE", path, nil, nil)
	if err != nil {
		return err
	}

	resp, err := c.Do(req)
	if err != nil {
		return err
	}

	if resp.StatusCode != 200 {
		return errors.New(resp.Status)
	}

	return nil
}<commit_msg>playlist methods use central request method<commit_after>package gapi

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
)

type PlaylistItem struct {
	Type  string `json:"type"`
	Value string `json:"value"`
	Order int    `json:"order"`
	Title string `json:"title"`
}

type Playlist struct {
	Id       int            `json:"id"`
	Name     string         `json:"name"`
	Interval string         `json:"interval"`
	Items    []PlaylistItem `json:"items"`
}

// Playlist fetches and returns a Grafana playlist.
func (c *Client) Playlist(id int) (*Playlist, error) {
	path := fmt.Sprintf("/api/playlists/%d", id)
	resp, err := c.request("GET", path, nil, nil)
	if err != nil {
		return nil, err
	}

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	playlist := &Playlist{}

	err = json.Unmarshal(data, &playlist)
	if err != nil {
		return nil, err
	}

	return playlist, nil
}

// NewPlaylist creates a new Grafana playlist.
func (c *Client) NewPlaylist(playlist Playlist) (int, error) {
	data, err := json.Marshal(playlist)
	if err != nil {
		return 0, err
	}

	resp, err := c.request("POST", "/api/playlists", nil, bytes.NewBuffer(data))
	if err != nil {
		return 0, err
	}

	data, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		return 0, err
	}

	result := struct {
		Id int
	}{}

	err = json.Unmarshal(data, &result)
	if err != nil {
		return 0, err
	}

	return result.Id, nil
}

// UpdatePlaylist updates a Grafana playlist.
func (c *Client) UpdatePlaylist(playlist Playlist) error {
	path := fmt.Sprintf("/api/playlists/%d", playlist.Id)
	data, err := json.Marshal(playlist)
	if err != nil {
		return err
	}

	_, err = c.request("PUT", path, nil, bytes.NewBuffer(data))

	return err
}

// DeletePlaylist deletes the Grafana playlist whose ID it's passed.
func (c *Client) DeletePlaylist(id int) error {
	path := fmt.Sprintf("/api/playlists/%d", id)
	_, err := c.request("DELETE", path, nil, nil)

	return err
}
{'fraction_non_alphanumeric': 0.15417156286721503, 'fraction_numerical': 0.005170387779083431, 'mean_word_length': 3.243270189431705, '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': '7264988', 'n_tokens_mistral': 1654, 'n_tokens_neox': 1573, 'n_words': 451}