Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shizumico committed Dec 3, 2024
0 parents commit a34aded
Show file tree
Hide file tree
Showing 34 changed files with 904 additions and 0 deletions.
25 changes: 25 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]

jobs:
tests:
runs-on: macos
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.23
- name: Run linter
uses: golangci/golangci-lint-action@v3
with:
version: v1.61
- name: Run tests
run: go test -v ./tests/...
env:
TOKEN: ${{ secrets.TOKEN }}
GROUP_ID: ${{ secrets.GROUP_ID }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.idea
.env
go.sum
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 AEJoy

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<img src="cupcake.png"/>

<p align="center">Package for «Keksik» API in VK Mini App</p>

<div align="center">
<a href="https://pkg.go.dev/github.com/aejoy/keksikgo">
<img src="https://img.shields.io/static/v1?label=go&message=reference&color=00add8&logo=go" />
</a>
<a href="http://www.opensource.org/licenses/MIT">
<img src="https://img.shields.io/badge/license-MIT-fuchsia.svg" />
</a>
<a href="https://goreportcard.com/report/github.com/aejoy/keksikgo">
<img src="https://goreportcard.com/badge/github.com/aejoy/keksikgo" />
</a>
</div>

# Installation

```bash
go get github.com/aejoy/keksikgo
```

# Usage

## Methods

```go
package main

import (
"fmt"
"os"

"github.com/aejoy/keksikgo/api"
)

func main() {
app := api.New(os.Getenv("TOKEN"), 123)
fmt.Println(app.Balance())
}
```

## Callback

```go
package main

import (
"fmt"
"log"
"os"

"github.com/aejoy/keksikgo/api"
"github.com/aejoy/keksikgo/callback"
"github.com/aejoy/keksikgo/update"
"github.com/gofiber/fiber/v3"
"go.uber.org/zap"
)

func main() {
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}

cake := api.New(123, os.Getenv("TOKEN"), logger)

app := fiber.New()
session := callback.New(cake)

session.Donate(func(_ *api.API, donate update.Donate) {
fmt.Printf("New donate: %+v\n", donate)
})

app.Post("/callback/:confirmation", session.Fiber)

log.Fatalln(app.Listen(":3000"))
}
```
81 changes: 81 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package api

import (
"bytes"
"context"
"encoding/json"
"net/http"

"go.uber.org/zap"
)

type API struct {
Version int
Group int
Token string
Client *http.Client
Logger *zap.Logger
}

func New(group int, token string, logger *zap.Logger) *API {
api := &API{
Version: 1,
Group: group,
Token: token,
Client: http.DefaultClient,
Logger: logger,
}

return api
}

func (api *API) Call(context context.Context, method string, params any, data any) error {
body, err := json.Marshal(params)
if err != nil {
api.Logger.Error(err.Error(), zap.String("type", "MarshalParams"))
return err
}

api.Logger.Info("NewMethod",
zap.String("method", method),
zap.String("body", string(body)))

request, err := http.NewRequestWithContext(
context, http.MethodGet,
"https://api.keksik.io/"+method,
bytes.NewBuffer(body),
)
if err != nil {
api.Logger.Error(err.Error(), zap.String("type", "NewRequest"))
return err
}

response, err := api.Client.Do(request)
if err != nil {
api.Logger.Error(err.Error(), zap.String("type", "DoRequest"))
return err
}

defer response.Body.Close()

if err = json.NewDecoder(response.Body).Decode(&data); err != nil {
api.Logger.Error(err.Error(), zap.String("type", "DecodeResponse"))
return err
}

return nil
}

func (api *API) NewMapRequiredParams(keyValues map[string]any) map[string]any {
params := map[string]any{
"v": api.Version,
"group": api.Group,
"token": api.Token,
}

for key, value := range keyValues {
params[key] = value
}

return params
}
10 changes: 10 additions & 0 deletions api/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package api

import (
"context"
"github.com/aejoy/keksikgo/responses"
)

func (api *API) Balance(context context.Context) (balance responses.Balance, err error) {
return balance, api.Call(context, "balance", api.NewMapRequiredParams(nil), &balance)
}
29 changes: 29 additions & 0 deletions api/campaigns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package api

import (
"context"
"github.com/aejoy/keksikgo/responses"
"github.com/aejoy/keksikgo/types"
)

func (api *API) GetCampaigns(context context.Context, ids []int) (campaigns responses.Campaigns, err error) {
return campaigns, api.Call(context, "campaigns/get", api.NewMapRequiredParams(map[string]any{"ids": ids}), &campaigns)
}

func (api *API) GetActiveCampaign(context context.Context) (activeCampaign responses.ActiveCampaign, err error) {
return activeCampaign, api.Call(context, "campaigns/get-active", api.NewMapRequiredParams(nil), &activeCampaign)
}

func (api *API) GetCampaignRewards(context context.Context, campaign int) (campaignRewards responses.CampaignRewards, err error) {
return campaignRewards, api.Call(context, "campaigns/get-rewards", api.NewMapRequiredParams(map[string]any{"campaign": campaign}), &campaignRewards)
}

func (api *API) ChangeCampaign(context context.Context, changeCampaigns types.ChangeCampaign) (campaigns responses.ChangeCampaign, err error) {
changeCampaigns.SetRequiredFields(api.Version, api.Group, api.Token)
return campaigns, api.Call(context, "campaigns/change", changeCampaigns, &campaigns)
}

func (api *API) ChangeCampaignReward(context context.Context, changeCampaignReward types.ChangeCampaignReward) (campaignReward responses.ChangeCampaign, err error) {
changeCampaignReward.SetRequiredFields(api.Version, api.Group, api.Token)
return campaignReward, api.Call(context, "campaigns/change-reward", changeCampaignReward, &campaignReward)
}
32 changes: 32 additions & 0 deletions api/donates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api

import (
"context"
"github.com/aejoy/keksikgo/responses"
"github.com/aejoy/keksikgo/types"
)

func (api *API) GetDonates(context context.Context, getDonates types.GetDonates) (donates responses.Donates, err error) {
getDonates.SetRequiredFields(api.Version, api.Group, api.Token)
return donates, api.Call(context, "donates/get", getDonates, &donates)
}

func (api *API) GetLastDonates(context context.Context, getLastDonates types.GetLastDonates) (donates responses.Donates, err error) {
getLastDonates.SetRequiredFields(api.Version, api.Group, api.Token)
return donates, api.Call(context, "donates/get-last", getLastDonates, &donates)
}

func (api *API) ChangeStatus(context context.Context, changeStatus types.ChangeStatus) (status responses.Status, err error) {
changeStatus.SetRequiredFields(api.Version, api.Group, api.Token)
return status, api.Call(context, "donates/change-status", changeStatus, &status)
}

func (api *API) Answer(context context.Context, answer types.Answer) (info responses.Answer, err error) {
answer.SetRequiredFields(api.Version, api.Group, api.Token)
return info, api.Call(context, "donates/answer", answer, &info)
}

func (api *API) ChangeReward(context context.Context, changeReward types.ChangeReward) (info responses.ChangeReward, err error) {
changeReward.SetRequiredFields(api.Version, api.Group, api.Token)
return info, api.Call(context, "donates/change-reward-status", changeReward, &info)
}
16 changes: 16 additions & 0 deletions api/payments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package api

import (
"context"
"github.com/aejoy/keksikgo/responses"
"github.com/aejoy/keksikgo/types"
)

func (api *API) GetPayments(context context.Context, ids []int) (payments responses.Payments, err error) {
return payments, api.Call(context, "payments/get", api.NewMapRequiredParams(map[string]any{"ids": ids}), &payments)
}

func (api *API) CreatePayments(context context.Context, createPayments types.CreatePayments) (payments responses.CreatePayments, err error) {
createPayments.SetRequiredFields(api.Version, api.Group, api.Token)
return payments, api.Call(context, "payments/create", createPayments, &payments)
}
12 changes: 12 additions & 0 deletions callback/callback.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package callback

import "github.com/aejoy/keksikgo/api"

type Callback struct {
API *api.API
Scenes Scenes
}

func New(api *api.API) *Callback {
return &Callback{API: api}
}
22 changes: 22 additions & 0 deletions callback/scenes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package callback

import (
"github.com/aejoy/keksikgo/api"
"github.com/aejoy/keksikgo/update"
)

type Scenes struct {
Donate Donate
Payment Payment
}

type Donate func(*api.API, update.Donate)
type Payment func(*api.API, update.Payment)

func (callback *Callback) Donate(donate Donate) {
callback.Scenes.Donate = donate
}

func (callback *Callback) Payment(payment Payment) {
callback.Scenes.Payment = payment
}
35 changes: 35 additions & 0 deletions callback/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package callback

import (
"encoding/json"
"fmt"
"github.com/aejoy/keksikgo/update"
"github.com/gofiber/fiber/v3"
"go.uber.org/zap"
)

func (callback *Callback) Fiber(context fiber.Ctx) error {
update := update.Update{}
if err := json.Unmarshal(context.Body(), &update); err != nil {
callback.API.Logger.Error(err.Error(), zap.String("type", "UnmarshallUpdate"))
return err
}

if callback.API.Logger != nil {
callback.API.Logger.Info(string(context.Body()), zap.String("type", "NewUpdate"))
}

if update.Type == "confirmation" {
confirmation := context.Params("confirmation", "no_confirmation_param")
return context.SendString(fmt.Sprintf(`{"status": "ok", "code": "%s"}`, confirmation))
} else {
switch update.Type {
case "new_donate":
callback.Scenes.Donate(callback.API, update.Donate)
case "payment_status":
callback.Scenes.Payment(callback.API, update.Payment)
}
}

return context.SendString(`{"status": "ok"}`)
}
Loading

0 comments on commit a34aded

Please sign in to comment.