Client.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Gogs\API {
  3. /**
  4. * Gogs API client.
  5. *
  6. * This class initially provide the programmer with a starting
  7. * point (a kind of an "interface" to start from), to keep
  8. * track of the context when going from a user, to a repo,
  9. * organization etc.
  10. *
  11. * @author Joachim M. Giaever (joachim[]giaever.org)
  12. * @version 0.1
  13. */
  14. final class Client {
  15. use \Gogs\Lib\Curl\Client;
  16. /**
  17. * @param string $api_url The base URL for the Gogs API (e.g https://git.domain.tld/api/v1)
  18. * @param string $api_token The token for an authorized user to query Gogs API.
  19. */
  20. public function __construct(string $api_url, string $api_token) {
  21. $this->url = $api_url;
  22. $this->token = $api_token;
  23. }
  24. /**
  25. * Returns a Request\Users to fetch users from the
  26. * Gogs installation.
  27. *
  28. * @see Request\Users class to understand usage (e.g ->load() to fetch all,
  29. * ->search(array params) to search for one or several
  30. * users etc).
  31. *
  32. * @return Request\Users
  33. */
  34. public function users() {
  35. return new Request\Users($this->url, $this->token);
  36. }
  37. /**
  38. * Get a single user from Gogs.
  39. *
  40. * Returns either
  41. * * the authorized user ($name = "" or "me")
  42. * * the specified user ($name = anything else)
  43. *
  44. * @return Request\User
  45. */
  46. public function user(string $name = "me") {
  47. return new Request\User($this->url, $this->token, $name);
  48. }
  49. /**
  50. * Returns an \Request\Repos to fetch repositories
  51. * on the Gogs installation.
  52. *
  53. * @see \Request\Repos to understand usage. Inherits
  54. * the same class as \R\Users, but the usage may differ!
  55. *
  56. * Note! To fetch a particular repo under a user, you
  57. * should go through the user (see method below).
  58. *
  59. * @return Request\Repos
  60. */
  61. public function repos() {
  62. return new Request\Repos($this->url, $this->token);
  63. }
  64. /**
  65. * A wrapper function as get_log on Client wont
  66. * return anything. This is bogus, but....
  67. * this workaround WORKS!
  68. *
  69. * @return array
  70. */
  71. public function get_log() {
  72. return (new Request\User($this->url, $this->token))->get_log();
  73. }
  74. }
  75. }
  76. ?>