Class: Openapply::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty, GetManyStudents, GetOneStudent, Put
Defined in:
lib/openapply/client.rb

Constant Summary collapse

NET_EXCEPTIONS =
[Net::ReadTimeout, Net::OpenTimeout, TooManyRequestError]
API_TIMEOUT =
ENV['OA_TIMEOUT'].to_i == 0 ? 5 : ENV['OA_TIMEOUT'].to_i

Instance Method Summary collapse

Methods included from GetManyStudents

#many_ids_updated_time, #many_students_details, #many_students_details_by_ids, #many_students_ids, #many_students_summaries, #url_for_many_students_summaries

Methods included from GetOneStudent

#one_student_details_by_id, #one_student_payments_by_id, #one_student_record_by_id

Methods included from Put

#update_student_id

Constructor Details

#initialize(url: nil, client_id: nil, client_secret: nil, token: nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • url (String) (defaults to: nil)

    Base uri

  • client_id (String) (defaults to: nil)
  • client_secret (String) (defaults to: nil)
  • token (String) (defaults to: nil)

    if present will not generate an auth token

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/openapply/client.rb', line 27

def initialize(url: nil, client_id: nil, client_secret: nil, token: nil)
  @api_url     = format_api_url(url || ENV['OA_BASE_URI'])
  @api_client_id     = client_id || ENV['OA_CLIENT_ID']
  @api_client_secret     = client_secret || ENV['OA_CLIENT_SECRET']
  @api_key             = token

  raise ArgumentError, 'OA_BASE_URI is missing'   if api_url.nil? or
                                                      api_url.empty?
  raise ArgumentError, 'OA_CLIENT_ID is missing' if api_client_id.nil? or
                                                      api_client_id.empty?
  raise ArgumentError, 'OA_CLIENT_SECRET is missing' if api_client_secret.nil? or
                                                      api_client_secret.empty?

  self.class.base_uri api_url
end

Instance Method Details

#api_client_idObject



51
52
53
# File 'lib/openapply/client.rb', line 51

def api_client_id
  @api_client_id
end

#api_client_secretObject



55
56
57
# File 'lib/openapply/client.rb', line 55

def api_client_secret
  @api_client_secret
end

#api_keyObject



59
60
61
# File 'lib/openapply/client.rb', line 59

def api_key
  @api_key || authentificate.token
end

#api_pathObject



63
64
65
# File 'lib/openapply/client.rb', line 63

def api_path
  "/api/v3"
end

#api_recordsObject



67
68
69
# File 'lib/openapply/client.rb', line 67

def api_records
  ENV['OA_RECORD_COUNT'] || '100'
end

#api_timeoutObject



47
48
49
# File 'lib/openapply/client.rb', line 47

def api_timeout
  API_TIMEOUT
end

#api_urlObject



43
44
45
# File 'lib/openapply/client.rb', line 43

def api_url
  @api_url
end

#authentificateOAuth2::AccessToken

Note:

authentificate using oauth2

Returns:

  • (OAuth2::AccessToken)


211
212
213
214
# File 'lib/openapply/client.rb', line 211

def authentificate
  client = OAuth2::Client.new(api_client_id, api_client_secret, site: api_url)
  return client.client_credentials.get_token
end

#get(url, options = {}) ⇒ Object Also known as: oa_api_call

Note:

Does the actual api call(get) to OpenApply & handles API timeouts gracefully

Parameters:



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
123
# File 'lib/openapply/client.rb', line 75

def get(url, options={})
  max_retries = 3
  times_retried = 0
  begin
    options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8', "Authorization" => auth_token}
    
    if ENV['OA_DEBUG']
      puts "###"
      puts "DEBUG OA CLIENT GET at #{Time.now}"
      puts "request URI: #{url}"
      puts "request options: #{options}"
      puts "###"
    end

    answer = self.class.get(url, options)

    if ENV['OA_DEBUG']
      puts "ANSWER:"
      puts "code: #{answer.code}"
      puts "headers: #{answer.headers}"
      puts "---"
      puts "body:"
      puts answer.body
      puts "###"
    end

    raise TooManyRequestError if answer.too_many_requests?

    return answer
  rescue TooManyRequestError
    puts "TooManyRequestError"
    sleep (answer.headers['X-RateLimit-Period'].to_i + 1)
    retry
  rescue *NET_EXCEPTIONS => error
    if ENV['OA_DEBUG']
      puts "ERROR:"
      puts "error: #{error}"
      puts "message: #{error.message}"
      puts "###"
    end

    if times_retried < max_retries
      times_retried += 1
      retry
    else
      { error: "no response (timeout) from URL: #{url}"  }
    end
  end
end

#oa_answer(url, value = {}, options = {}) ⇒ Object

Note:

checks the info for validity & unpacks the json retubed to a JS format

Note:

by passing in a value such as student_id or status this will automatically trigger the change form get to put

Parameters:

  • url (String)
    • this is the url to do the call

  • value (Hash) (defaults to: {})
    • This is used to update the student_id or status

  • options (defaults to: {})


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/openapply/client.rb', line 193

def oa_answer(url, value={}, options={})
  return { error: 'no url given' }        if url.nil? or url.to_s.eql? ""
  return { error: 'bad url - has space' } if url&.include? " "
  return { error: 'bad api_path' }    unless url&.include? "#{api_path}"
  if value.empty?
    api_answer = send(:get, url, options) if value.empty?
  else
    api_answer = send(:put, url, value, options) unless value.empty?
  end

  return api_answer               unless api_answer.respond_to? "response"
  return { error: 'no response' }     if api_answer.response.nil?
  return { error: 'no response' }     if api_answer.response.to_s.eql? ""
  return JSON.parse(api_answer.response.body, symbolize_names: true)
end

#put(url, value, options = {}) ⇒ Object

Note:

Does the actual api call(put) to OpenApply & handles API timeouts gracefully

Parameters:



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/openapply/client.rb', line 130

def put(url, value, options={})
  # Per emai from Mackenzine on API call formatting, vs what is found on API site
 #  curl -X "PUT" "https://las.openapply.com/api/v1/students/OAID" \
 # -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
 # --data-urlencode "student_id=YOURID" \
 # --data-urlencode "auth_token=YOURTOKEN"
  max_retries = 3
  times_retried = 0
  begin
    options[:headers] = { 'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8', "Authorization" => auth_token}
    options[:body] = value

    if ENV['OA_DEBUG']
      puts "###" if ENV['OA_DEBUG']
      puts "DEBUG OA CLIENT PUT at #{Time.now}"
      puts "request URI: #{url}"
      puts "request options: #{options}"
      puts "###"
    end

    answer = self.class.put(url, options)

    if ENV['OA_DEBUG']
      puts "ANSWER:"
      puts "code: #{answer.code}"
      puts "headers: #{answer.headers}"
      puts "---"
      puts "body:"
      puts answer.body
      puts "###"
    end

    raise TooManyRequestError if answer.too_many_requests?

    return answer

  rescue TooManyRequestError
    puts "TooManyRequestError"
    sleep (answer.headers['X-RateLimit-Period'].to_i + 1)
    retry
  rescue *NET_EXCEPTIONS => error
    if ENV['OA_DEBUG']
      puts "ERROR:"
      puts "error: #{error}"
      puts "message: #{error.message}"
      puts "###"
    end

    if times_retried < max_retries
      times_retried += 1
      retry
    else
      { error: "no response (timeout) from URL: #{url}"  }
    end
  end
end