当前位置: 首页>>代码示例>>Golang>>正文


Golang api.NewClient函数代码示例

本文整理汇总了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")
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:27,代码来源:posts_test.go

示例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)
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:28,代码来源:client_test.go

示例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))
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:31,代码来源:posts_test.go

示例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)
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:29,代码来源:file_test.go

示例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)
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:28,代码来源:post_test.go

示例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")
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:28,代码来源:post_test.go

示例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)
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:30,代码来源:post_test.go

示例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)
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:29,代码来源:post_test.go

示例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))
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:59,代码来源:post_test.go

示例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)
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:11,代码来源:main.go

示例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())
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:50,代码来源:post_test.go

示例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")
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:15,代码来源:client_test.go

示例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")
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:19,代码来源:client_test.go

示例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)
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:21,代码来源:post_test.go

示例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])
	}
}
开发者ID:minodisk,项目名称:qiitactl,代码行数:40,代码来源:teams_test.go


注:本文中的github.com/minodisk/qiitactl/api.NewClient函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。