Giter Club home page Giter Club logo

go-gitee's Introduction

go-gitee

go-gitee is a Go client library for accessing the [Gitee API v5][].

go-gitee requires Go version 1.7 or greater.

Usage

The go-gitee library does not directly handle authentication. Instead, when creating a new client, pass an http.Client that can handle authentication for you. The easiest and recommended way to do this is using the oauth2 library, but you can always use any other library that provides an http.Client. If you have an OAuth2 access token (for example, a personal API token), you can use it with the oauth2 library using:

import "golang.org/x/oauth2"
import "github.com/weilaihui/go-gitee/gitee"


ctx := context.Background()
conf := &oauth2.Config{
    ClientID:     "{ClientID}",
    ClientSecret: "{ClientSecret}",
    Scopes:       "{Scopes}",
    Endpoint: oauth2.Endpoint{
        AuthURL:  "https://gitee.com/oauth/auth",
        TokenURL: "https://gitee.com/oauth/token",
    },
}
token,err := conf.PasswordCredentialsToken(ctx,"{username}","{password}")

tp := gitee.OAuthTransport{
	Token: token,
}

client := gitee.NewClient(tp.Client())

user, _, err := client.Users.Get(ctx, "")
if err != nil {
	fmt.Printf("\nerror: %v\n", err)
	return
}

fmt.Printf("\n%v\n", gitee.Stringify(user))
fmt.Printf("\n%v\n", *user.Login)

The services of a client divide the API into logical chunks and correspond to the structure of the Gitee API documentation at https://gitee.com/api/v5/swagger.

Accepted Status

Some endpoints may return a 202 Accepted status code, meaning that the information required is not yet ready and was scheduled to be gathered on the GitHub side. Methods known to behave like this are documented specifying this behavior.

To detect this condition of error, you can check if its type is *gitee.AcceptedError:

stats, _, err := client.Repositories.ListContributorsStats(ctx, org, repo)
if _, ok := err.(*gitee.AcceptedError); ok {
	log.Println("scheduled on Gitee side")
}

Creating and Updating Resources

All structs for Gitee resources use pointer values for all non-repeated fields. This allows distinguishing between unset fields and those set to a zero-value. Helper functions have been provided to easily create these pointers for string, bool, and int values. For example:

// create a new private repository named "foo"
repo := &gitee.Repository{
	Name:    gitee.String("foo"),
	Private: gitee.Bool(true),
}
client.Repositories.Create(ctx, "", repo)

Users who have worked with protocol buffers should find this pattern familiar.

Pagination

All requests for resource collections (repos, pull requests, issues, etc.) support pagination. Pagination options are described in the gitee.ListOptions struct and passed to the list methods directly or as an embedded type of a more specific list options struct (for example gitee.PullRequestListOptions). Pages information is available via the gitee.Response struct.

client := gitee.NewClient(nil)

opt := &gitee.RepositoryListByOrgOptions{
	ListOptions: gitee.ListOptions{PerPage: 10},
}
// get all pages of results
var allRepos []*gitee.Repository
for {
	repos, resp, err := client.Repositories.ListByOrg(ctx, "gitee", opt)
	if err != nil {
		return err
	}
	allRepos = append(allRepos, repos...)
	if resp.NextPage == 0 {
		break
	}
	opt.Page = resp.NextPage
}

For complete usage of go-gitee, see the full package docs.

Integration Tests

You can run integration tests from the test directory. See the integration tests README.

Roadmap

This library is being initially developed by go-github, so API methods will likely be implemented like go-github. You can track the status of implementation in go-github. Eventually, I would like to cover the entire Gitee API, so contributions are of course always welcome. The calling pattern is pretty well established, so adding new methods is relatively straightforward.

License

This library is distributed under the BSD-style license found in the LICENSE file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.