1 Star 0 Fork 0

yangx282441848 / scm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
gogs.go 2.48 KB
一键复制 编辑 原始数据 按行查看 历史
Your Name 提交于 2021-10-11 10:02 . gitee
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package gogs implements a Gogs client.
package gogs
import (
"bytes"
"context"
"encoding/json"
"errors"
"io"
"net/http"
"net/url"
"strings"
"gitee.com/yangx282441848/scm/scm"
)
// New returns a new Gogs API client.
func New(uri string) (*scm.Client, error) {
base, err := url.Parse(uri)
if err != nil {
return nil, err
}
if !strings.HasSuffix(base.Path, "/") {
base.Path = base.Path + "/"
}
client := &wrapper{new(scm.Client)}
client.BaseURL = base
// initialize services
client.Driver = scm.DriverGogs
client.Linker = &linker{base.String()}
client.Contents = &contentService{client}
client.Git = &gitService{client}
client.Issues = &issueService{client}
client.Organizations = &organizationService{client}
client.Milestones = &milestoneService{client}
client.PullRequests = &pullService{client}
client.Repositories = &repositoryService{client}
client.Releases = &releaseService{client}
client.Reviews = &reviewService{client}
client.Users = &userService{client}
client.Webhooks = &webhookService{client}
return client.Client, nil
}
// wraper wraps the Client to provide high level helper functions
// for making http requests and unmarshaling the response.
type wrapper struct {
*scm.Client
}
// do wraps the Client.Do function by creating the Request and
// unmarshalling the response.
func (c *wrapper) do(ctx context.Context, method, path string, in, out interface{}) (*scm.Response, error) {
req := &scm.Request{
Method: method,
Path: path,
}
// if we are posting or putting data, we need to
// write it to the body of the request.
if in != nil {
buf := new(bytes.Buffer)
json.NewEncoder(buf).Encode(in)
req.Header = map[string][]string{
"Content-Type": {"application/json"},
}
req.Body = buf
}
// execute the http request
res, err := c.Client.Do(ctx, req)
if err != nil {
return nil, err
}
defer res.Body.Close()
// if an error is encountered, unmarshal and return the
// error response.
if res.Status > 300 {
return res, errors.New(
http.StatusText(res.Status),
)
}
if out == nil {
return res, nil
}
// if raw output is expected, copy to the provided
// buffer and exit.
if w, ok := out.(io.Writer); ok {
io.Copy(w, res.Body)
return res, nil
}
// if a json response is expected, parse and return
// the json response.
return res, json.NewDecoder(res.Body).Decode(out)
}
1
https://gitee.com/yangx282441848/scm.git
git@gitee.com:yangx282441848/scm.git
yangx282441848
scm
scm
v1.16.27

搜索帮助