Class: Cnvrg::API

Inherits:
Object
  • Object
show all
Defined in:
lib/cnvrg/api.rb

Constant Summary collapse

USER_AGENT =
"CnvrgCLI/#{Cnvrg::VERSION}"
ENDPOINT_VERSION =
'v1'

Class Method Summary collapse

Class Method Details

.display_error(response) ⇒ Object



130
131
132
# File 'lib/cnvrg/api.rb', line 130

def self.display_error(response)
    "Oops, an error occurred! Reason: #{response['message']}"
end

.endpoint_uriObject



124
125
126
127
# File 'lib/cnvrg/api.rb', line 124

def self.endpoint_uri
    api = get_api()
    return "#{api}/#{Cnvrg::API::ENDPOINT_VERSION}"
end

.get_apiObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cnvrg/api.rb', line 12

def self.get_api
    home_dir = File.expand_path('~')
    config = ""
    begin
        if File.exist? home_dir+"/.cnvrg/config.yml"
            config = YAML.load_file(home_dir+"/.cnvrg/config.yml")
        else
            return "https://cnvrg.io/api"
        end

    rescue
        return "https://cnvrg.io/api"
    end
    if !config or config.empty? or config.to_h[:api].nil?
        return "https://cnvrg.io/api"
    else
      return config.to_h[:api]
    end
end

.request(resource, method = 'GET', data = {}, parse_request = true) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/cnvrg/api.rb', line 31

def self.request(resource, method = 'GET', data = {}, parse_request = true)
    begin
        n = Netrc.read
    rescue => e
    end

    # Make sure there is an entry for the Acquia API before generating the
    # requests.
    if n['cnvrg.io'].nil?
        puts 'You\'re not logged in'
        puts 'Please log in via `cnvrg login`'
        return
    end

    @user, @pass = n[Cnvrg::Helpers.netrc_domain]
    begin
    conn = Faraday.new
    conn.headers['Auth-Token'] = @pass
    conn.headers['User-Agent'] = "#{Cnvrg::API::USER_AGENT}"

    case method
    when 'GET'
        response = conn.get "#{endpoint_uri}/#{resource}"

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
      when 'POST'
        conn.options.timeout = 420
        conn.options.open_timeout =420
        response = conn.post "#{endpoint_uri}/#{resource}", data

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    when 'POST_FILE'
        conn = Faraday.new do |fr|
            fr.headers['Auth-Token'] = @pass
            fr.headers['User-Agent'] = "#{Cnvrg::API::USER_AGENT}"
            fr.headers["Content-Type"] = "multipart/form-data"


            fr.request :multipart
            fr.request :url_encoded
            fr.adapter :net_http
        end
        conn.options.timeout = 420
        conn.options.open_timeout =420


        # what if windows?
        # data[:file] = Faraday::UploadIO.new(data[:absolute_path], content_type)
          file_base = File.basename(data[:relative_path])
            temp_path = File.expand_path('~')+"/.cnvrg/tmp_files/#{file_base}"
            FileUtils.touch(temp_path)


        data[:file] = Faraday::UploadIO.new("#{temp_path}", "plain/text")

        response = conn.post "#{endpoint_uri}/#{resource}", data

        FileUtils.rm_rf(temp_path)


        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    when 'DELETE'
        response = conn.delete "#{endpoint_uri}/#{resource}", data

        if parse_request == true
            JSON.parse(response.body)
        else
            response
        end
    else
    end
    rescue => e
      puts e
       return nil
    rescue SignalException
        return false

    end

end