/**
= Creative Commons Lizenzvertrag =
Diese Software ist von der archium GmbH, Gera ist lizenziert unter einer Creative Commons Namensnennung - Nicht kommerziell - Keine Bearbeitungen 4.0 International Lizenz. (http://creativecommons.org/licenses/by-nc-nd/4.0/deed.de)
Individuelle über diese Lizenz hinausgehende Berechtigungen können Sie unter https://archium.org erhalten.

= Creative Commons License =
Software by archium GmbH, Gera is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License. (http://creativecommons.org/licenses/by-nc-nd/4.0/)
Individual permissions beyond the scope of this license may be available at https://archium.org.
**/

package dataverse

import (
	"fmt"
	//
	con "Toolbox/goDataverse/connect"
	tol "Toolbox/goDataverse/tools"
)

func ListDataverses(_dp tol.TDVParams) (string, error) {
	res, err := con.GetRequest(tol.GetSiteUrl(_dp)+"/api/search?q=*&types=dataverse&per_page=1000",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}

func ListDataverses2(server_url, api_token string) string {
	response, _ := con.GetRequest(server_url+"/api/search?q=*&types=dataverse&per_page=1000",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": api_token})
	//
	return (fmt.Sprintf("%s", response))
}

func GetContentByAlias(_dp tol.TDVParams, _alias string) (string, error) {
	res, err := con.GetRequest(tol.GetSiteUrl(_dp)+"/api/dataverses/"+_alias+"/contents",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}

func GetDataverseByIdOrAlias(_dp tol.TDVParams, _id string) (string, error) {
	res, err := con.GetRequest(tol.GetSiteUrl(_dp)+"/api/dataverses/"+_id,
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}

func GetDataverseByIdOrAlias2(server_url, api_token, id string) string {
	response, _ := con.GetRequest(server_url+"/api/dataverses/"+id, map[string]string{}, map[string]string{"X-Dataverse-key": api_token})
	//
	return (fmt.Sprintf("%s", response))
}

func GetDataverseStoragesizeById(_dp tol.TDVParams, _id string) (string, error) {
	res, err := con.GetRequest(tol.GetSiteUrl(_dp)+"/api/dataverses/"+_id+"/storagesize",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}

func IsDataverseRoot(_dp tol.TDVParams, _alias string) (string, error) {
	res, err := con.GetRequest(tol.GetSiteUrl(_dp)+"/api/dataverses/"+_alias+"/metadatablocks/isRoot",
		tol.ColMap{}, tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}

func CreateDataverse(_dp tol.TDVParams, _parent, _params string) (string, error) {
	res, err := con.PostRequestB(tol.GetSiteUrl(_dp)+"/api/dataverses/"+_parent,
		[]byte(_params), tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}

func DeleteDataverseById(_dp tol.TDVParams, _id string) (string, error) {
	res, err := con.DeleteRequest(tol.GetSiteUrl(_dp)+"/api/dataverses/"+_id,
		tol.ColMap{"X-Dataverse-key": tol.GetApiKey(_dp)})
	//
	return (fmt.Sprintf("%s", res)), err
}
