<commit_before>package knsq

import (
	"io"
	"log"
	"net/http"
)

// FIXME: Use socket API instead of HTTP

// CreateTopic makes sure a topic exists on the given nsqd.
func CreateTopic(nsqd string, topic string) error {
	resp, err := http.Get("http://" + nsqd + "/create_topic?topic=" + topic)
	if resp != nil && resp.Body != nil {
		resp.Body.Close()
	}
	return err
}

// Send sends a message on a topic to the specified nsqd.
func Send(nsqd string, topic string, body io.Reader) error {
	reqURL := nsqd + "/put?topic=" + topic
	log.Printf("Sending %s message to: %s", topic, reqURL)
	resp, err := http.DefaultClient.Post(reqURL, "application/json", body)
	defer resp.Body.Close()
	return err
}<commit_msg>Refactor knsq to an interface<commit_after>package knsq

import (
	"io"
	"log"
	"net/http"
	"net/url"
)

// TODO: Add socket implementation

type Client interface {
	// CreateTopic makes sure a topic exists on the given nsqd.
	CreateTopic(topic string) error
	// Send sends a message on a topic to the specified nsqd.
	Send(topic string, body io.Reader)
}

// HttpClient implements the Client interface using HTTP requests
type HttpClient struct {
	*url.URL
}

func (h HttpClient) CreateTopic(topic string) error {
	resp, err := http.Get(h.String() + "/create_topic?topic=" + topic)
	if resp != nil && resp.Body != nil {
		resp.Body.Close()
	}
	return err
}

func (h HttpClient) Send(topic string, body io.Reader) error {
	reqURL := h.String() + "/put?topic=" + topic
	log.Printf("Sending %s message to: %s", topic, reqURL)
	resp, err := http.DefaultClient.Post(reqURL, "application/json", body)
	if resp != nil && resp.Body != nil {
		defer resp.Body.Close()
	}
	return err
}
{'fraction_non_alphanumeric': 0.1354723707664884, 'fraction_numerical': 0.0, 'mean_word_length': 3.881159420289855, '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': '467503', 'n_tokens_mistral': 577, 'n_tokens_neox': 575, 'n_words': 192}