Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
nothub committed Aug 30, 2022
0 parents commit a5cda18
Show file tree
Hide file tree
Showing 15 changed files with 736 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
14 changes: 14 additions & 0 deletions .github/dependabot.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: 2
updates:

- package-ecosystem: "gomod"
directory: "/"
schedule:
interval: "daily"
assignees:
- "nothub"
reviewers:
- "nothub"
commit-message:
prefix: "gomod"
include: "scope"
14 changes: 14 additions & 0 deletions .github/workflows/verify.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: "🚔"
on: [ push, pull_request ]
jobs:
verify:
name: "Verify"
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: 'go.mod'
check-latest: true
cache: true
- run: make lint test
13 changes: 13 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
*
!*/

!/.github/**/*.yaml

!/.gitattributes
!/.gitignore

!/Makefile

!/go.mod
!/go.sum
!/**/*.go
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
MODNAME = $(shell go list -m)
BINNAME = $(shell basename $(MODNAME))
THREADS = $(shell grep -c -E "^processor.*[0-9]+" "/proc/cpuinfo")

$(BINNAME): lint test
go build -race -o $@

clean:
go clean
go mod tidy

lint:
go vet

test:
go test $(MODNAME)/api -parallel $(THREADS)

.PHONY: clean lint test
165 changes: 165 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
package api

import (
"testing"
)

func Test_GetProject_Success(t *testing.T) {
t.Parallel()
c := NewClient()
project, err := c.GetProject("fabric-api")
if err != nil {
t.Fatal(err)
}
if project.Slug != "fabric-api" {
t.Fatal("wrong slug!")
}
if project.ProjectType != "mod" {
t.Fatal("wrong type!")
}
}

func Test_GetProject_404(t *testing.T) {
t.Parallel()
c := NewClient()
_, err := c.GetProject("x")
if err.Error() != "http status 404" {
t.Fatal("wrong status!")
}
}

func TestClient_GetProjects_Count(t *testing.T) {
t.Parallel()
c := NewClient()
projects, err := c.GetProjects([]string{"P7dR8mSH", "XxWD5pD3", "x"})
if err != nil {
t.Fatal(err)
}
if len(projects) != 2 {
t.Fatal("wrong count!")
}
}

func TestClient_GetProjects_Slug(t *testing.T) {
t.Parallel()
c := NewClient()
projects, err := c.GetProjects([]string{"P7dR8mSH"})
if err != nil {
t.Fatal(err)
}
if projects[0].Slug != "fabric-api" {
t.Fatal("wrong slug!")
}
}

func TestClient_CheckProjectValidity_Slug(t *testing.T) {
t.Parallel()
c := NewClient()
response, err := c.CheckProjectValidity("fabric-api")
if err != nil {
t.Fatal(err)
}
if response.Id != "P7dR8mSH" {
t.Fatal("wrong id!")
}
}

func TestClient_CheckProjectValidity_Id(t *testing.T) {
t.Parallel()
c := NewClient()
response, err := c.CheckProjectValidity("P7dR8mSH")
if err != nil {
t.Fatal(err)
}
if response.Id != "P7dR8mSH" {
t.Fatal("wrong id!")
}
}

func TestClient_GetDependencies(t *testing.T) {
t.Parallel()
c := NewClient()
dependencies, err := c.GetDependencies("rinthereout")
if err != nil {
t.Fatal(err)
}
if len(dependencies.Projects) < 1 {
t.Fatal("wrong count!")
}
}

func TestClient_GetProjectVersions_Count(t *testing.T) {
t.Parallel()
c := NewClient()
versions, err := c.GetProjectVersions("fabric-api", &GetProjectVersionsParams{})
if err != nil {
t.Fatal(err)
}
if len(versions) < 1 {
t.Fatal("wrong count!")
}
}

func TestClient_GetProjectVersions_Filter_Results(t *testing.T) {
t.Parallel()
c := NewClient()
versions, err := c.GetProjectVersions("fabric-api", &GetProjectVersionsParams{
GameVersions: []string{"1.16.5"},
})
if err != nil {
t.Fatal(err)
}
if len(versions) < 1 {
t.Fatal("wrong count!")
}
}

func TestClient_GetProjectVersions_Filter_NoResults(t *testing.T) {
t.Parallel()
c := NewClient()
versions, err := c.GetProjectVersions("fabric-api", &GetProjectVersionsParams{
Loaders: []string{"forge"},
})
if err != nil {
t.Fatal(err)
}
if len(versions) > 0 {
t.Fatal("wrong count!")
}
}

func TestClient_GetVersion(t *testing.T) {
t.Parallel()
c := NewClient()
version, err := c.GetVersion("IQ3UGSc2")
if err != nil {
t.Fatal(err)
}
if version.ProjectId != "P7dR8mSH" {
t.Fatal("wrong parent id!")
}
}

func TestClient_GetVersions(t *testing.T) {
t.Parallel()
c := NewClient()
versions, err := c.GetVersions([]string{"IQ3UGSc2", "DrzwF8io", "foobar"})
if err != nil {
t.Fatal(err)
}
if len(versions) != 2 {
t.Fatal("wrong count!")
}
}

func TestClient_VersionFromHash(t *testing.T) {
t.Parallel()
c := NewClient()
version, err := c.VersionFromHash("619e250c133106bacc3e3b560839bd4b324dfda8", "sha1")
if err != nil {
t.Fatal(err)
}
if version.Id != "d5nXweHE" {
t.Fatal("wrong id!")
}
}
75 changes: 75 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"runtime/debug"
"strconv"
)

type Client struct {
UserAgent string
HTTPClient *http.Client
}

func NewClient() *Client {
userAgent := "gorinth"
info, ok := debug.ReadBuildInfo()
if ok {
userAgent = info.Main.Path + "/" + info.Main.Version
}
return &Client{
UserAgent: userAgent,
HTTPClient: &http.Client{},
}
}

func (client *Client) buildRequest(method string, url string, body io.Reader) *http.Request {
request, err := http.NewRequest(method, url, body)
if err != nil {
log.Fatalln(err)
}

request.Header.Set("User-Agent", client.UserAgent)
request.Header.Set("Accept", "application/json")
if request.Method == "POST" || request.Method == "PATCH" || request.Method == "PUT" {
request.Header.Set("Content-Type", "application/json")
}

request.Close = true

return request
}

func (client *Client) sendRequest(method string, url string, body io.Reader, result interface{}) error {
response, err := client.HTTPClient.Do(client.buildRequest(method, url, body))
if err != nil {
return err
}

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
fmt.Println(err)
}
}(response.Body)

if response.StatusCode < http.StatusOK || response.StatusCode >= http.StatusBadRequest {
var e Error
if json.NewDecoder(response.Body).Decode(&e) != nil {
return errors.New("http status " + strconv.Itoa(response.StatusCode))
}
return errors.New("http status " + strconv.Itoa(response.StatusCode) + " - " + e.Error + " " + e.Description)
}

err = json.NewDecoder(response.Body).Decode(&result)
if err != nil {
return errors.New("http status " + strconv.Itoa(response.StatusCode) + " - " + err.Error())
}

return nil
}
Loading

0 comments on commit a5cda18

Please sign in to comment.