<commit_before>package api

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"net/http"
	"net/url"
	"socialapi/models"
	"socialapi/workers/common/response"

	"github.com/codahale/sneaker"
	"github.com/koding/logging"
)

var (
	ErrPathNotFound           = errors.New("required a path name to store keys")
	ErrRequiredValuesNotFound = errors.New("required fields not found to store")
	ErrPathContentNotFound    = errors.New("Path content not found")
)

// KeyValue holds the credentials whatever you want as key-value pair
type KeyValue map[string]interface{}

type SneakerS3 struct {
	*sneaker.Manager
	log logging.Logger
}

func createLog(context *models.Context, operation, path string, code int) string {

	log := fmt.Sprintf("Logged with IP: %v, requester: %s, operation: %s, key path: %s, response code: %d",
		context.Client.IP,
		context.Client.Account.OldId,
		operation,
		path,
		code,
	)
	return log
}

// Store stores the given credentials on s3
func (s *SneakerS3) Store(u *url.URL, h http.Header, kv KeyValue, context *models.Context) (int, http.Header, interface{}, error) {
	pathName := u.Query().Get("pathName")

	detailedLog := createLog(context, "POST", pathName, http.StatusBadRequest)

	if pathName == "" {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, ErrPathNotFound)
	}

	if !context.IsLoggedIn() {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, models.ErrNotLoggedIn)
	}

	if kv == nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, ErrRequiredValuesNotFound)
	}

	// convert credentials to bytes
	byt, err := json.Marshal(kv)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, err)
	}

	// bytes need to imlement io.Reader interface
	// then we can use struct as 2.parameter of manager.Upload function
	aa := bytes.NewReader(byt)

	// if another requeest comes to same pathName, its data will be updated.
	// and new incoming data is gonna override the old data
	err = s.Manager.Upload(pathName, aa)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, err)
	}

	detailedLog = createLog(context, "POST", pathName, http.StatusOK)

	s.log.Info(detailedLog)
	return response.NewOK(nil)
}

func (s *SneakerS3) Get(u *url.URL, h http.Header, _ interface{}, context *models.Context) (int, http.Header, interface{}, error) {
	pathName := u.Query().Get("pathName")
	detailedLog := createLog(context, "GET", pathName, http.StatusBadRequest)
	if pathName == "" {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, ErrPathNotFound)
	}

	if !context.IsLoggedIn() {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, models.ErrNotLoggedIn)
	}

	downArray := []string{pathName}
	down, err := s.Manager.Download(downArray)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, err)
	}

	if down[pathName] == nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, ErrPathContentNotFound)
	}

	var kv KeyValue

	downX := bytes.NewReader(down[pathName])
	if err := json.NewDecoder(downX).Decode(&kv); err != nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, err)
	}

	detailedLog = createLog(context, "GET", pathName, http.StatusOK)
	return response.NewOK(kv)
}

func (s *SneakerS3) Delete(u *url.URL, h http.Header, _ interface{}, context *models.Context) (int, http.Header, interface{}, error) {
	pathName := u.Query().Get("pathName")
	detailedLog := createLog(context, "DELETE", pathName, http.StatusBadRequest)

	if pathName == "" {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, ErrPathNotFound)
	}

	if !context.IsLoggedIn() {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, models.ErrNotLoggedIn)
	}

	err := s.Manager.Rm(pathName)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(s.log, detailedLog, err)
	}

	detailedLog = createLog(context, "DELETE", pathName, http.StatusOK)
	return response.NewDeleted()
}<commit_msg>go/logging: generate and use logger to pass into func<commit_after>package api

import (
	"bytes"
	"encoding/json"
	"errors"
	"net/http"
	"net/url"
	"socialapi/models"
	"socialapi/workers/common/response"

	"github.com/codahale/sneaker"
	"github.com/koding/logging"
)

var (
	ErrPathNotFound           = errors.New("required a path name to store keys")
	ErrRequiredValuesNotFound = errors.New("required fields not found to store")
	ErrPathContentNotFound    = errors.New("Path content not found")
)

// KeyValue holds the credentials whatever you want as key-value pair
type KeyValue map[string]interface{}

type SneakerS3 struct {
	*sneaker.Manager
	log logging.Logger
}

// Store stores the given credentials on s3
func (s *SneakerS3) Store(u *url.URL, h http.Header, kv KeyValue, context *models.Context) (int, http.Header, interface{}, error) {
	pathName := u.Query().Get("pathName")

	logger := s.createLogger(context, "POST", pathName, http.StatusBadRequest)

	if pathName == "" {
		return response.NewBadRequestWithDetailedLogger(logger, ErrPathNotFound)
	}

	if !context.IsLoggedIn() {
		return response.NewBadRequestWithDetailedLogger(logger, models.ErrNotLoggedIn)
	}

	if kv == nil {
		return response.NewBadRequestWithDetailedLogger(logger, ErrRequiredValuesNotFound)
	}

	// convert credentials to bytes
	byt, err := json.Marshal(kv)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(logger, err)
	}

	// bytes need to imlement io.Reader interface
	// then we can use struct as 2.parameter of manager.Upload function
	aa := bytes.NewReader(byt)

	// if another requeest comes to same pathName, its data will be updated.
	// and new incoming data is gonna override the old data
	err = s.Manager.Upload(pathName, aa)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(logger, err)
	}

	logger = s.createLogger(context, "POST", pathName, http.StatusOK)
	logger.Info("Info")

	return response.NewOK(nil)
}

func (s *SneakerS3) Get(u *url.URL, h http.Header, _ interface{}, context *models.Context) (int, http.Header, interface{}, error) {
	pathName := u.Query().Get("pathName")

	logger := s.createLogger(context, "GET", pathName, http.StatusBadRequest)
	if pathName == "" {
		return response.NewBadRequestWithDetailedLogger(logger, ErrPathNotFound)
	}

	if !context.IsLoggedIn() {
		return response.NewBadRequestWithDetailedLogger(logger, models.ErrNotLoggedIn)
	}

	downArray := []string{pathName}
	down, err := s.Manager.Download(downArray)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(logger, err)
	}

	if down[pathName] == nil {
		return response.NewBadRequestWithDetailedLogger(logger, ErrPathContentNotFound)
	}

	var kv KeyValue

	downX := bytes.NewReader(down[pathName])
	if err := json.NewDecoder(downX).Decode(&kv); err != nil {
		return response.NewBadRequestWithDetailedLogger(logger, err)
	}

	logger = s.createLogger(context, "GET", pathName, http.StatusOK)
	logger.Info("Info")

	return response.NewOK(kv)
}

func (s *SneakerS3) Delete(u *url.URL, h http.Header, _ interface{}, context *models.Context) (int, http.Header, interface{}, error) {
	pathName := u.Query().Get("pathName")

	logger := s.createLogger(context, "GET", pathName, http.StatusBadRequest)

	if pathName == "" {
		return response.NewBadRequestWithDetailedLogger(logger, ErrPathNotFound)
	}

	if !context.IsLoggedIn() {
		return response.NewBadRequestWithDetailedLogger(logger, models.ErrNotLoggedIn)
	}

	err := s.Manager.Rm(pathName)
	if err != nil {
		return response.NewBadRequestWithDetailedLogger(logger, err)
	}

	logger = s.createLogger(context, "DELETE", pathName, http.StatusOK)
	logger.Info("Info")
	return response.NewDeleted()
}

// createLogger creates the log system for sneaker S3 storage
func (s *SneakerS3) createLogger(context *models.Context, reqType, keyPath string, code int) logging.Logger {
	ctx := s.log.New("Sneaker S3")
	logger := ctx.New("IP", context.Client.IP, "requester", context.Client.Account.Nick, "operation", reqType, "key path", keyPath, "response code", code)

	return logger
}
{'fraction_non_alphanumeric': 0.12353882121323982, 'fraction_numerical': 0.0018456995201181247, 'mean_word_length': 5.043122676579926, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2759325', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2643, 'n_words': 657}