Skip to content

Commit

Permalink
fix: Add record_global.go
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Aug 19, 2023
1 parent 94e40fd commit 15b6ec6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 69 deletions.
54 changes: 19 additions & 35 deletions casdoorsdk/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,31 +41,6 @@ type Record struct {
IsTriggered bool `json:"isTriggered"`
}

func (c *Client) AddRecord(record *Record) (bool, error) {
if record.Owner == "" {
record.Owner = c.OrganizationName
}
if record.Organization == "" {
record.Organization = c.OrganizationName
}

postBytes, err := json.Marshal(record)
if err != nil {
return false, err
}

resp, err := c.DoPost("add-record", nil, postBytes, false, false)
if err != nil {
return false, err
}

return resp.Data == "Affected", nil
}

func AddRecord(record *Record) (bool, error) {
return globalClient.AddRecord(record)
}

func (c *Client) GetRecords() ([]*Record, error) {
queryMap := map[string]string{
"owner": c.OrganizationName,
Expand All @@ -86,10 +61,6 @@ func (c *Client) GetRecords() ([]*Record, error) {
return records, nil
}

func GetRecords() ([]*Record, error) {
return globalClient.GetRecords()
}

func (c *Client) GetPaginationRecords(p int, pageSize int, queryMap map[string]string) ([]*Record, int, error) {
queryMap["owner"] = c.OrganizationName
queryMap["p"] = strconv.Itoa(p)
Expand All @@ -110,10 +81,6 @@ func (c *Client) GetPaginationRecords(p int, pageSize int, queryMap map[string]s
return records, int(response.Data2.(float64)), nil
}

func GetPaginationRecords(p int, pageSize int, queryMap map[string]string) ([]*Record, int, error) {
return globalClient.GetPaginationRecords(p, pageSize, queryMap)
}

func (c *Client) GetRecord(name string) (*Record, error) {
queryMap := map[string]string{
"id": fmt.Sprintf("%s/%s", c.OrganizationName, name),
Expand All @@ -134,6 +101,23 @@ func (c *Client) GetRecord(name string) (*Record, error) {
return record, nil
}

func GetRecord(name string) (*Record, error) {
return globalClient.GetRecord(name)
func (c *Client) AddRecord(record *Record) (bool, error) {
if record.Owner == "" {
record.Owner = c.OrganizationName
}
if record.Organization == "" {
record.Organization = c.OrganizationName
}

postBytes, err := json.Marshal(record)
if err != nil {
return false, err
}

resp, err := c.DoPost("add-record", nil, postBytes, false, false)
if err != nil {
return false, err
}

return resp.Data == "Affected", nil
}
31 changes: 31 additions & 0 deletions casdoorsdk/record_global.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package casdoorsdk

func GetRecords() ([]*Record, error) {
return globalClient.GetRecords()
}

func GetPaginationRecords(p int, pageSize int, queryMap map[string]string) ([]*Record, int, error) {
return globalClient.GetPaginationRecords(p, pageSize, queryMap)
}

func GetRecord(name string) (*Record, error) {
return globalClient.GetRecord(name)
}

func AddRecord(record *Record) (bool, error) {
return globalClient.AddRecord(record)
}
26 changes: 23 additions & 3 deletions casdoorsdk/sms.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,6 +14,26 @@

package casdoorsdk

func SendSms(content string, receivers ...string) error {
return globalClient.SendSms(content, receivers...)
import "encoding/json"

type smsForm struct {
Content string `json:"content"`
Receivers []string `json:"receivers"`
}

func (c *Client) SendSms(content string, receivers ...string) error {
form := smsForm{
Content: content,
Receivers: receivers,
}
postBytes, err := json.Marshal(form)
if err != nil {
return err
}

_, err = c.DoPost("send-sms", nil, postBytes, false, false)
if err != nil {
return err
}
return nil
}
34 changes: 3 additions & 31 deletions casdoorsdk/sms_global.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The Casdoor Authors. All Rights Reserved.
// Copyright 2021 The Casdoor Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -14,34 +14,6 @@

package casdoorsdk

import (
"encoding/json"
"fmt"
)

type smsForm struct {
Content string `json:"content"`
Receivers []string `json:"receivers"`
}

func (c *Client) SendSms(content string, receivers ...string) error {
form := smsForm{
Content: content,
Receivers: receivers,
}
postBytes, err := json.Marshal(form)
if err != nil {
return err
}

resp, err := c.DoPost("send-sms", nil, postBytes, false, false)
if err != nil {
return err
}

if resp.Status != "ok" {
return fmt.Errorf(resp.Msg)
}

return nil
func SendSms(content string, receivers ...string) error {
return globalClient.SendSms(content, receivers...)
}

0 comments on commit 15b6ec6

Please sign in to comment.