Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

spamwatch-go v1 #1

Merged
merged 9 commits into from
Oct 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# <a href="https://telegram.me/SpamWatch"><img src="https://avatars.githubusercontent.com/u/37397813?s=200&v=4" width="35px" align="left"></img></a> SpamWatch API Go Wrapper

[![Go Reference](https://pkg.go.dev/badge/github.com/SpamWatch/spamwatch-go.svg)](https://pkg.go.dev/github.com/SpamWatch/spamwatch-go) [![GPLv3 license](https://img.shields.io/badge/License-GPLv3-blue.svg)](http://perso.crans.org/besson/LICENSE.html)

spamwatch-go is official Go wrapper for [SpamWatch API](https://api.spamwat.ch), which is fast, secure and requires no additional packages to be installed.

<hr/>

## Features

- Can use custom SpamWatch API endpoint with the help of ClientOpts.
- It's in pure go, no need to install any kind of plugin or include any kind of additional files.
- No third party library bloat; only uses standard library.
- Type safe; no weird `interface{}` logic.

<hr/>

## Getting started

You can easily download the library with the standard `go get` command:

```bash
go get github.com/SpamWatch/spamwatch-go
```

Full documentation of this API, can be found [here](https://docs.spamwat.ch/).

<hr/>

## Basic Usage

```go
package main

import (
"fmt"

"github.com/SpamWatch/spamwatch-go"
)

var client = spamwatch.Client("API_KEY", nil)

func main() {
ban, err := client.GetBan(777000)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(ban)
}
```

Still need more examples? Take a look at the [examples directory](examples).

Ask your doubts at the [support group](https://telegram.me/SpamWatchSupport).

<hr/>

## License

[![GNU General Public License v3.0](https://www.gnu.org/graphics/gplv3-127x51.png)](https://www.gnu.org/licenses/gpl-3.0.en.html#header)

The spamwatch-go project is under the [GPL-3.0](https://opensource.org/licenses/GPL-3.0) license. You can find the license file [here](LICENSE).

11 changes: 11 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package spamwatch

const DEFAULT_API_URL = "https://api.spamwat.ch/"

const SPAMWATCH_ERROR_PREFIX = "SpamWatch-Error"

const UserPermission = "User"

const AdminPermission = "Admin"

const RootPermission = "Root"
32 changes: 32 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package spamwatch

import (
"encoding/json"
"fmt"
"time"
)

func handleError(sw *SpamWatch, s int, b []byte, method string) error {
if s != 200 && s != 201 && s != 204 {
var e = Error{}
err := json.Unmarshal(b, &e)
if err != nil {
return err
}
return &ErrorHandler{Err: &e, Spamwatch: sw, Method: method}
}
return nil
}

func (e *ErrorHandler) Error() string {
switch e.Err.Code {
case 401:
return fmt.Sprintf("%s: Make sure your API Token is correct", SPAMWATCH_ERROR_PREFIX)
case 403:
return fmt.Sprintf("%s: Your token's permission '%s' is not high enough", SPAMWATCH_ERROR_PREFIX, e.Spamwatch.Token.Permission)
case 429:
return fmt.Sprintf("%s: Too Many Requests for method '%s': Try again in %d seconds", SPAMWATCH_ERROR_PREFIX, e.Method, e.Err.Until-time.Now().Unix())
default:
return fmt.Sprintf("%s: %s: %d-%s", SPAMWATCH_ERROR_PREFIX, e.Method, e.Err.Code, e.Err.Description)
}
}
41 changes: 41 additions & 0 deletions examples/banlist/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package main

import (
"fmt"

"github.com/SpamWatch/spamwatch-go"
)

var client = spamwatch.Client("API_KEY", nil)

func main() {
// Getting a specific ban
ban, err := client.GetBan(777000)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(ban)

// Getting all bans
bans, err := client.GetBans()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(bans)

// Getting a list of banned ids
bannedIds, err := client.GetBansMin()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(bannedIds)

// Adding a ban
client.AddBan(777000, "reason", "message")

// Deleting a ban
client.DeleteBan(777000)
}
27 changes: 27 additions & 0 deletions examples/misc/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"

"github.com/SpamWatch/spamwatch-go"
)

var client = spamwatch.Client("API_KEY", nil)

func main() {
// Getting the API version
v, err := client.Version()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(v)

// Getting some stats
stats, err := client.Stats()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(stats.TotalBansCount)
}
49 changes: 49 additions & 0 deletions examples/tokens/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"fmt"

"github.com/SpamWatch/spamwatch-go"
)

var client = spamwatch.Client("API_KEY", nil)

func main() {
// Getting your own Token
myToken, err := client.GetSelf()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(myToken)

// Getting all Tokens
tokens, err := client.GetTokens()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(tokens)

// Getting a specific Token
Token, err := client.GetToken(1)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(Token)

// Getting a Users tokens
tokens, err = client.GetUserTokens(777000)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(tokens)

// Creating a Token
client.CreateToken(777000, spamwatch.UserPermission)

// Retiring a specific Token
client.DeleteToken(1)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/SpamWatch/spamwatch-go

go 1.17
Loading