repos_traffic.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2016 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. )
  10. // TrafficReferrer represent information about traffic from a referrer .
  11. type TrafficReferrer struct {
  12. Referrer *string `json:"referrer,omitempty"`
  13. Count *int `json:"count,omitempty"`
  14. Uniques *int `json:"uniques,omitempty"`
  15. }
  16. // TrafficPath represent information about the traffic on a path of the repo.
  17. type TrafficPath struct {
  18. Path *string `json:"path,omitempty"`
  19. Title *string `json:"title,omitempty"`
  20. Count *int `json:"count,omitempty"`
  21. Uniques *int `json:"uniques,omitempty"`
  22. }
  23. // TrafficData represent information about a specific timestamp in views or clones list.
  24. type TrafficData struct {
  25. Timestamp *Timestamp `json:"timestamp,omitempty"`
  26. Count *int `json:"count,omitempty"`
  27. Uniques *int `json:"uniques,omitempty"`
  28. }
  29. // TrafficViews represent information about the number of views in the last 14 days.
  30. type TrafficViews struct {
  31. Views []*TrafficData `json:"views,omitempty"`
  32. Count *int `json:"count,omitempty"`
  33. Uniques *int `json:"uniques,omitempty"`
  34. }
  35. // TrafficClones represent information about the number of clones in the last 14 days.
  36. type TrafficClones struct {
  37. Clones []*TrafficData `json:"clones,omitempty"`
  38. Count *int `json:"count,omitempty"`
  39. Uniques *int `json:"uniques,omitempty"`
  40. }
  41. // TrafficBreakdownOptions specifies the parameters to methods that support breakdown per day or week.
  42. // Can be one of: day, week. Default: day.
  43. type TrafficBreakdownOptions struct {
  44. Per string `url:"per,omitempty"`
  45. }
  46. // ListTrafficReferrers list the top 10 referrers over the last 14 days.
  47. //
  48. // GitHub API docs: https://developer.github.com/v3/repos/traffic/#list-referrers
  49. func (s *RepositoriesService) ListTrafficReferrers(ctx context.Context, owner, repo string) ([]*TrafficReferrer, *Response, error) {
  50. u := fmt.Sprintf("repos/%v/%v/traffic/popular/referrers", owner, repo)
  51. req, err := s.client.NewRequest("GET", u, nil)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. var trafficReferrers []*TrafficReferrer
  56. resp, err := s.client.Do(ctx, req, &trafficReferrers)
  57. if err != nil {
  58. return nil, resp, err
  59. }
  60. return trafficReferrers, resp, nil
  61. }
  62. // ListTrafficPaths list the top 10 popular content over the last 14 days.
  63. //
  64. // GitHub API docs: https://developer.github.com/v3/repos/traffic/#list-paths
  65. func (s *RepositoriesService) ListTrafficPaths(ctx context.Context, owner, repo string) ([]*TrafficPath, *Response, error) {
  66. u := fmt.Sprintf("repos/%v/%v/traffic/popular/paths", owner, repo)
  67. req, err := s.client.NewRequest("GET", u, nil)
  68. if err != nil {
  69. return nil, nil, err
  70. }
  71. var paths []*TrafficPath
  72. resp, err := s.client.Do(ctx, req, &paths)
  73. if err != nil {
  74. return nil, resp, err
  75. }
  76. return paths, resp, nil
  77. }
  78. // ListTrafficViews get total number of views for the last 14 days and breaks it down either per day or week.
  79. //
  80. // GitHub API docs: https://developer.github.com/v3/repos/traffic/#views
  81. func (s *RepositoriesService) ListTrafficViews(ctx context.Context, owner, repo string, opt *TrafficBreakdownOptions) (*TrafficViews, *Response, error) {
  82. u := fmt.Sprintf("repos/%v/%v/traffic/views", owner, repo)
  83. u, err := addOptions(u, opt)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. req, err := s.client.NewRequest("GET", u, nil)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. trafficViews := new(TrafficViews)
  92. resp, err := s.client.Do(ctx, req, &trafficViews)
  93. if err != nil {
  94. return nil, resp, err
  95. }
  96. return trafficViews, resp, nil
  97. }
  98. // ListTrafficClones get total number of clones for the last 14 days and breaks it down either per day or week for the last 14 days.
  99. //
  100. // GitHub API docs: https://developer.github.com/v3/repos/traffic/#views
  101. func (s *RepositoriesService) ListTrafficClones(ctx context.Context, owner, repo string, opt *TrafficBreakdownOptions) (*TrafficClones, *Response, error) {
  102. u := fmt.Sprintf("repos/%v/%v/traffic/clones", owner, repo)
  103. u, err := addOptions(u, opt)
  104. if err != nil {
  105. return nil, nil, err
  106. }
  107. req, err := s.client.NewRequest("GET", u, nil)
  108. if err != nil {
  109. return nil, nil, err
  110. }
  111. trafficClones := new(TrafficClones)
  112. resp, err := s.client.Do(ctx, req, &trafficClones)
  113. if err != nil {
  114. return nil, resp, err
  115. }
  116. return trafficClones, resp, nil
  117. }