Class: OneApi::OneApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/oneapi-ruby/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OneApiClient.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/oneapi-ruby/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://oneapi.infobip.com'
    end

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

    @oneapi_authentication = nil
    @raise_exceptions = true
end

Instance Attribute Details

#raise_exceptionsObject

If true – an exception will be thrown on error, otherwise, you have to check the is_success and exception methods on resulting objects.



15
16
17
# File 'lib/oneapi-ruby/client.rb', line 15

def raise_exceptions
  @raise_exceptions
end

Instance Method Details

#convert_from_json(classs, json, is_error) ⇒ Object



151
152
153
154
155
156
157
158
159
# File 'lib/oneapi-ruby/client.rb', line 151

def convert_from_json(classs, json, is_error)
    result = Conversions.from_json(classs, json, is_error)

    if @raise_exceptions and !result.is_success
        raise "#{result.exception.message_id}: #{result.exception.text} [#{result.exception.variables}]"
    end

    result
end

#execute_GET(url, params = nil) ⇒ Object



89
90
91
# File 'lib/oneapi-ruby/client.rb', line 89

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

#execute_POST(url, params = nil) ⇒ Object



93
94
95
# File 'lib/oneapi-ruby/client.rb', line 93

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

#execute_request(http_method, url, params) ⇒ Object



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
123
124
125
126
# File 'lib/oneapi-ruby/client.rb', line 97

def execute_request(http_method, url, params)
    rest_url = get_rest_url(url)
    uri = URI(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)
        request.set_form_data(params)
    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 "response = #{response.body}"

    return is_success(response), response.body
end

#fill_oneapi_authentication(json, is_success) ⇒ Object



140
141
142
143
144
145
146
147
148
149
# File 'lib/oneapi-ruby/client.rb', line 140

def fill_oneapi_authentication(json, is_success)
    @oneapi_authentication = convert_from_json(OneApiAuthentication, json, !is_success)

    @oneapi_authentication.username = @username
    @oneapi_authentication.password = @password

    @oneapi_authentication.authenticated = @oneapi_authentication.ibsso_token ? @oneapi_authentication.ibsso_token.length > 0 : false

    @oneapi_authentication
end

#get_or_create_client_correlator(client_correlator = nil) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/oneapi-ruby/client.rb', line 46

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



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/oneapi-ruby/client.rb', line 128

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



64
65
66
67
68
69
# File 'lib/oneapi-ruby/client.rb', line 64

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

    is_success
end

#loginObject



35
36
37
38
39
40
41
42
43
44
# File 'lib/oneapi-ruby/client.rb', line 35

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

    is_success, result = execute_POST('/1/customerProfile/login', params)

    return fill_oneapi_authentication(result, is_success)
end

#prepare_headers(request) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/oneapi-ruby/client.rb', line 54

def prepare_headers(request)
    request["User-Agent"] = "OneApi-ruby-#{OneApi::VERSION}"
    if @oneapi_authentication and @oneapi_authentication.ibsso_token
        request['Authorization'] = "IBSSO #{@oneapi_authentication.ibsso_token}"
    else
        auth_string = Base64.encode64("#{@username}:#{@password}").strip
        request['Authorization'] = "Basic #{auth_string}"
    end
end

#urlencode(params) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/oneapi-ruby/client.rb', line 71

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