~sircmpwn/abused-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
1

[RFC PATCH abused] Add generated client package

Details
Message ID
<20240207103820.1141450-1-ch@bitfehler.net>
DKIM signature
pass
Download raw message
Patch: +185 -1
---
This is what I was thinking of. I think it could also be useful to add a
simple CLI tool to this repo. We'd be checking in generated code, but I
think this is a decent usecase for that?

 client/client.go       | 135 +++++++++++++++++++++++++++++++++++++++++
 client/gen.go          |  10 +++
 client/queries.graphql |  33 ++++++++++
 go.mod                 |   4 +-
 go.sum                 |   4 ++
 5 files changed, 185 insertions(+), 1 deletion(-)
 create mode 100644 client/client.go
 create mode 100644 client/gen.go
 create mode 100644 client/queries.graphql

diff --git a/client/client.go b/client/client.go
new file mode 100644
index 0000000..dd4ece3
--- /dev/null
+++ b/client/client.go
@@ -0,0 +1,135 @@
// Code generated by gqlclientgen - DO NOT EDIT.

package client

import (
	"context"
	gqlclient "git.sr.ht/~emersion/gqlclient"
)

// The outcome of an abuse check operation.
type Analysis struct {
	// The outcome of the entire ruleset.
	Outcome Outcome `json:"outcome"`
	// List of applicable rules and their outcomes.
	Rules []RuleOutcome `json:"rules"`
}

// Enumerates the possible outcomes of an abuse check operation.
type Outcome string

const (
	// All rules passed and the requested action should be allowed to proceed.
	OutcomePass Outcome = "PASS"
	// One more more rules failed the abuse check.
	OutcomeFail Outcome = "FAIL"
)

// The outcome of an anti-abuse rule.
type RuleOutcome struct {
	// Name of applicable rule
	Name string `json:"name"`
	// Outcome of this rule
	Outcome Outcome `json:"outcome"`
}

// Input for query { analyze }
//
// All fields are optional. Analysis is performed on the rulesets which support
// the analysis of whichever fields are provided.
//
// For instance, to analyze a registration attempt, you might include their IP
// address, email address, and username. To analyze a comment submission, you
// might included the text of the comment.
type Sample struct {
	IpAddress *string `json:"ipAddress,omitempty"`
	Email     *string `json:"email,omitempty"`
	Username  *string `json:"username,omitempty"`
	Domain    *string `json:"domain,omitempty"`
	Url       *string `json:"url,omitempty"`
	Text      *string `json:"text,omitempty"`
	// Which rate limit bucket to apply, if appropriate.
	RateLimit *string `json:"rateLimit,omitempty"`
}

func Analyze(client *gqlclient.Client, ctx context.Context, sample Sample) (analyze *Analysis, err error) {
	op := gqlclient.NewOperation("query Analyze ($sample: Sample!) {\n\tanalyze(sample: $sample) {\n\t\toutcome\n\t\trules {\n\t\t\tname\n\t\t\toutcome\n\t\t}\n\t}\n}\n")
	op.Var("sample", sample)
	var respData struct {
		Analyze *Analysis
	}
	err = client.Execute(ctx, op, &respData)
	return respData.Analyze, err
}

func ReportSample(client *gqlclient.Client, ctx context.Context, sample Sample, federate bool, reason string) (reportSample bool, err error) {
	op := gqlclient.NewOperation("mutation ReportSample ($sample: Sample!, $federate: Boolean!, $reason: String!) {\n\treportSample(sample: $sample, federate: $federate, reason: $reason)\n}\n")
	op.Var("sample", sample)
	op.Var("federate", federate)
	op.Var("reason", reason)
	var respData struct {
		ReportSample bool
	}
	err = client.Execute(ctx, op, &respData)
	return respData.ReportSample, err
}

func UnreportSample(client *gqlclient.Client, ctx context.Context, sample Sample, federate bool, reason string) (unreportSample bool, err error) {
	op := gqlclient.NewOperation("mutation UnreportSample ($sample: Sample!, $federate: Boolean!, $reason: String!) {\n\tunreportSample(sample: $sample, federate: $federate, reason: $reason)\n}\n")
	op.Var("sample", sample)
	op.Var("federate", federate)
	op.Var("reason", reason)
	var respData struct {
		UnreportSample bool
	}
	err = client.Execute(ctx, op, &respData)
	return respData.UnreportSample, err
}

func ReportSubnet(client *gqlclient.Client, ctx context.Context, cidr string, federate bool, reason string) (reportSubnet string, err error) {
	op := gqlclient.NewOperation("mutation ReportSubnet ($cidr: String!, $federate: Boolean!, $reason: String!) {\n\treportSubnet(cidr: $cidr, federate: $federate, reason: $reason)\n}\n")
	op.Var("cidr", cidr)
	op.Var("federate", federate)
	op.Var("reason", reason)
	var respData struct {
		ReportSubnet string
	}
	err = client.Execute(ctx, op, &respData)
	return respData.ReportSubnet, err
}

func UnreportSubnet(client *gqlclient.Client, ctx context.Context, cidr string, federate bool, reason string) (unreportSubnet string, err error) {
	op := gqlclient.NewOperation("mutation UnreportSubnet ($cidr: String!, $federate: Boolean!, $reason: String!) {\n\tunreportSubnet(cidr: $cidr, federate: $federate, reason: $reason)\n}\n")
	op.Var("cidr", cidr)
	op.Var("federate", federate)
	op.Var("reason", reason)
	var respData struct {
		UnreportSubnet string
	}
	err = client.Execute(ctx, op, &respData)
	return respData.UnreportSubnet, err
}

func ReportDEP(client *gqlclient.Client, ctx context.Context, domain string, federate bool, reason string) (reportDEP string, err error) {
	op := gqlclient.NewOperation("mutation ReportDEP ($domain: String!, $federate: Boolean!, $reason: String!) {\n\treportDEP(domain: $domain, federate: $federate, reason: $reason)\n}\n")
	op.Var("domain", domain)
	op.Var("federate", federate)
	op.Var("reason", reason)
	var respData struct {
		ReportDEP string
	}
	err = client.Execute(ctx, op, &respData)
	return respData.ReportDEP, err
}

func UnreportDEP(client *gqlclient.Client, ctx context.Context, domain string, federate bool, reason string) (unreportDEP string, err error) {
	op := gqlclient.NewOperation("mutation UnreportDEP ($domain: String!, $federate: Boolean!, $reason: String!) {\n\tunreportDEP(domain: $domain, federate: $federate, reason: $reason)\n}\n")
	op.Var("domain", domain)
	op.Var("federate", federate)
	op.Var("reason", reason)
	var respData struct {
		UnreportDEP string
	}
	err = client.Execute(ctx, op, &respData)
	return respData.UnreportDEP, err
}
diff --git a/client/gen.go b/client/gen.go
new file mode 100644
index 0000000..27f9c21
--- /dev/null
+++ b/client/gen.go
@@ -0,0 +1,10 @@
//go:build generate
// +build generate

package client

import (
	_ "git.sr.ht/~emersion/gqlclient/cmd/gqlclientgen"
)

//go:generate go run git.sr.ht/~emersion/gqlclient/cmd/gqlclientgen -s ../graph/schema.graphqls -q queries.graphql -o client.go
diff --git a/client/queries.graphql b/client/queries.graphql
new file mode 100644
index 0000000..ea06581
--- /dev/null
+++ b/client/queries.graphql
@@ -0,0 +1,33 @@
query Analyze($sample: Sample!) {
  analyze(sample: $sample) {
    outcome
    rules {
      name
      outcome
    }
  }
}

mutation ReportSample($sample: Sample!, $federate: Boolean!, $reason: String!) {
  reportSample(sample: $sample, federate: $federate, reason: $reason)
}

mutation UnreportSample($sample: Sample!, $federate: Boolean!, $reason: String!) {
  unreportSample(sample: $sample, federate: $federate, reason: $reason)
}

mutation ReportSubnet($cidr: String!, $federate: Boolean!, $reason: String!) {
  reportSubnet(cidr: $cidr, federate: $federate, reason: $reason)
}

mutation UnreportSubnet($cidr: String!, $federate: Boolean!, $reason: String!) {
  unreportSubnet(cidr: $cidr, federate: $federate, reason: $reason)
}

mutation ReportDEP($domain: String!, $federate: Boolean!, $reason: String!) {
  reportDEP(domain: $domain, federate: $federate, reason: $reason)
}

mutation UnreportDEP($domain: String!, $federate: Boolean!, $reason: String!) {
  unreportDEP(domain: $domain, federate: $federate, reason: $reason)
}
diff --git a/go.mod b/go.mod
index be9cc98..9597da3 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,9 @@ module git.sr.ht/~sircmpwn/abused
go 1.21.4

require (
	git.sr.ht/~emersion/go-oauth2 v0.0.0-20231231101254-b1c55ea5fbd5
	git.sr.ht/~emersion/go-scfg v0.0.0-20240128091534-2ae16e782082
	git.sr.ht/~emersion/gqlclient v0.0.0-20230820050442-8873fe0204b9
	git.sr.ht/~sircmpwn/dowork v0.0.0-20221010085743-46c4299d76a1
	github.com/99designs/gqlgen v0.17.43
	github.com/go-chi/chi/v5 v5.0.11
@@ -15,11 +17,11 @@ require (
)

require (
	git.sr.ht/~emersion/go-oauth2 v0.0.0-20231231101254-b1c55ea5fbd5 // indirect
	github.com/agnivade/levenshtein v1.1.1 // indirect
	github.com/beorn7/perks v1.0.1 // indirect
	github.com/cespare/xxhash/v2 v2.2.0 // indirect
	github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
	github.com/dave/jennifer v1.7.0 // indirect
	github.com/google/uuid v1.6.0 // indirect
	github.com/gorilla/websocket v1.5.1 // indirect
	github.com/mitchellh/mapstructure v1.5.0 // indirect
diff --git a/go.sum b/go.sum
index c2d5d62..ace796f 100644
--- a/go.sum
+++ b/go.sum
@@ -2,6 +2,8 @@ git.sr.ht/~emersion/go-oauth2 v0.0.0-20231231101254-b1c55ea5fbd5 h1:zf5/waL3WYqJ
git.sr.ht/~emersion/go-oauth2 v0.0.0-20231231101254-b1c55ea5fbd5/go.mod h1:VHj0jSCLIkrfEwmOvJ4+ykpoVbD/YLN7BM523oKKBHc=
git.sr.ht/~emersion/go-scfg v0.0.0-20240128091534-2ae16e782082 h1:9Udx5fm4vRtmgDIBjy2ef5QioHbzpw5oHabbhpAUyEw=
git.sr.ht/~emersion/go-scfg v0.0.0-20240128091534-2ae16e782082/go.mod h1:ybgvEJTIx5XbaspSviB3KNa6OdPmAZqDoSud7z8fFlw=
git.sr.ht/~emersion/gqlclient v0.0.0-20230820050442-8873fe0204b9 h1:QNwHP6WknvS7X6MEFxCpefQb1QJMqgIIt+vn/PVoMMg=
git.sr.ht/~emersion/gqlclient v0.0.0-20230820050442-8873fe0204b9/go.mod h1:kvl/JK0Z3VRmtbBxdOJR4ydyXVouUIcFIXgv4H6rVAY=
git.sr.ht/~sircmpwn/dowork v0.0.0-20221010085743-46c4299d76a1 h1:EvPKkneKkF/f7zEgKPqIZVyj3jWO8zSmsBOvMhAGqMA=
git.sr.ht/~sircmpwn/dowork v0.0.0-20221010085743-46c4299d76a1/go.mod h1:8neHEO3503w/rNtttnR0JFpQgM/GFhaafVwvkPsFIDw=
github.com/99designs/gqlgen v0.17.43 h1:I4SYg6ahjowErAQcHFVKy5EcWuwJ3+Xw9z2fLpuFCPo=
@@ -29,6 +31,8 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/dave/jennifer v1.7.0 h1:uRbSBH9UTS64yXbh4FrMHfgfY762RD+C7bUPKODpSJE=
github.com/dave/jennifer v1.7.0/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
-- 
2.43.0
Details
Message ID
<CYYSXOA60KH5.X6OJ4T4XJD0Q@taiga>
In-Reply-To
<20240207103820.1141450-1-ch@bitfehler.net> (view parent)
DKIM signature
pass
Download raw message
Looks good. Thanks!

To git@git.sr.ht:~sircmpwn/abused
   6fcc1f3..6810a2b  master -> master
Reply to thread Export thread (mbox)