<commit_before>package ip

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/Toorop/govh"
	"net/url"
	//"os"
)

type IpRessource struct {
	client *govh.OvhClient
}

func New(client *govh.OvhClient) (*IpRessource, error) {
	if client == nil {
		return nil, errors.New("client is nil")
	}
	return &IpRessource{client: client}, nil
}

// List IP
func (r *IpRessource) List(ipType string) (ips []IpBlock, err error) {
	var uri string
	if ipType == "all" {
		uri = "ip"
		ipType = ""
	} else {
		uri = fmt.Sprintf("ip?type=%s", ipType)
	}
	t, err := r.client.Do("GET", uri, "")
	var ipl = []string{}
	if err = json.Unmarshal(t, &ipl); err == nil {
		for _, i := range ipl {
			ips = append(ips, IpBlock{i, ipType})
		}
	}
	return
}

//// LOADBALANCING

// List IP load balancing
func (r *IpRessource) LbList() (resp []byte, err error) {
	resp, err = r.client.Do("GET", "ip/loadBalancing", "")
	return
}

// FIREWALL

// List IP of block IP under firewall protection
func (r *IpRessource) FwListIpOfBlock(block IpBlock) (ips []string, err error) {
	uri := fmt.Sprintf("ip/%s/firewall", url.QueryEscape(block.IP))
	t, err := r.client.Do("GET", uri, "")
	if err != nil {
		return
	}
	err = json.Unmarshal(t, &ips)
	return
}

// Add IP to firewall
func (r *IpRessource) FwAddIp(block IpBlock, ipv4 string) error {
	uri := fmt.Sprintf("ip/%s/firewall", url.QueryEscape(block.IP))
	type p struct {
		IpOnFirewall string `json:"ipOnFirewall"`
	}
	payload, err := json.Marshal(p{ipv4})
	if err != nil {
		return err
	}
	_, err = r.client.Do("POST", uri, string(payload))
	return err
}

// Remove IP from firewall
func (r *IpRessource) FwRemoveIp(block IpBlock, ipv4 string) error {
	uri := fmt.Sprintf("ip/%s/firewall/%s", url.QueryEscape(block.IP), ipv4)
	_, err := r.client.Do("DELETE", uri, "")
	return err
}

// Enable / disable firewall for IP ipv4 of IpBlock block
func (r *IpRessource) FwSetFirewallEnable(block IpBlock, ipv4 string, enabled bool) error {
	uri := fmt.Sprintf("ip/%s/firewall/%s", url.QueryEscape(block.IP), ipv4)
	type p struct {
		Enabled bool `json:"enabled"`
	}
	payload, err := json.Marshal(p{enabled})
	if err != nil {
		return err
	}
	s, err := r.client.Do("PUT", uri, string(payload))
	fmt.Println(string(s))<commit_msg>Clean code<commit_after>package ip

import (
	"encoding/json"
	"errors"
	"fmt"
	"github.com/Toorop/govh"
	"net/url"
	//"os"
)

type IpRessource struct {
	client *govh.OvhClient
}

func New(client *govh.OvhClient) (*IpRessource, error) {
	if client == nil {
		return nil, errors.New("client is nil")
	}
	return &IpRessource{client: client}, nil
}

// List IP
func (r *IpRessource) List(ipType string) (ips []IpBlock, err error) {
	var uri string
	if ipType == "all" {
		uri = "ip"
		ipType = ""
	} else {
		uri = fmt.Sprintf("ip?type=%s", ipType)
	}
	t, err := r.client.Do("GET", uri, "")
	var ipl = []string{}
	if err = json.Unmarshal(t, &ipl); err == nil {
		for _, i := range ipl {
			ips = append(ips, IpBlock{i, ipType})
		}
	}
	return
}

//// LOADBALANCING

// List IP load balancing
func (r *IpRessource) LbList() (resp []byte, err error) {
	resp, err = r.client.Do("GET", "ip/loadBalancing", "")
	return
}

// FIREWALL

// List IP of block IP under firewall protection
func (r *IpRessource) FwListIpOfBlock(block IpBlock) (ips []string, err error) {
	uri := fmt.Sprintf("ip/%s/firewall", url.QueryEscape(block.IP))
	t, err := r.client.Do("GET", uri, "")
	if err != nil {
		return
	}
	err = json.Unmarshal(t, &ips)
	return
}

// Add IP to firewall
func (r *IpRessource) FwAddIp(block IpBlock, ipv4 string) error {
	uri := fmt.Sprintf("ip/%s/firewall", url.QueryEscape(block.IP))
	type p struct {
		IpOnFirewall string `json:"ipOnFirewall"`
	}
	payload, err := json.Marshal(p{ipv4})
	if err != nil {
		return err
	}
	_, err = r.client.Do("POST", uri, string(payload))
	return err
}

// Remove IP from firewall
func (r *IpRessource) FwRemoveIp(block IpBlock, ipv4 string) error {
	uri := fmt.Sprintf("ip/%s/firewall/%s", url.QueryEscape(block.IP), ipv4)
	_, err := r.client.Do("DELETE", uri, "")
	return err
}

// Enable / disable firewall for IP ipv4 of IpBlock block
func (r *IpRessource) FwSetFirewallEnable(block IpBlock, ipv4 string, enabled bool) error {
	uri := fmt.Sprintf("ip/%s/firewall/%s", url.QueryEscape(block.IP), ipv4)
	type p struct {
		Enabled bool `json:"enabled"`
	}
	payload, err := json.Marshal(p{enabled})
	if err != nil {
		return err
	}
	s, err := r.client.Do("PUT", uri, string(payload))
{'fraction_non_alphanumeric': 0.17014122394082046, 'fraction_numerical': 0.003138309796009863, 'mean_word_length': 3.876502732240437, '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': '4238434', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1748, 'n_words': 453}