apps_marketplace.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Copyright 2017 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. // MarketplaceService handles communication with the marketplace related
  11. // methods of the GitHub API.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/apps/marketplace/
  14. type MarketplaceService struct {
  15. client *Client
  16. // Stubbed controls whether endpoints that return stubbed data are used
  17. // instead of production endpoints. Stubbed data is fake data that's useful
  18. // for testing your GitHub Apps. Stubbed data is hard-coded and will not
  19. // change based on actual subscriptions.
  20. //
  21. // GitHub API docs: https://developer.github.com/v3/apps/marketplace/
  22. Stubbed bool
  23. }
  24. // MarketplacePlan represents a GitHub Apps Marketplace Listing Plan.
  25. type MarketplacePlan struct {
  26. URL *string `json:"url,omitempty"`
  27. AccountsURL *string `json:"accounts_url,omitempty"`
  28. ID *int64 `json:"id,omitempty"`
  29. Name *string `json:"name,omitempty"`
  30. Description *string `json:"description,omitempty"`
  31. MonthlyPriceInCents *int `json:"monthly_price_in_cents,omitempty"`
  32. YearlyPriceInCents *int `json:"yearly_price_in_cents,omitempty"`
  33. PriceModel *string `json:"price_model,omitempty"`
  34. UnitName *string `json:"unit_name,omitempty"`
  35. Bullets *[]string `json:"bullets,omitempty"`
  36. }
  37. // MarketplacePurchase represents a GitHub Apps Marketplace Purchase.
  38. type MarketplacePurchase struct {
  39. BillingCycle *string `json:"billing_cycle,omitempty"`
  40. NextBillingDate *string `json:"next_billing_date,omitempty"`
  41. UnitCount *int `json:"unit_count,omitempty"`
  42. Plan *MarketplacePlan `json:"plan,omitempty"`
  43. Account *MarketplacePlanAccount `json:"account,omitempty"`
  44. }
  45. // MarketplacePlanAccount represents a GitHub Account (user or organization) on a specific plan.
  46. type MarketplacePlanAccount struct {
  47. URL *string `json:"url,omitempty"`
  48. Type *string `json:"type,omitempty"`
  49. ID *int64 `json:"id,omitempty"`
  50. Login *string `json:"login,omitempty"`
  51. Email *string `json:"email,omitempty"`
  52. OrganizationBillingEmail *string `json:"organization_billing_email,omitempty"`
  53. MarketplacePurchase *MarketplacePurchase `json:"marketplace_purchase,omitempty"`
  54. }
  55. // ListPlans lists all plans for your Marketplace listing.
  56. //
  57. // GitHub API docs: https://developer.github.com/v3/apps/marketplace/#list-all-plans-for-your-marketplace-listing
  58. func (s *MarketplaceService) ListPlans(ctx context.Context, opt *ListOptions) ([]*MarketplacePlan, *Response, error) {
  59. uri := s.marketplaceURI("plans")
  60. u, err := addOptions(uri, opt)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. req, err := s.client.NewRequest("GET", u, nil)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. // TODO: remove custom Accept header when this API fully launches.
  69. req.Header.Set("Accept", mediaTypeMarketplacePreview)
  70. var plans []*MarketplacePlan
  71. resp, err := s.client.Do(ctx, req, &plans)
  72. if err != nil {
  73. return nil, resp, err
  74. }
  75. return plans, resp, nil
  76. }
  77. // ListPlanAccountsForPlan lists all GitHub accounts (user or organization) on a specific plan.
  78. //
  79. // GitHub API docs: https://developer.github.com/v3/apps/marketplace/#list-all-github-accounts-user-or-organization-on-a-specific-plan
  80. func (s *MarketplaceService) ListPlanAccountsForPlan(ctx context.Context, planID int64, opt *ListOptions) ([]*MarketplacePlanAccount, *Response, error) {
  81. uri := s.marketplaceURI(fmt.Sprintf("plans/%v/accounts", planID))
  82. u, err := addOptions(uri, opt)
  83. if err != nil {
  84. return nil, nil, err
  85. }
  86. req, err := s.client.NewRequest("GET", u, nil)
  87. if err != nil {
  88. return nil, nil, err
  89. }
  90. // TODO: remove custom Accept header when this API fully launches.
  91. req.Header.Set("Accept", mediaTypeMarketplacePreview)
  92. var accounts []*MarketplacePlanAccount
  93. resp, err := s.client.Do(ctx, req, &accounts)
  94. if err != nil {
  95. return nil, resp, err
  96. }
  97. return accounts, resp, nil
  98. }
  99. // ListPlanAccountsForAccount lists all GitHub accounts (user or organization) associated with an account.
  100. //
  101. // GitHub API docs: https://developer.github.com/v3/apps/marketplace/#check-if-a-github-account-is-associated-with-any-marketplace-listing
  102. func (s *MarketplaceService) ListPlanAccountsForAccount(ctx context.Context, accountID int64, opt *ListOptions) ([]*MarketplacePlanAccount, *Response, error) {
  103. uri := s.marketplaceURI(fmt.Sprintf("accounts/%v", accountID))
  104. u, err := addOptions(uri, opt)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. req, err := s.client.NewRequest("GET", u, nil)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. // TODO: remove custom Accept header when this API fully launches.
  113. req.Header.Set("Accept", mediaTypeMarketplacePreview)
  114. var accounts []*MarketplacePlanAccount
  115. resp, err := s.client.Do(ctx, req, &accounts)
  116. if err != nil {
  117. return nil, resp, err
  118. }
  119. return accounts, resp, nil
  120. }
  121. // ListMarketplacePurchasesForUser lists all GitHub marketplace purchases made by a user.
  122. //
  123. // GitHub API docs: https://developer.github.com/v3/apps/marketplace/#get-a-users-marketplace-purchases
  124. func (s *MarketplaceService) ListMarketplacePurchasesForUser(ctx context.Context, opt *ListOptions) ([]*MarketplacePurchase, *Response, error) {
  125. uri := "user/marketplace_purchases"
  126. if s.Stubbed {
  127. uri = "user/marketplace_purchases/stubbed"
  128. }
  129. u, err := addOptions(uri, opt)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. req, err := s.client.NewRequest("GET", u, nil)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. // TODO: remove custom Accept header when this API fully launches.
  138. req.Header.Set("Accept", mediaTypeMarketplacePreview)
  139. var purchases []*MarketplacePurchase
  140. resp, err := s.client.Do(ctx, req, &purchases)
  141. if err != nil {
  142. return nil, resp, err
  143. }
  144. return purchases, resp, nil
  145. }
  146. func (s *MarketplaceService) marketplaceURI(endpoint string) string {
  147. url := "marketplace_listing"
  148. if s.Stubbed {
  149. url = "marketplace_listing/stubbed"
  150. }
  151. return url + "/" + endpoint
  152. }