Class: VSTS::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/vsts/api_client.rb

Overview

API client for Visual Studio Team Services (VSTS) Manages access tokens and API versions, builds proper requests as expected by the VSTS API

Class Method Summary collapse

Class Method Details

.delete(resource, opts = {}) ⇒ Hash

Helper method for DELETE requests, calls #request

Parameters:

  • resource (String)

    the resource to request under the base_url (ie. "/changesets")

  • opts (Hash) (defaults to: {})

    query options, see #request

Returns:

  • (Hash)

    request results as parsed from json



88
89
90
# File 'lib/vsts/api_client.rb', line 88

def self.delete(resource, opts = {})
  request(:delete, resource, opts)
end

.get(resource, opts = {}) ⇒ Hash

Helper method for GET requests, calls #request

Parameters:

  • resource (String)

    the resource to request under the base_url (ie. "/changesets")

  • opts (Hash) (defaults to: {})

    query options, see #request

Returns:

  • (Hash)

    request results as parsed from json



46
47
48
# File 'lib/vsts/api_client.rb', line 46

def self.get(resource, opts = {})
  request(:get, resource, opts)
end

.patch(resource, payload, opts = {}) ⇒ Hash

Helper method for PATCH requests, calls #request

Parameters:

  • resource (String)

    the resource to request under the base_url (ie. "/changesets")

  • payload (Hash)

    payload to be sent with the request, takes precedence over opts[:payload]

  • opts (Hash) (defaults to: {})

    query options, see #request

Returns:

  • (Hash)

    request results as parsed from json



78
79
80
81
# File 'lib/vsts/api_client.rb', line 78

def self.patch(resource, payload, opts = {})
  opts[:payload] = payload
  request(:patch, resource, opts)
end

.post(resource, payload, opts = {}) ⇒ Hash

Helper method for POST requests, calls #request

Parameters:

  • resource (String)

    the resource to request under the base_url (ie. "/changesets")

  • payload (Hash)

    payload to be sent with the request, takes precedence over opts[:payload]

  • opts (Hash) (defaults to: {})

    query options, see #request

Returns:

  • (Hash)

    request results as parsed from json



56
57
58
59
# File 'lib/vsts/api_client.rb', line 56

def self.post(resource, payload, opts = {})
  opts[:payload] = payload
  request(:post, resource, opts)
end

.put(resource, payload, opts = {}) ⇒ Hash

Helper method for PUT requests, calls #request

Parameters:

  • resource (String)

    the resource to request under the base_url (ie. "/changesets")

  • payload (Hash)

    payload to be sent with the request, takes precedence over opts[:payload]

  • opts (Hash) (defaults to: {})

    query options, see #request

Returns:

  • (Hash)

    request results as parsed from json



67
68
69
70
# File 'lib/vsts/api_client.rb', line 67

def self.put(resource, payload, opts = {})
  opts[:payload] = payload
  request(:put, resource, opts)
end

.request(method, resource, opts = {}) ⇒ Hash

Make an API request

Parameters:

  • method (Symbol)

    the method to be used, can be :get, :put, :post, :delete or :head (will be passed to RestClient)

  • resource (String)

    the resource to request under the base_url (ie. "/changesets")

  • opts (Hash) (defaults to: {})

    options for the request

Options Hash (opts):

  • :payload (Hash)

    payload for the request (if any)

  • :api_version (String)
  • :collection (String)
  • :team_project (String)
  • :area (String)
  • :accept (String)
  • :content_type (String)
  • :urlparams (Hash)

Returns:

  • (Hash)

    request results as parsed from json



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vsts/api_client.rb', line 24

def self.request(method, resource, opts = {})
  url = build_url(resource, opts)
  VSTS.logger.debug("VSTS request: #{method} #{url}") if VSTS.configuration.debug
  req = {
    method: method,
    url: url,
    payload: opts[:payload],
    headers: {
      Authorization: authz_header_value,
      Accept: opts[:accept] || "application/json",
      "Content-Type" => opts[:content_type] || "application/json"
    }
  }
  resp = RestClient::Request.execute(req)
  APIResponse.new(req, resp)
end