Class: Geet::Github::ApiInterface
- Inherits:
-
Object
- Object
- Geet::Github::ApiInterface
- Extended by:
- T::Sig
- Defined in:
- lib/geet/github/api_interface.rb
Constant Summary collapse
- API_AUTH_USER =
We don’t need the login, as the API key uniquely identifies the user
T.let("", String)
- API_BASE_URL =
T.let("https://api.github.com", String)
- GRAPHQL_API_URL =
T.let("https://api.github.com/graphql", String)
Instance Attribute Summary collapse
-
#repository_path ⇒ Object
readonly
Returns the value of attribute repository_path.
Instance Method Summary collapse
-
#initialize(api_token, repo_path: nil, upstream: nil) ⇒ ApiInterface
constructor
A new instance of ApiInterface.
- #send_graphql_request(query, variables: {}) ⇒ Object
- #send_request(api_path, params: nil, data: nil, multipage: false, http_method: nil) ⇒ Object
- #upstream? ⇒ Boolean
Constructor Details
#initialize(api_token, repo_path: nil, upstream: nil) ⇒ ApiInterface
Returns a new instance of ApiInterface.
30 31 32 33 34 |
# File 'lib/geet/github/api_interface.rb', line 30 def initialize(api_token, repo_path: nil, upstream: nil) @api_token = T.let(api_token, String) @repository_path = T.let(repo_path, T.nilable(String)) @upstream = T.let(upstream, T.nilable(T::Boolean)) end |
Instance Attribute Details
#repository_path ⇒ Object (readonly)
Returns the value of attribute repository_path.
18 19 20 |
# File 'lib/geet/github/api_interface.rb', line 18 def repository_path @repository_path end |
Instance Method Details
#send_graphql_request(query, variables: {}) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/geet/github/api_interface.rb', line 108 def send_graphql_request(query, variables: {}) uri = URI(GRAPHQL_API_URL) Net::HTTP.start(uri.host, use_ssl: true) do |http| request = Net::HTTP::Post.new(uri).tap do it.basic_auth API_AUTH_USER, @api_token it["Accept"] = "application/vnd.github.v3+json" it.body = {query:, variables:}.to_json end response = http.request(request) parsed_response = T.let(nil, T.nilable(T::Hash[String, T.untyped])) if response.body parsed_response = T.cast(T.unsafe(JSON.parse(response.body)), T::Hash[String, T.untyped]) end if error?(response) = decode_and_format_error(T.must(parsed_response)) raise Geet::Shared::HttpError.new(, response.code) end if parsed_response&.key?("errors") = T.cast(parsed_response["errors"], T::Array[T::Hash[String, T.untyped]]).map { |err| T.cast(err["message"], String) }.join(", ") raise Geet::Shared::HttpError.new("GraphQL errors: #{}", response.code) end T.cast(T.must(parsed_response).fetch("data"), T::Hash[String, T.untyped]) end end |
#send_request(api_path, params: nil, data: nil, multipage: false, http_method: nil) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/geet/github/api_interface.rb', line 69 def send_request(api_path, params: nil, data: nil, multipage: false, http_method: nil) address = T.let(api_url(api_path), T.nilable(String)) # filled only on :multipage parsed_responses = T.let([], T::Array[T::Hash[String, T.untyped]]) loop do response = send_http_request(T.must(address), params:, data:, http_method:) parsed_response = T.unsafe(JSON.parse(T.must(response.body))) if response.body if error?(response) = decode_and_format_error(T.cast(parsed_response, T::Hash[String, T.untyped])) raise Geet::Shared::HttpError.new(, response.code) end return parsed_response if !multipage parsed_responses.concat(T.cast(parsed_response, T::Array[T::Hash[String, T.untyped]])) address = link_next_page(response.to_hash) return parsed_responses if address.nil? end end |
#upstream? ⇒ Boolean
37 38 39 |
# File 'lib/geet/github/api_interface.rb', line 37 def upstream? @upstream end |