<commit_before>/*
 *    Copyright (C) 2015 <NAME>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Authors:
 *      <NAME> <<EMAIL>>
 */

// beehive's RESTful api for introspection and configuration
package api

import (
	_ "log"
	"net/http"
	_ "strconv"

	"github.com/muesli/beehive/bees"

	"github.com/emicklei/go-restful"
	_ "github.com/emicklei/go-restful/swagger"
)

// POST http://localhost:8080/v1/hives
//
// GET http://localhost:8080/v1/hives/1
//
// PUT http://localhost:8080/v1/hives/1
//
// DELETE http://localhost:8080/v1/hives/1
//

type Hive struct {
	Id          string
	Description string
	Image       string
	Options     []bees.BeeOptionDescriptor
	Events      []bees.EventDescriptor
	Actions     []bees.ActionDescriptor
}

type HiveResource struct {
}

func (r HiveResource) Register(container *restful.Container) {
	ws := new(restful.WebService)
	ws.Path("/v1/hives").
		Doc("Manage Hives").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)

	ws.Route(ws.GET("").To(r.findHives).
		// docs
		Doc("get all hives").
		Operation("findHives").
		Writes(Hive{}))

	container.Add(ws)
}

// GET http://localhost:8080/v1/hives
//
func (r HiveResource) findHives(request *restful.Request, response *restful.Response) {
	hives := bees.GetBeeFactories()
	if len(hives) == 0 {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusNotFound, "404: No hives could be found.")
		return
	}

	res := []Hive{}
	for _, hive := range hives {
		h := Hive{
			Id:          (*hive).Name(),
			Description: (*hive).Description(),
			Image:       (*hive).Image(),
			Options:     (*hive).Options(),
			Events:      (*hive).Events(),
			Actions:     (*hive).Actions(),
		}

		res = append(res, h)
	}
	response.WriteEntity(res)
}<commit_msg>Made hives API ember-data compatible.<commit_after>/*
 *    Copyright (C) 2015 <NAME>
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as published
 *    by the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    Authors:
 *      <NAME> <<EMAIL>>
 */

// beehive's RESTful api for introspection and configuration
package api

import (
	_ "log"
	"net/http"
	_ "strconv"

	"github.com/muesli/beehive/bees"

	"github.com/emicklei/go-restful"
	_ "github.com/emicklei/go-restful/swagger"
)

// POST http://localhost:8080/v1/hives
//
// GET http://localhost:8080/v1/hives/1
//
// PUT http://localhost:8080/v1/hives/1
//
// DELETE http://localhost:8080/v1/hives/1
//

type Hive struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	Description string `json:"description"`
	Image       string `json:"image"`
	Options     []bees.BeeOptionDescriptor
	Events      []bees.EventDescriptor
	Actions     []bees.ActionDescriptor
}

type Hives struct {
	Hives 		[]Hive
}

type HiveResource struct {
}

func (r HiveResource) Register(container *restful.Container) {
	ws := new(restful.WebService)
	ws.Path("/v1/hives").
		Doc("Manage Hives").
		Consumes(restful.MIME_JSON).
		Produces(restful.MIME_JSON)

	ws.Route(ws.GET("").To(r.findHives).
		// docs
		Doc("get all hives").
		Operation("findHives").
		Writes(Hive{}))

	container.Add(ws)
}

// GET http://localhost:8080/v1/hives
//
func (r HiveResource) findHives(request *restful.Request, response *restful.Response) {
	hives := bees.GetBeeFactories()
	if len(hives) == 0 {
		response.AddHeader("Content-Type", "text/plain")
		response.WriteErrorString(http.StatusNotFound, "404: No hives could be found.")
		return
	}

	res := Hives{}
	for _, hive := range hives {
		h := Hive{
			Id:          (*hive).Name(),
			Name:        (*hive).Name(),
			Description: (*hive).Description(),
			Image:       "/images/" + (*hive).Image(),
			Options:     (*hive).Options(),
			Events:      (*hive).Events(),
			Actions:     (*hive).Actions(),
		}

		res.Hives = append(res.Hives, h)
	}
	response.WriteEntity(res)
}
{'fraction_non_alphanumeric': 0.13785644051130777, 'fraction_numerical': 0.014945919370698132, 'mean_word_length': 3.172272354388843, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5578030', 'n_tokens_mistral': 1822, 'n_tokens_neox': 1696, 'n_words': 458}