Module: Monkeylearn::Requests

Included in:
Classifiers, Extractors, Tags
Defined in:
lib/monkeylearn/requests.rb

Instance Method Summary collapse

Instance Method Details

#get_connectionObject



103
104
105
106
107
# File 'lib/monkeylearn/requests.rb', line 103

def get_connection
  @conn ||= Faraday.new(url: Monkeylearn.base_url) do |faraday|
    faraday.adapter Faraday.default_adapter # Net::HTTP
  end
end

#get_exception_class(status_code, error_code) ⇒ Object



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
# File 'lib/monkeylearn/requests.rb', line 49

def get_exception_class(status_code, error_code)
  case status_code
  when 422
    return RequestParamsError
  when 401
    return AuthenticationError
  when 403
    case error_code
    when 'MODEL_LIMIT'
      return ModelLimitError
    else
      return ForbiddenError
    end
  when 404
    case error_code
    when 'MODEL_NOT_FOUND'
      return ModelNotFound
    when 'TAG_NOT_FOUND'
      return TagNotFound
    else
      return ResourceNotFound
    end
  when 429
    case error_code
    when 'PLAN_RATE_LIMIT'
      return PlanRateLimitError
    when 'CONCURRENCY_RATE_LIMIT'
      return ConcurrencyRateLimitError
    when 'PLAN_QUERY_LIMIT'
      return PlanQueryLimitError
    else
      return RateLimitError
    end
  when 423
    return ModuleStateError
  else
    return MonkeylearnResponseError
  end
end

#raise_for_status(raw_response) ⇒ Object

Raises:



43
44
45
46
47
# File 'lib/monkeylearn/requests.rb', line 43

def raise_for_status(raw_response)
  body = JSON.parse(raw_response.body)
  error_code = body.fetch("error_code", nil)
  raise get_exception_class(raw_response.status, error_code).new(raw_response)
end

#request(method, path, data = nil, query_params = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/monkeylearn/requests.rb', line 8

def request(method, path, data = nil, query_params = nil)
  unless Monkeylearn.token
    raise MonkeylearnError, 'Please initialize the Monkeylearn library with your API token'
  end

  while true
    response = get_connection.send(method) do |req|
      url = path.to_s
      if query_params
        url += '?' + URI.encode_www_form(query_params)
      end
      req.url url
      req.headers['Authorization'] = 'Token ' + Monkeylearn.token
      req.headers['Content-Type'] = 'application/json'
      req.headers['User-Agent'] = 'ruby-sdk'
      if data
        req.body = data.to_json
      end
    end

    seconds = throttled?(response)
    if seconds && Monkeylearn.retry_if_throttle
      sleep seconds
    else
      break
    end
  end

  if response.status != 200
    raise_for_status(response)
  end

  Monkeylearn::Response.new(response)
end

#throttled?(response) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/monkeylearn/requests.rb', line 89

def throttled?(response)
  return false unless response.status == 429
  body = JSON.parse(response.body)

  case body['error_code']
  when 'CONCURRENCY_RATE_LIMIT'
    seconds = 2
  when 'PLAN_RATE_LIMIT'
    match = /([\d]+) seconds/.match(body['detail'])
    seconds = if match then match[1].to_i else 60 end
  end
  seconds
end