Skip to content

Commit a952a5f

Browse files
committed
initial version of go-habbo Habbo API client
0 parents  commit a952a5f

22 files changed

+3358
-0
lines changed

.github/dependabot.yml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "gomod"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"
7+
- package-ecosystem: "github-actions"
8+
directory: "/"
9+
schedule:
10+
interval: "weekly"

.github/workflows/go.yml

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ main ]
6+
pull_request:
7+
branches: [ main ]
8+
9+
jobs:
10+
11+
build:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v3
18+
with:
19+
go-version: 1.18
20+
21+
- name: Build
22+
run: go build -v ./...
23+
24+
- name: Test
25+
run: go test -v ./...

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/go-habbo

README.md

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# go-habbo
2+
3+
Library to deal with unofficial Habbo API.
4+
5+
## Installation
6+
7+
go get github.com/go-habbo/habbo
8+
9+
## Usage
10+
11+
```go
12+
parser := client.NewParser(http.DefaultClient)
13+
api := client.NewHabboAPI(parser)
14+
15+
habbo, err := api.GetHabboByName(ctx, "com", "myHabboName")
16+
if err != nil {
17+
// handle error
18+
}
19+
```

client.go

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"regexp"
6+
7+
"github.com/gerbenjacobs/go-habbo/habbo"
8+
)
9+
10+
var uniqueIdRegexp = regexp.MustCompile(`^hh\w{2}-[a-zA-Z\d]{32}$`)
11+
12+
// HabboParser is an interface that the Habbo API uses to fetch data.
13+
type HabboParser interface {
14+
habbo(ctx context.Context, hotel string, habboID string, byName bool) (*habbo.Habbo, error)
15+
profile(ctx context.Context, hotel string, habboID string) (*habbo.Profile, error)
16+
}
17+
18+
// HabboAPI is a Habbo API client.
19+
type HabboAPI struct {
20+
parser HabboParser
21+
}
22+
23+
// NewHabboAPI creates a new Habbo API client.
24+
func NewHabboAPI(parser HabboParser) *HabboAPI {
25+
return &HabboAPI{parser: parser}
26+
}
27+
28+
// GetHabbo fetches a Habbo by ID.
29+
func (c *HabboAPI) GetHabbo(ctx context.Context, hotel string, habboID string) (*habbo.Habbo, error) {
30+
if !habbo.IsValidHotel(hotel) {
31+
return nil, ErrInvalidHotel
32+
}
33+
if !uniqueIdRegexp.MatchString(habboID) {
34+
return nil, ErrInvalidUniqueID
35+
}
36+
return c.parser.habbo(ctx, hotel, habboID, true)
37+
}
38+
39+
// GetHabboByName fetches a Habbo by name.
40+
func (c *HabboAPI) GetHabboByName(ctx context.Context, hotel string, habboName string) (*habbo.Habbo, error) {
41+
if !habbo.IsValidHotel(hotel) {
42+
return nil, ErrInvalidHotel
43+
}
44+
return c.parser.habbo(ctx, hotel, habboName, false)
45+
}
46+
47+
// GetProfile fetches a Habbo's profile.
48+
func (c *HabboAPI) GetProfile(ctx context.Context, hotel string, habboID string) (*habbo.Profile, error) {
49+
if !habbo.IsValidHotel(hotel) {
50+
return nil, ErrInvalidHotel
51+
}
52+
return c.parser.profile(ctx, hotel, habboID)
53+
}

0 commit comments

Comments
 (0)