~sircmpwn/core-go

864816cfbc0ca3cc86b9e7248196486b245545c7 — Simon Ser 4 months ago 23808bb
client: rename Execute to Do

583d0b1bcb08 ("client/graphql: handle GraphQL errors") changed
Execute behavior by wrapping the result into a struct with "data"
and "errors" fields. This is a breaking change, but it's hard to
spot when upgrading core-go because it won't cause a compilation
error.

Rename Execute to Do to break the build and force callers to update
accordingly.
2 files changed, 20 insertions(+), 29 deletions(-)

M auth/middleware.go
M client/graphql.go
M auth/middleware.go => auth/middleware.go +19 -28
@@ 320,23 320,6 @@ func FetchMetaProfile(ctx context.Context, username string, user *AuthContext) e
		panic(errors.New("Cannot fetch profile from ourselves"))
	}

	type GraphQLProfile struct {
		ID               int     `json:"id"`
		Username         string  `json:"username"`
		Email            string  `json:"email"`
		URL              *string `json:"url"`
		Location         *string `json:"location"`
		Bio              *string `json:"bio"`
		UserType         string  `json:"userType"`
		SuspensionNotice string  `json:"suspensionNotice"`
	}

	type GraphQLResponse struct {
		Data struct {
			Me GraphQLProfile `json:"me"`
		} `json:"data"`
	}

	query := client.GraphQLQuery{
		Query: `
			query {


@@ 352,13 335,24 @@ func FetchMetaProfile(ctx context.Context, username string, user *AuthContext) e
			}`,
	}

	var result GraphQLResponse
	if err := client.Execute(ctx, username,
	var result struct {
		Me struct {
			ID               int     `json:"id"`
			Username         string  `json:"username"`
			Email            string  `json:"email"`
			URL              *string `json:"url"`
			Location         *string `json:"location"`
			Bio              *string `json:"bio"`
			UserType         string  `json:"userType"`
			SuspensionNotice string  `json:"suspensionNotice"`
		} `json:"me"`
	}
	if err := client.Do(ctx, username,
		"meta.sr.ht", query, &result); err != nil {
		return err
	}

	profile := result.Data.Me
	profile := result.Me
	return database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		// TODO: Make the database representation consistent with this
		ut := strings.ToLower(profile.UserType)


@@ 474,11 468,6 @@ func LookupUser(ctx context.Context, username string, user *AuthContext) error {
// should not be trusted)
func LookupTokenRevocation(ctx context.Context,
	username string, hash [64]byte, clientID string) (bool, error) {
	type GraphQLResponse struct {
		Data struct {
			RevocationStatus bool `json:"tokenRevocationStatus"`
		} `json:"data"`
	}

	query := client.GraphQLQuery{
		Query: `


@@ 491,12 480,14 @@ func LookupTokenRevocation(ctx context.Context,
		},
	}

	var result GraphQLResponse
	if err := client.Execute(ctx, username,
	var result struct {
		RevocationStatus bool `json:"tokenRevocationStatus"`
	}
	if err := client.Do(ctx, username,
		"meta.sr.ht", query, &result); err != nil {
		return true, err
	}
	return result.Data.RevocationStatus, nil
	return result.RevocationStatus, nil
}

func OAuth2(token string, hash [64]byte, w http.ResponseWriter,

M client/graphql.go => client/graphql.go +1 -1
@@ 25,7 25,7 @@ type InternalAuth struct {
	NodeID   string `json:"node_id"`
}

func Execute(ctx context.Context, username string, svc string,
func Do(ctx context.Context, username string, svc string,
	query GraphQLQuery, result interface{}) error {

	body, err := json.Marshal(query)