httpclient

package
v0.0.0-...-80666a8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 10, 2023 License: MIT Imports: 9 Imported by: 3

Documentation

Overview

Package httpclient contains issues.Service implementation over HTTP.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewIssues

func NewIssues(httpClient *http.Client, scheme, host string) issues.Service

NewIssues creates a client that implements issues.Service remotely over HTTP. If a nil httpClient is provided, http.DefaultClient will be used. scheme and host can be empty strings to target local service.

Example
package main

import (
	"net/http"
	"net/http/httptest"

	"github.com/shurcooL/issuesapp/httpclient"
)

func main() {
	issuesClient := httpclient.NewIssues(nil, "http", "localhost:8080")

	// Now you can use any of issuesClient methods.

	_ = issuesClient
}

func init() {

	http.DefaultTransport.(*http.Transport).RegisterProtocol("", localRoundTripper{})
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using http.DefaultServeMux directly, instead of going over an HTTP connection.
type localRoundTripper struct{}

func (localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	w := httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(w, req)
	return w.Result(), nil
}
Output:

Example (Authenticated)
package main

import (
	"context"
	"net/http"
	"net/http/httptest"

	"github.com/shurcooL/issuesapp/httpclient"
	"golang.org/x/oauth2"
)

func main() {
	// HTTP client with authentication.
	src := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: "... your access token ..."},
	)
	httpClient := oauth2.NewClient(context.Background(), src)

	issuesClient := httpclient.NewIssues(httpClient, "http", "localhost:8080")

	// Now you can use any of issuesClient methods.

	_ = issuesClient
}

func init() {

	http.DefaultTransport.(*http.Transport).RegisterProtocol("", localRoundTripper{})
}

// localRoundTripper is an http.RoundTripper that executes HTTP transactions
// by using http.DefaultServeMux directly, instead of going over an HTTP connection.
type localRoundTripper struct{}

func (localRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
	w := httptest.NewRecorder()
	http.DefaultServeMux.ServeHTTP(w, req)
	return w.Result(), nil
}
Output:

Types

type Issues

type Issues struct {
	// contains filtered or unexported fields
}

Issues implements issues.Service remotely over HTTP. Use NewIssues for creation, zero value of Issues is unfit for use.

func (*Issues) Count

func (i *Issues) Count(ctx context.Context, repo issues.RepoSpec, opt issues.IssueListOptions) (uint64, error)
Example
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"

	"github.com/shurcooL/issues"
	"github.com/shurcooL/issuesapp/httpclient"
)

var issuesClient = httpclient.NewIssues(nil, "", "")

func main() {
	count, err := issuesClient.Count(context.Background(), issues.RepoSpec{URI: "example.org/repo"}, issues.IssueListOptions{
		State: issues.AllStates,
	})
	if err != nil {
		log.Fatalln(err)
	}

	printJSON(count)

}

// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
// It's meant to be used by examples to print the output.
func printJSON(v interface{}) {
	w := json.NewEncoder(os.Stdout)
	w.SetIndent("", "\t")
	err := w.Encode(v)
	if err != nil {
		panic(err)
	}
}
Output:

1

func (*Issues) Create

func (*Issues) Create(_ context.Context, repo issues.RepoSpec, issue issues.Issue) (issues.Issue, error)

func (*Issues) CreateComment

func (*Issues) CreateComment(_ context.Context, repo issues.RepoSpec, id uint64, comment issues.Comment) (issues.Comment, error)

func (*Issues) Edit

func (*Issues) EditComment

func (i *Issues) EditComment(ctx context.Context, repo issues.RepoSpec, id uint64, cr issues.CommentRequest) (issues.Comment, error)

func (*Issues) Get

func (*Issues) Get(_ context.Context, repo issues.RepoSpec, id uint64) (issues.Issue, error)

func (*Issues) List

Example
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"

	"github.com/shurcooL/issues"
	"github.com/shurcooL/issuesapp/httpclient"
)

var issuesClient = httpclient.NewIssues(nil, "", "")

func main() {
	is, err := issuesClient.List(context.Background(), issues.RepoSpec{URI: "example.org/repo"}, issues.IssueListOptions{
		State: issues.StateFilter(issues.OpenState),
	})
	if err != nil {
		log.Fatalln(err)
	}

	printJSON(is)

}

// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
// It's meant to be used by examples to print the output.
func printJSON(v interface{}) {
	w := json.NewEncoder(os.Stdout)
	w.SetIndent("", "\t")
	err := w.Encode(v)
	if err != nil {
		panic(err)
	}
}
Output:

[
	{
		"ID": 1,
		"State": "open",
		"Title": "Sample title",
		"Labels": null,
		"User": {
			"ID": 1,
			"Domain": "example.org",
			"CanonicalMe": "",
			"Elsewhere": null,
			"Login": "gopher",
			"Name": "Sample Gopher",
			"Email": "gopher@example.org",
			"AvatarURL": "",
			"HTMLURL": "",
			"SiteAdmin": false
		},
		"CreatedAt": "2016-09-24T22:00:50.642521756Z",
		"Edited": null,
		"Body": "",
		"Reactions": null,
		"Editable": false,
		"Replies": 2
	}
]

func (*Issues) ListComments

func (i *Issues) ListComments(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error)
Example
package main

import (
	"context"
	"encoding/json"
	"log"
	"os"

	"github.com/shurcooL/issues"
	"github.com/shurcooL/issuesapp/httpclient"
)

var issuesClient = httpclient.NewIssues(nil, "", "")

func main() {
	is, err := issuesClient.ListComments(context.Background(), issues.RepoSpec{URI: "example.org/repo"}, 1, nil)
	if err != nil {
		log.Fatalln(err)
	}

	printJSON(is)

}

// printJSON prints v as JSON encoded with indent to stdout. It panics on any error.
// It's meant to be used by examples to print the output.
func printJSON(v interface{}) {
	w := json.NewEncoder(os.Stdout)
	w.SetIndent("", "\t")
	err := w.Encode(v)
	if err != nil {
		panic(err)
	}
}
Output:

[
	{
		"ID": 0,
		"User": {
			"ID": 1,
			"Domain": "example.org",
			"CanonicalMe": "",
			"Elsewhere": null,
			"Login": "gopher",
			"Name": "Sample Gopher",
			"Email": "gopher@example.org",
			"AvatarURL": "",
			"HTMLURL": "",
			"SiteAdmin": false
		},
		"CreatedAt": "2016-09-24T22:00:50.642521756Z",
		"Edited": null,
		"Body": "Sample body.",
		"Reactions": [
			{
				"Reaction": "grinning",
				"Users": [
					{
						"ID": 1,
						"Domain": "example.org",
						"CanonicalMe": "",
						"Elsewhere": null,
						"Login": "gopher",
						"Name": "Sample Gopher",
						"Email": "gopher@example.org",
						"AvatarURL": "",
						"HTMLURL": "",
						"SiteAdmin": false
					}
				]
			},
			{
				"Reaction": "+1",
				"Users": [
					{
						"ID": 2,
						"Domain": "example.org",
						"CanonicalMe": "",
						"Elsewhere": null,
						"Login": "2@example.org",
						"Name": "",
						"Email": "",
						"AvatarURL": "https://secure.gravatar.com/avatar?d=mm\u0026f=y\u0026s=96",
						"HTMLURL": "",
						"SiteAdmin": false
					},
					{
						"ID": 1,
						"Domain": "example.org",
						"CanonicalMe": "",
						"Elsewhere": null,
						"Login": "gopher",
						"Name": "Sample Gopher",
						"Email": "gopher@example.org",
						"AvatarURL": "",
						"HTMLURL": "",
						"SiteAdmin": false
					},
					{
						"ID": 3,
						"Domain": "example.org",
						"CanonicalMe": "",
						"Elsewhere": null,
						"Login": "3@example.org",
						"Name": "",
						"Email": "",
						"AvatarURL": "https://secure.gravatar.com/avatar?d=mm\u0026f=y\u0026s=96",
						"HTMLURL": "",
						"SiteAdmin": false
					}
				]
			},
			{
				"Reaction": "mushroom",
				"Users": [
					{
						"ID": 3,
						"Domain": "example.org",
						"CanonicalMe": "",
						"Elsewhere": null,
						"Login": "3@example.org",
						"Name": "",
						"Email": "",
						"AvatarURL": "https://secure.gravatar.com/avatar?d=mm\u0026f=y\u0026s=96",
						"HTMLURL": "",
						"SiteAdmin": false
					}
				]
			}
		],
		"Editable": true
	},
	{
		"ID": 1,
		"User": {
			"ID": 2,
			"Domain": "example.org",
			"CanonicalMe": "",
			"Elsewhere": null,
			"Login": "2@example.org",
			"Name": "",
			"Email": "",
			"AvatarURL": "https://secure.gravatar.com/avatar?d=mm\u0026f=y\u0026s=96",
			"HTMLURL": "",
			"SiteAdmin": false
		},
		"CreatedAt": "2016-10-02T12:31:50.813167602Z",
		"Edited": null,
		"Body": "Sample reply.",
		"Reactions": null,
		"Editable": false
	},
	{
		"ID": 2,
		"User": {
			"ID": 1,
			"Domain": "example.org",
			"CanonicalMe": "",
			"Elsewhere": null,
			"Login": "gopher",
			"Name": "Sample Gopher",
			"Email": "gopher@example.org",
			"AvatarURL": "",
			"HTMLURL": "",
			"SiteAdmin": false
		},
		"CreatedAt": "2016-10-02T18:51:14.250725508Z",
		"Edited": {
			"By": {
				"ID": 1,
				"Domain": "example.org",
				"CanonicalMe": "",
				"Elsewhere": null,
				"Login": "gopher",
				"Name": "Sample Gopher",
				"Email": "gopher@example.org",
				"AvatarURL": "",
				"HTMLURL": "",
				"SiteAdmin": false
			},
			"At": "2016-10-02T18:57:47.938813179Z"
		},
		"Body": "Sample another reply.",
		"Reactions": null,
		"Editable": true
	}
]

func (*Issues) ListEvents

func (i *Issues) ListEvents(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL