func CreatePermission(ctx context.Context, clientsEndpoint string, clientID string, permission KeycloakPermission, protectionAPIToken string) (string, error) {
	b, err := json.Marshal(permission)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"permission": permission,
			"err":        err.Error(),
		}, "Unable to marshal keyclaok permission struct")
		return "", errors.NewInternalError("unable to marshal keyclaok permission struct " + err.Error())
	}

	req, err := http.NewRequest("POST", clientsEndpoint+"/"+clientID+"/authz/resource-server/policy", strings.NewReader(string(b)))
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"err": err.Error(),
		}, "Unable to crete http request")
		return "", errors.NewInternalError("unable to crete http request " + err.Error())
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer "+protectionAPIToken)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"client_id":  clientID,
			"permission": permission,
			"err":        err.Error(),
		}, "Unable to crete the Keycloak permission")
		return "", errors.NewInternalError("Unable to create the Keycloak permission " + err.Error())
	}
	if res.StatusCode != http.StatusCreated {
		log.Error(ctx, map[string]interface{}{
			"client_id":       clientID,
			"permission":      permission,
			"response_status": res.Status,
			"response_body":   rest.ReadBody(res.Body),
		}, "Unable to update the Keycloak permission")
		return "", errors.NewInternalError("Unable to create the Keycloak permission. Response status: " + res.Status + ". Responce body: " + rest.ReadBody(res.Body))
	}
	jsonString := rest.ReadBody(res.Body)

	var r cretePolicyRequestResultPayload
	err = json.Unmarshal([]byte(jsonString), &r)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"client_id":   clientID,
			"permission":  permission,
			"json_string": jsonString,
		}, "Unable to unmarshal json with the create keycloak permission request result")
		return "", errors.NewInternalError(fmt.Sprintf("error when unmarshal json with the create keycloak permission request result %s ", jsonString) + err.Error())
	}

	return r.ID, nil
}
// CreatePermission creates a Keycloak permission
CreatePermission creates a Keycloak permission
// CreatePermission creates a Keycloak permission
func CreatePermission(ctx context.Context, clientsEndpoint string, clientID string, permission KeycloakPermission, protectionAPIToken string) (string, error) {
	b, err := json.Marshal(permission)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"permission": permission,
			"err":        err.Error(),
		}, "Unable to marshal keyclaok permission struct")
		return "", errors.NewInternalError("unable to marshal keyclaok permission struct " + err.Error())
	}
	req, err := http.NewRequest("POST", clientsEndpoint+"/"+clientID+"/authz/resource-server/policy", strings.NewReader(string(b)))
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"err": err.Error(),
		}, "Unable to crete http request")
		return "", errors.NewInternalError("unable to crete http request " + err.Error())
	}
	req.Header.Add("Content-Type", "application/json")
	req.Header.Add("Authorization", "Bearer "+protectionAPIToken)
	res, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"client_id":  clientID,
			"permission": permission,
			"err":        err.Error(),
		}, "Unable to crete the Keycloak permission")
		return "", errors.NewInternalError("Unable to create the Keycloak permission " + err.Error())
	}
	if res.StatusCode != http.StatusCreated {
		log.Error(ctx, map[string]interface{}{
			"client_id":       clientID,
			"permission":      permission,
			"response_status": res.Status,
			"response_body":   rest.ReadBody(res.Body),
		}, "Unable to update the Keycloak permission")
		return "", errors.NewInternalError("Unable to create the Keycloak permission. Response status: " + res.Status + ". Responce body: " + rest.ReadBody(res.Body))
	}
	jsonString := rest.ReadBody(res.Body)
	var r cretePolicyRequestResultPayload
	err = json.Unmarshal([]byte(jsonString), &r)
	if err != nil {
		log.Error(ctx, map[string]interface{}{
			"client_id":   clientID,
			"permission":  permission,
			"json_string": jsonString,
		}, "Unable to unmarshal json with the create keycloak permission request result")
		return "", errors.NewInternalError(fmt.Sprintf("error when unmarshal json with the create keycloak permission request result %s ", jsonString) + err.Error())
	}
	return r.ID, nil
}
func CreatePermission ( ctx context . Context , clientsEndpoint string , clientID string , permission KeycloakPermission , protectionAPIToken string ) ( string , error ) { b , err := json . Marshal ( permission ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : permission , " " : err . Error ( ) , } , " " ) return " " , errors . NewInternalError ( " " + err . Error ( ) ) } req , err := http . NewRequest ( " " , clientsEndpoint + " " + clientID + " " , strings . NewReader ( string ( b ) ) ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : err . Error ( ) , } , " " ) return " " , errors . NewInternalError ( " " + err . Error ( ) ) } req . Header . Add ( " " , " " ) req . Header . Add ( " " , " " + protectionAPIToken ) res , err := http . DefaultClient . Do ( req ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : clientID , " " : permission , " " : err . Error ( ) , } , " " ) return " " , errors . NewInternalError ( " " + err . Error ( ) ) } if res . StatusCode != http . StatusCreated { log . Error ( ctx , map [ string ] interface { } { " " : clientID , " " : permission , " " : res . Status , " " : rest . ReadBody ( res . Body ) , } , " " ) return " " , errors . NewInternalError ( " " + res . Status + " " + rest . ReadBody ( res . Body ) ) } jsonString := rest . ReadBody ( res . Body ) var r cretePolicyRequestResultPayload err = json . Unmarshal ( [ ] byte ( jsonString ) , & r ) if err != nil { log . Error ( ctx , map [ string ] interface { } { " " : clientID , " " : permission , " " : jsonString , } , " " ) return " " , errors . NewInternalError ( fmt . Sprintf ( " " , jsonString ) + err . Error ( ) ) } return r . ID , nil }
CreatePermission creates a Keycloak permission
CreatePermission creates a Keycloak permission