Skip to content

Commit 8ce7282

Browse files
Added library to get read me file from github
1 parent 1390e39 commit 8ce7282

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
1414
.glide/
15+
.idea/

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
# github-readme
1+
# Github Readme
22
This library is used to get GitHub readme content in HTML as well as in JSON format
3+
4+
## How to use it
5+
```go
6+
7+
src := oauth2.StaticTokenSource(
8+
&oauth2.Token{AccessToken: "<!-- Your github access token -->"},
9+
)
10+
ctx := context.Background()
11+
httpClient := oauth2.NewClient(ctx, src)
12+
githubClient := github.NewGithub(httpClient)
13+
14+
content, err := githubClient.GetReadme(ctx, "<!-- organization name -->", "<!-- Github repository name -->")
15+
16+
fmt.Println(content)
17+
fmt.Println(err)
18+
19+
```

example/main.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"golang.org/x/oauth2"
7+
8+
"github.com/Golang-Coach/github-readme"
9+
)
10+
11+
func main() {
12+
13+
src := oauth2.StaticTokenSource(
14+
&oauth2.Token{AccessToken: "<!-- Your github access token -->"},
15+
)
16+
ctx := context.Background()
17+
httpClient := oauth2.NewClient(ctx, src)
18+
githubClient := github.NewGithub(httpClient)
19+
20+
content, err := githubClient.GetReadme(ctx, "parnurzeal", "gorequest")
21+
22+
fmt.Println(content)
23+
fmt.Println(err)
24+
25+
}

github.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"errors"
7+
"io"
8+
"io/ioutil"
9+
"net/http"
10+
"net/url"
11+
)
12+
13+
type Github struct {
14+
client *http.Client
15+
}
16+
17+
// NewGithub initialized Github
18+
func NewGithub(client *http.Client) *Github {
19+
return &Github{
20+
client: client,
21+
}
22+
}
23+
24+
func withContext(ctx context.Context, req *http.Request) *http.Request {
25+
return req.WithContext(ctx)
26+
}
27+
28+
// sanitizeURL redacts the client_secret parameter from the URL which may be
29+
// exposed to the user.
30+
func sanitizeURL(uri *url.URL) *url.URL {
31+
if uri == nil {
32+
return nil
33+
}
34+
params := uri.Query()
35+
if len(params.Get("client_secret")) > 0 {
36+
params.Set("client_secret", "REDACTED")
37+
uri.RawQuery = params.Encode()
38+
}
39+
return uri
40+
}
41+
42+
// GetReadme returns readme content in HTML as well as in JSON format
43+
func (g *Github) GetReadme(ctx context.Context, owner string, repo string) (string, error) {
44+
u := fmt.Sprintf("repos/%v/%v/readme", owner, repo)
45+
46+
req, err := http.NewRequest("GET", "https://api.github.com/"+u, nil) // TODO -- need to refactored
47+
req = withContext(ctx, req)
48+
req.Header.Add("Accept", "application/vnd.github.v3.html+json")
49+
resp, err := g.client.Do(req)
50+
if err != nil {
51+
// If we got an error, and the context has been canceled,
52+
// the context'g error is probably more useful.
53+
select {
54+
case <-ctx.Done():
55+
return "", ctx.Err()
56+
default:
57+
}
58+
59+
// If the error type is *url.Error, sanitize its URL before returning.
60+
if e, ok := err.(*url.Error); ok {
61+
if url, err := url.Parse(e.URL); err == nil {
62+
e.URL = sanitizeURL(url).String()
63+
return "", e
64+
}
65+
}
66+
67+
return "", err
68+
}
69+
70+
defer func() {
71+
// Drain up to 512 bytes and close the body to let the Transport reuse the connection
72+
io.CopyN(ioutil.Discard, resp.Body, 512)
73+
resp.Body.Close()
74+
}()
75+
76+
if c := resp.StatusCode; 200 <= c && c <= 299 {
77+
data, err := ioutil.ReadAll(resp.Body)
78+
if err != nil {
79+
return "", err
80+
}
81+
return string(data[:]), nil
82+
}
83+
return "", errors.New(resp.Status)
84+
}

0 commit comments

Comments
 (0)