Class: Gitlab::Request
- Inherits:
-
Object
- Object
- Gitlab::Request
- Defined in:
- lib/gitlab/request.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#body_as_json ⇒ Object
Returns the value of attribute body_as_json.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#pat_prefix ⇒ Object
Returns the value of attribute pat_prefix.
-
#private_token ⇒ Object
Returns the value of attribute private_token.
Class Method Summary collapse
Instance Method Summary collapse
- #authorization_header ⇒ Object
- #connection ⇒ Object
- #default_headers ⇒ Object
- #jsonify_body_content(params) ⇒ Object
- #make_request(method, path, params) ⇒ Object
- #request_defaults(sudo = nil) ⇒ Object
- #user_agent ⇒ Object
- #user_agent=(value) ⇒ Object
- #validate(response) ⇒ Object
Instance Attribute Details
#body_as_json ⇒ Object
Returns the value of attribute body_as_json.
8 9 10 |
# File 'lib/gitlab/request.rb', line 8 def body_as_json @body_as_json end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/gitlab/request.rb', line 8 def endpoint @endpoint end |
#pat_prefix ⇒ Object
Returns the value of attribute pat_prefix.
8 9 10 |
# File 'lib/gitlab/request.rb', line 8 def pat_prefix @pat_prefix end |
#private_token ⇒ Object
Returns the value of attribute private_token.
8 9 10 |
# File 'lib/gitlab/request.rb', line 8 def private_token @private_token end |
Class Method Details
.decode(response) ⇒ Object
26 27 28 29 30 |
# File 'lib/gitlab/request.rb', line 26 def self.decode(response) response ? JSON.parse(response) : {} rescue JSON::ParserError raise Error::Parsing, 'The response is not a valid JSON' end |
.parse(body) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gitlab/request.rb', line 10 def self.parse(body) body = decode(body) if body.is_a?(Hash) ObjectifiedHash.new(body) elsif body.is_a?(Array) PaginatedResponse.new(body.map { |e| ObjectifiedHash.new(e) }) elsif body true elsif !body false else raise Error::Parsing, "Couldn't parse a response body" end end |
Instance Method Details
#authorization_header ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/gitlab/request.rb', line 124 def raise Error::MissingCredentials, 'Please provide a private_token or auth_token for user' unless private_token if private_token.size >= 64 { 'Authorization' => "Bearer #{private_token}" } elsif private_token.start_with?(pat_prefix.to_s) { 'PRIVATE-TOKEN' => private_token } else { 'JOB-TOKEN' => private_token } end end |
#connection ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/gitlab/request.rb', line 75 def connection @connection ||= Faraday.new(url: endpoint) do |conn| conn.request :url_encoded conn..open_timeout = 30 conn..timeout = 60 end end |
#default_headers ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/gitlab/request.rb', line 108 def default_headers { 'Accept' => 'application/json', 'Content-Type' => 'application/x-www-form-urlencoded', 'User-Agent' => user_agent } end |
#jsonify_body_content(params) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/gitlab/request.rb', line 136 def jsonify_body_content(params) return unless params[:body] && params[:multipart] != true return if params[:headers]&.key?('Content-Type') params[:headers] ||= {} params[:headers]['Content-Type'] = 'application/json' end |
#make_request(method, path, params) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gitlab/request.rb', line 83 def make_request(method, path, params) headers = default_headers.merge(params[:headers] || {}) headers['Sudo'] = @sudo if @sudo query = params[:query] || {} body = params[:body] connection.send(method) do |req| req.url(path) req.headers = headers if %w[post put patch].include?(method) if body.is_a?(Hash) && headers['Content-Type'] == 'application/json' req.body = body.to_json elsif body.is_a?(Hash) req.body = URI.encode_www_form(body) else req.body = body end query.each { |k, v| req.params[k] = v } else query.each { |k, v| req.params[k] = v } end end end |
#request_defaults(sudo = nil) ⇒ Object
69 70 71 72 73 |
# File 'lib/gitlab/request.rb', line 69 def request_defaults(sudo = nil) raise Error::MissingCredentials, 'Please set an endpoint to API' unless endpoint @sudo = sudo end |
#user_agent ⇒ Object
116 117 118 |
# File 'lib/gitlab/request.rb', line 116 def user_agent @user_agent || Configuration::DEFAULT_USER_AGENT end |
#user_agent=(value) ⇒ Object
120 121 122 |
# File 'lib/gitlab/request.rb', line 120 def user_agent=(value) @user_agent = value end |
#validate(response) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'lib/gitlab/request.rb', line 59 def validate(response) error_klass = Error.klass(response) raise error_klass, response if error_klass parsed = self.class.parse(response.body) parsed.client = self if parsed.respond_to?(:client=) parsed.parse_headers!(response.headers) if parsed.respond_to?(:parse_headers!) parsed end |