From 268084164794eaa5d94047d0d2ef5408d78d4498 Mon Sep 17 00:00:00 2001 From: Yang Luo Date: Sat, 19 Aug 2023 18:15:40 +0800 Subject: [PATCH] Add record_global.go --- casdoorsdk/record.go | 54 +++++++++++++------------------------ casdoorsdk/record_global.go | 31 +++++++++++++++++++++ casdoorsdk/sms.go | 26 +++++++++++++++--- casdoorsdk/sms_global.go | 34 +++-------------------- 4 files changed, 76 insertions(+), 69 deletions(-) create mode 100644 casdoorsdk/record_global.go diff --git a/casdoorsdk/record.go b/casdoorsdk/record.go index 03e2c62..b4f56ad 100644 --- a/casdoorsdk/record.go +++ b/casdoorsdk/record.go @@ -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, @@ -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) @@ -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), @@ -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 } diff --git a/casdoorsdk/record_global.go b/casdoorsdk/record_global.go new file mode 100644 index 0000000..f8cb62e --- /dev/null +++ b/casdoorsdk/record_global.go @@ -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) +} diff --git a/casdoorsdk/sms.go b/casdoorsdk/sms.go index 0e15f64..90f5a03 100644 --- a/casdoorsdk/sms.go +++ b/casdoorsdk/sms.go @@ -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. @@ -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 } diff --git a/casdoorsdk/sms_global.go b/casdoorsdk/sms_global.go index 8d837cc..0e15f64 100644 --- a/casdoorsdk/sms_global.go +++ b/casdoorsdk/sms_global.go @@ -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. @@ -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...) }