<commit_before>package rapi

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"strings"
)

// Request gathers all information about request
type Request struct {
	Root   string // default JSON root key
	Action string
	vars   map[string]interface{}
	params map[string]string

	req *http.Request
	w   http.ResponseWriter
}

// Init initializing controller
func (r *Request) Init(w http.ResponseWriter, req *http.Request, root string, params map[string]string, extras []string) {
	r.w = w
	r.req = req
	r.Root = root
	r.params = params
	r.Action = r.makeAction(extras)
	r.vars = make(map[string]interface{})
}

func (r *Request) makeAction(extras []string) string {
	if r.params["id"] == "" {
		switch r.req.Method {
		case "GET":
			return "Index"
		case "POST":
			return "Create"
		}
	}

	if r.params["action"] != "" {
		return r.req.Method + capitalize(r.params["action"])
	}

	if len(extras) > 0 {
		a := r.req.Method + capitalize(r.params["id"])
		for _, v := range extras {
			if a == v {
				return a
			}
		}
	}

	switch r.req.Method {
	case "GET":
		return "Show"
	case "POST", "PUT":
		return "Update"
	case "DELETE":
		return "Destroy"
	}

	return "WrongAction"
}

// LoadJSONRequest extracting JSON request by key
// from request body into interface
func (r *Request) LoadJSONRequest(root string, v interface{}) {
	defer r.req.Body.Close()

	if root == "" {
		extractJSONPayload(r.req.Body, &v)
		return
	}

	var s []byte
	var body JSON
	extractJSONPayload(r.req.Body, &body)
	s, _ = json.Marshal(body[root])
	json.Unmarshal(s, &v)
}

// QueryParam returns URL query param
func (r *Request) QueryParam(s string) string {
	return r.req.URL.Query().Get(s)
}

// Param get IRL param
func (r *Request) Param(k string) string {
	return r.params[k]
}

// SetVar set session variable
func (r *Request) SetVar(k string, v interface{}) {
	r.vars[k] = v
}

// Var returns session variable
func (r *Request) Var(k string) interface{} {
	return r.vars[k]
}<commit_msg>add Params func to return all URL params<commit_after>package rapi

import (
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	"os"
	"strconv"
	"strings"
)

// Request gathers all information about request
type Request struct {
	Root   string // default JSON root key
	Action string
	vars   map[string]interface{}
	params map[string]string

	req *http.Request
	w   http.ResponseWriter
}

// Init initializing controller
func (r *Request) Init(w http.ResponseWriter, req *http.Request, root string, params map[string]string, extras []string) {
	r.w = w
	r.req = req
	r.Root = root
	r.params = params
	r.Action = r.makeAction(extras)
	r.vars = make(map[string]interface{})
}

func (r *Request) makeAction(extras []string) string {
	if r.params["id"] == "" {
		switch r.req.Method {
		case "GET":
			return "Index"
		case "POST":
			return "Create"
		}
	}

	if r.params["action"] != "" {
		return r.req.Method + capitalize(r.params["action"])
	}

	if len(extras) > 0 {
		a := r.req.Method + capitalize(r.params["id"])
		for _, v := range extras {
			if a == v {
				return a
			}
		}
	}

	switch r.req.Method {
	case "GET":
		return "Show"
	case "POST", "PUT":
		return "Update"
	case "DELETE":
		return "Destroy"
	}

	return "WrongAction"
}

// LoadJSONRequest extracting JSON request by key
// from request body into interface
func (r *Request) LoadJSONRequest(root string, v interface{}) {
	defer r.req.Body.Close()

	if root == "" {
		extractJSONPayload(r.req.Body, &v)
		return
	}

	var s []byte
	var body JSON
	extractJSONPayload(r.req.Body, &body)
	s, _ = json.Marshal(body[root])
	json.Unmarshal(s, &v)
}

// QueryParam returns URL query param
func (r *Request) QueryParam(s string) string {
	return r.req.URL.Query().Get(s)
}

// Param get URL param
func (r *Request) Param(k string) string {
	return r.params[k]
}

// Params returns all URL params
func (r *Request) Params() map[string]string {
	return r.params
}

// SetVar set session variable
func (r *Request) SetVar(k string, v interface{}) {
	r.vars[k] = v
}

// Var returns session variable
func (r *Request) Var(k string) interface{} {
	return r.vars[k]
}
{'fraction_non_alphanumeric': 0.14540131739448647, 'fraction_numerical': 0.00048792388387411563, 'mean_word_length': 3.5657015590200447, 'pattern_counts': {'":': 10, '<': 3, '<?xml version=': 0, '>': 5, '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': '5029073', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1475, 'n_words': 429}