Class: InfobipApi::InfobipApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/infobipapi/client.rb

Direct Known Subclasses

SmsClient

Instance Method Summary collapse

Constructor Details

#initialize(username, password, base_url = nil) ⇒ InfobipApiClient

Returns a new instance of InfobipApiClient.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/infobipapi/client.rb', line 17

def initialize(username, password, base_url=nil)
    @username = username
    @password = password
    if base_url
        @base_url = base_url
    else
        @base_url = 'https://api.infobip.com/'
    end

    if @base_url[-1, 1] != '/'
        @base_url += '/'
    end

    @infobipapi_authentication = nil

    ()
end

Instance Method Details

#convert_from_json(classs, json, is_error) ⇒ Object



159
160
161
# File 'lib/infobipapi/client.rb', line 159

def convert_from_json(classs, json, is_error)
    Conversions.from_json(classs, json, is_error)
end

#execute_GET(url, params = nil) ⇒ Object



94
95
96
# File 'lib/infobipapi/client.rb', line 94

def execute_GET(url, params=nil)
    execute_request('GET', url, params)
end

#execute_POST(url, params = nil) ⇒ Object



98
99
100
# File 'lib/infobipapi/client.rb', line 98

def execute_POST(url, params=nil)
    execute_request('POST', url, params)
end

#execute_request(http_method, url, params) ⇒ Object



102
103
104
105
106
107
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
# File 'lib/infobipapi/client.rb', line 102

def execute_request(http_method, url, params)
    rest_url = get_rest_url(url)
    uri = URI(URI.encode(rest_url))

    if Utils.empty(params)
        params = {}
    end

    if http_method == 'GET'
        request = Net::HTTP::Get.new("#{uri.request_uri}?#{urlencode(params)}")
    elsif http_method == 'POST'
      request = Net::HTTP::Post.new(uri.request_uri)
      #binding.pry if params.has_key?(:binary)
      request.body = params.to_json
    end

    http = Net::HTTP.new(uri.host, uri.port)

    use_ssl = rest_url.start_with? "https"
    if use_ssl
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end

    prepare_headers(request)
    response = http.request(request)

    #puts ""
    #puts "response: #{response.inspect}"
    #puts "body: #{response.body}"
    #puts ""

    return is_success(response), response.body
end

#fill_infobipapi_authentication(json, is_success) ⇒ Object



149
150
151
152
153
154
155
156
157
# File 'lib/infobipapi/client.rb', line 149

def fill_infobipapi_authentication(json, is_success)
    @infobipapi_authentication = convert_from_json(AuthenticationAnswer, json, !is_success)


    @infobipapi_authentication = nil if @infobipapi_authentication.token.nil? \
      || @infobipapi_authentication.token.length == 0

    @infobipapi_authentication
end

#get_or_create_client_correlator(client_correlator = nil) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/infobipapi/client.rb', line 50

def get_or_create_client_correlator(client_correlator=nil)
    if client_correlator
        return client_correlator
    end

    return Utils.get_random_alphanumeric_string()
end

#get_rest_url(rest_path) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/infobipapi/client.rb', line 137

def get_rest_url(rest_path)
    if not rest_path
        return @base_url
    end

    if rest_path[0, 1] == '/'
        return @base_url + rest_path[1, rest_path.length]
    end

    @base_url + rest_path
end

#is_success(response) ⇒ Object



69
70
71
72
73
74
# File 'lib/infobipapi/client.rb', line 69

def is_success(response)
    http_code = response.code.to_i
    is_success = 200 <= http_code && http_code < 300

    is_success
end

#loginObject



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/infobipapi/client.rb', line 36

def ()
    params = {
      'username' => @username,
      'password' => @password,
    }

    is_success, result = execute_POST('auth/1/session', params)

    filled = fill_infobipapi_authentication(result, is_success)
    #puts ""
    #puts "login: #{filled.inspect}"
    return filled
end

#prepare_headers(request) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/infobipapi/client.rb', line 58

def prepare_headers(request)
    request["User-Agent"] = "InfobipApi-#{InfobipApi::VERSION}"
    request["Content-Type"] = "application/json"
    if @infobipapi_authentication and @infobipapi_authentication.token
        request['Authorization'] = "IBSSO #{@infobipapi_authentication.token}"
    else
        auth_string = Base64.encode64("#{@username}:#{@password}").strip
        request['Authorization'] = "Basic #{auth_string}"
    end
end

#urlencode(params) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/infobipapi/client.rb', line 76

def urlencode(params)
    if Utils.empty(params)
        return ''
    end
    if params.instance_of? String
        return URI.encode(params)
    end
    result = ''
    params.each_key do |key|
        if ! Utils.empty(result)
            result += '&'
        end
        result += URI.encode(key.to_s) + '=' + URI.encode(params[key].to_s)
    end

    return result
end