From 30e31f9a08cfcf01985a340c713e82a0137e5771 Mon Sep 17 00:00:00 2001 From: mappu Date: Thu, 19 Nov 2020 11:03:59 +1300 Subject: [PATCH] api: extract common http handling code into apiRequest() --- api.go | 81 +++++++++++++++++++++------------------------------------- 1 file changed, 29 insertions(+), 52 deletions(-) diff --git a/api.go b/api.go index c6bdaac..ba7760d 100644 --- a/api.go +++ b/api.go @@ -45,31 +45,41 @@ type MarkdownRequest struct { Wiki bool } -// reposPage gets a single page of the list of Git repositories in this organisation. -func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo, error) { +func (this *Application) apiRequest(ctx context.Context, endpoint string, target interface{}) error { err := this.apiSem.Acquire(ctx, 1) if err != nil { - return nil, err // e.g. ctx closed + return err // e.g. ctx closed } defer this.apiSem.Release(1) - req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+fmt.Sprintf(`/repos?page=%d&limit=%d`, page, limit), nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+endpoint, nil) if err != nil { - return nil, err + return err } resp, err := http.DefaultClient.Do(req) if err != nil { - return nil, err + return err } defer resp.Body.Close() if resp.StatusCode != 200 { - return nil, fmt.Errorf("HTTP %d", resp.StatusCode) + return fmt.Errorf("HTTP %d", resp.StatusCode) } + err = json.NewDecoder(resp.Body).Decode(target) + if err != nil { + return err + } + + return nil +} + +// reposPage gets a single page of the list of Git repositories in this organisation. +func (this *Application) reposPage(ctx context.Context, page, limit int) ([]Repo, error) { var repos []Repo - err = json.NewDecoder(resp.Body).Decode(&repos) + + err := this.apiRequest(ctx, `api/v1/orgs/`+url.PathEscape(this.cfg.Gitea.Org)+fmt.Sprintf(`/repos?page=%d&limit=%d`, page, limit), &repos) if err != nil { return nil, err } @@ -101,32 +111,19 @@ func (this *Application) repos(ctx context.Context) ([]Repo, error) { } } + + + } + + } + + // repoFile gets a single file from the default branch of the git repository // Usually the default branch is `master`. func (this *Application) repoFile(ctx context.Context, repo, filename string) ([]byte, error) { - err := this.apiSem.Acquire(ctx, 1) - if err != nil { - return nil, err // e.g. ctx closed - } - defer this.apiSem.Release(1) - - req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), nil) - if err != nil { - return nil, err - } - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode != 200 { - return nil, fmt.Errorf("HTTP %d", resp.StatusCode) - } - var cr ContentsResponse - err = json.NewDecoder(resp.Body).Decode(&cr) + + err := this.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/contents/`+url.PathEscape(filename), &cr) if err != nil { return nil, err } @@ -201,29 +198,9 @@ func (this *Application) imageFilesForRepo(ctx context.Context, repo string) ([] } func (this *Application) topicsForRepo(ctx context.Context, repo string) ([]string, error) { - err := this.apiSem.Acquire(ctx, 1) - if err != nil { - return nil, err // e.g. ctx closed - } - defer this.apiSem.Release(1) - - req, err := http.NewRequestWithContext(ctx, http.MethodGet, this.cfg.Gitea.URL+`api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/topics`, nil) - if err != nil { - return nil, err - } - - resp, err := http.DefaultClient.Do(req) - if err != nil { - return nil, err - } - defer resp.Body.Close() - - if resp.StatusCode != 200 { - return nil, fmt.Errorf("HTTP %d", resp.StatusCode) - } - var tr TopicsResponse - err = json.NewDecoder(resp.Body).Decode(&tr) + + err := this.apiRequest(ctx, `api/v1/repos/`+url.PathEscape(this.cfg.Gitea.Org)+`/`+url.PathEscape(repo)+`/topics`, &tr) if err != nil { return nil, err }