Sha256: 433f22aee6f5cda6b875981be0225944c80c6689452b339be512df59a45dfbc0

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

require 'faraday'

module ApigeeCli
  class Base
    attr_accessor :org, :environment

    def initialize(environment = nil)
      @username     = ApigeeCli.configuration.username
      @password     = ApigeeCli.configuration.password
      @org          = ApigeeCli.configuration.org
      @environment  = environment || ApigeeCli.configuration.environment
    end

    def get(url, params = {})
      conn = Faraday.new(url: url)
      conn.basic_auth(@username, @password)
      conn.get do |request|
        request.params = params
      end
    end

    def upload_file(url, file)
      conn = Faraday.new(url: url)
      conn.basic_auth(@username, @password)
      conn.post do |request|
        request.headers['Content-Type'] = "application/octet-stream"
        request.headers['Content-Length'] = File.size(file).to_s
        request.body = Faraday::UploadIO.new(file, 'text/plain')
      end
    end

    def post(url, body)
      conn = Faraday.new(url: url)
      conn.basic_auth(@username, @password)
      conn.post do |request|
        request.headers['Content-Type'] = "application/json"
        request.body = body.to_json
      end
    end

    def put(url, body)
      conn = Faraday.new(url: url)
      conn.basic_auth(@username, @password)
      conn.put do |request|
        request.headers['Content-Type'] = "application/json"
        request.body = body.to_json
      end
    end

    def delete(url)
      conn = Faraday.new(url: url)
      conn.basic_auth(@username, @password)
      conn.delete
    end

    def response_error(response)
      raise "Response Error: #{response.status} #{response.body}"
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
apigee_cli-0.0.3 lib/apigee_cli/base.rb
apigee_cli-0.0.2 lib/apigee_cli/base.rb
apigee_cli-0.0.1 lib/apigee_cli/base.rb