本文整理汇总了Golang中github.com/minodisk/qiitactl/api.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestFetchPostsWitWrongResponseBody
func TestFetchPostsWitWrongResponseBody(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Non JSON format")
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
_, err = model.FetchPosts(client, nil)
if err == nil {
t.Fatal("error should occur")
}
}
示例2: TestClientPost
func TestClientPost(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
client.DebugMode(true)
body, _, err := client.Post("", "/echo", "data")
if err != nil {
t.Fatal(err)
}
var b string
err = json.Unmarshal(body, &b)
if err != nil {
t.Fatal(err)
}
if b != "data" {
t.Errorf("wrong body: %s", b)
}
}
示例3: TestFetchPostsWithResponseStatusError
func TestFetchPostsWithResponseStatusError(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/authenticated_user/items", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
_, err = model.FetchPosts(client, nil)
if err == nil {
t.Fatal("error should occur")
}
_, ok := err.(api.StatusError)
if !ok {
t.Fatalf("wrong type error: %s", reflect.TypeOf(err))
}
}
示例4: TestGenerateUniqueFile
func TestGenerateUniqueFile(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(nil, inf)
testutil.ShouldExistFile(t, 0)
app := command.New(inf, client, os.Stdout, os.Stderr)
app.Run([]string{"qiitactl", "generate", "file", "Example Title"})
app.Run([]string{"qiitactl", "generate", "file", "Example Title"})
testutil.ShouldExistFile(t, 2)
path := fmt.Sprintf("mine/%s/Example Title.md", time.Now().Format("2006/01/02"))
_, err = os.Stat(path)
if err != nil {
t.Errorf("file should exist at %s", path)
}
path = fmt.Sprintf("mine/%s/Example Title-.md", time.Now().Format("2006/01/02"))
_, err = os.Stat(path)
if err != nil {
t.Errorf("file should exist at %s", path)
}
}
示例5: TestPostCreateWithNoServer
func TestPostCreateWithNoServer(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", nil, nil)
err = post.Create(client, model.CreationOptions{})
if err == nil {
t.Fatal("error should occur")
}
testutil.ShouldExistFile(t, 0)
}
示例6: TestPostCreateWithWrongResponseBody
func TestPostCreateWithWrongResponseBody(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/items", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Non JSON format")
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
post := model.NewPost("Example Title", nil, nil)
err = post.Create(client, model.CreationOptions{})
if err == nil {
t.Fatal("error should occur")
}
}
示例7: TestPostDeleteWithWrongResponseBody
func TestPostDeleteWithWrongResponseBody(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/items/abcdefghijklmnopqrst", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Non JSON format")
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
post := model.NewPost("Example Title", nil, nil)
post.ID = "abcdefghijklmnopqrst"
err = post.Delete(client)
// Don't parse response body from DELETE method
if err != nil {
t.Fatal(err)
}
}
示例8: TestPostDeleteWithNoServer
func TestPostDeleteWithNoServer(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", &model.Time{time.Date(2000, 1, 1, 9, 0, 0, 0, time.UTC)}, nil)
post.ID = "abcdefghijklmnopqrst"
err = post.Delete(client)
if err == nil {
t.Fatal("error should occur")
}
testutil.ShouldExistFile(t, 0)
}
示例9: TestFetchPostWithID
func TestFetchPostWithID(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
handleItem(mux)
serverMine := httptest.NewServer(mux)
defer serverMine.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
switch subDomain {
case "":
url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
default:
log.Fatalf("wrong sub domain \"%s\"", subDomain)
}
return
}, inf)
testutil.ShouldExistFile(t, 0)
errBuf := bytes.NewBuffer([]byte{})
app := command.New(inf, client, os.Stdout, errBuf)
app.Run([]string{"qiitactl", "fetch", "post", "-i", "4bd431809afb1bb99e4f"})
e := errBuf.Bytes()
if len(e) != 0 {
t.Fatal(string(e))
}
testutil.ShouldExistFile(t, 1)
b, err := ioutil.ReadFile("mine/2000/01/01/Example Title.md")
if err != nil {
t.Fatal(err)
}
actual := string(b)
expected := `<!--
id: 4bd431809afb1bb99e4f
url: https://qiita.com/yaotti/items/4bd431809afb1bb99e4f
created_at: 2000-01-01T09:00:00+09:00
updated_at: 2000-01-01T09:00:00+09:00
private: false
coediting: false
tags:
- Ruby:
- 0.0.1
team: null
-->
# Example Title
## Example body`
if actual != expected {
t.Errorf("wrong body:\n%s", testutil.Diff(expected, actual))
}
}
示例10: main
func main() {
godotenv.Load()
g := MustAsset(".goxc.json")
info, err := info.New(g)
if err != nil {
panic("fail to load bindata")
}
client := api.NewClient(nil, info)
cmd := command.New(info, client, os.Stdout, os.Stderr)
cmd.Run(os.Args)
}
示例11: TestShowPosts
func TestShowPosts(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
mux := http.NewServeMux()
handleAuthenticatedUserItems(mux)
handleTeams(mux)
serverMine := httptest.NewServer(mux)
defer serverMine.Close()
mux = http.NewServeMux()
handleAuthenticatedUserItemsWithTeam(mux)
serverTeam := httptest.NewServer(mux)
defer serverTeam.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
switch subDomain {
case "":
url = fmt.Sprintf("%s%s%s", serverMine.URL, "/api/v2", path)
case "increments":
url = fmt.Sprintf("%s%s%s", serverTeam.URL, "/api/v2", path)
default:
log.Fatalf("wrong sub domain \"%s\"", subDomain)
}
return
}, inf)
testutil.ShouldExistFile(t, 0)
buf := bytes.NewBuffer([]byte{})
errBuf := bytes.NewBuffer([]byte{})
app := command.New(inf, client, buf, errBuf)
app.Run([]string{"qiitactl", "show", "posts"})
e := errBuf.Bytes()
if len(e) != 0 {
t.Fatal(string(e))
}
testutil.ShouldExistFile(t, 0)
if string(buf.Bytes()) != `Posts in Qiita:
4bd431809afb1bb99e4f 2000/01/01 Example Title
Posts in Qiita:Team (Increments Inc.):
4bd431809afb1bb99e4t 2015/09/25 Example Title in team
` {
t.Errorf("written text is wrong: %s", buf.Bytes())
}
}
示例12: TestClientProcessWithEmptyToken
func TestClientProcessWithEmptyToken(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
_, _, err := client.Options("", "/echo", nil)
_, ok := err.(api.EmptyTokenError)
if !ok {
t.Fatal("empty token error should occur")
}
}
示例13: TestClientProcessWithStatusError
func TestClientProcessWithStatusError(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
_, _, err = client.Options("", "/errors/status", nil)
_, ok := err.(api.StatusError)
if !ok {
t.Fatal("status error should occur")
}
}
示例14: TestPostUpdateWithEmptyID
func TestPostUpdateWithEmptyID(t *testing.T) {
testutil.CleanUp()
defer testutil.CleanUp()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
log.Fatal(err)
}
client := api.NewClient(nil, inf)
testutil.ShouldExistFile(t, 0)
post := model.NewPost("Example Title", nil, nil)
err = post.Update(client)
err, ok := err.(model.EmptyIDError)
if !ok {
t.Fatal("empty ID error should occur")
}
testutil.ShouldExistFile(t, 0)
}
示例15: TestTeams_FetchTeams
func TestTeams_FetchTeams(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/api/v2/teams", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
log.Fatalf("wrong method: %s", r.Method)
}
w.Write([]byte(`[
{
"active": true,
"id": "increments",
"name": "Increments Inc."
}
]`))
})
server := httptest.NewServer(mux)
defer server.Close()
err := os.Setenv("QIITA_ACCESS_TOKEN", "XXXXXXXXXXXX")
if err != nil {
t.Fatal(err)
}
client := api.NewClient(func(subDomain, path string) (url string) {
url = fmt.Sprintf("%s%s%s", server.URL, "/api/v2", path)
return
}, inf)
teams, err := model.FetchTeams(client)
if err != nil {
t.Fatal(err)
}
if len(teams) != 1 {
t.Fatalf("wrong length of teams: expected %d, but actual %d", 1, len(teams))
}
if teams[0].Active != true || teams[0].ID != "increments" || teams[0].Name != "Increments Inc." {
t.Errorf("wrong team: %+v", teams[0])
}
}