Class: Baidu::Aip::Base

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/baidu/aip/base.rb

Direct Known Subclasses

PostBase

Constant Summary collapse

TOKEN_URL =
'https://aip.baidubce.com/oauth/2.0/token'

Constants included from Constants

Constants::FACE_DETECT, Constants::FACE_GROUP_COPY_USER, Constants::FACE_GROUP_DELETE_USER, Constants::FACE_GROUP_GET_LIST, Constants::FACE_GROUP_GET_USERS, Constants::FACE_IDENTIFY, Constants::FACE_MATCH, Constants::FACE_MULTI_IDENTIFY, Constants::FACE_USER_ADD, Constants::FACE_USER_DELETE, Constants::FACE_USER_GET, Constants::FACE_USER_UPDATE, Constants::FACE_VERIFY, Constants::IMAGE_AUDIT_ANTI_PORN, Constants::IMAGE_AUDIT_ANTI_PORN_GIF, Constants::IMAGE_AUDIT_ANTI_TERROR, Constants::IMAGE_AUDIT_COMBINATION, Constants::IMAGE_AUDIT_FACE, Constants::IMAGE_AUDIT_USER_DEFINED, Constants::IMAGE_RECOGNITION_ANIMAL, Constants::IMAGE_RECOGNITION_CAR, Constants::IMAGE_RECOGNITION_DISH, Constants::IMAGE_RECOGNITION_LOGO, Constants::IMAGE_RECOGNITION_LOGO_ADD, Constants::IMAGE_RECOGNITION_LOGO_DELETE, Constants::IMAGE_RECOGNITION_OBJECT, Constants::IMAGE_RECOGNITION_PLANT, Constants::IMAGE_SEARCH_PRODUCT, Constants::IMAGE_SEARCH_PRODUCT_ADD, Constants::IMAGE_SEARCH_PRODUCT_DELETE, Constants::IMAGE_SEARCH_SAME, Constants::IMAGE_SEARCH_SAME_ADD, Constants::IMAGE_SEARCH_SAME_DELETE, Constants::IMAGE_SEARCH_SIMILAR, Constants::IMAGE_SEARCH_SIMILAR_ADD, Constants::IMAGE_SEARCH_SIMILAR_DELETE, Constants::KNOWLEDGE_GRAPH_TASK_CREATE, Constants::KNOWLEDGE_GRAPH_TASK_INFO, Constants::KNOWLEDGE_GRAPH_TASK_QUERY, Constants::KNOWLEDGE_GRAPH_TASK_START, Constants::KNOWLEDGE_GRAPH_TASK_STATUS, Constants::KNOWLEDGE_GRAPH_TASK_UPDATE, Constants::NLP_COMMENT_TAG, Constants::NLP_DEP_PARSER, Constants::NLP_DNNLM_CN, Constants::NLP_KEYWORD, Constants::NLP_LEXER, Constants::NLP_LEXER_CUSTOM, Constants::NLP_SENTIMENT_CLASSIFY, Constants::NLP_SIMNET, Constants::NLP_WORD_EMBEDDING, Constants::NLP_WORD_SIM_EMBEDDING, Constants::OCR_ACCURATE, Constants::OCR_ACCURATE_BASIC, Constants::OCR_BANKCARD, Constants::OCR_BUSINESS_LICENSE, Constants::OCR_CUSTOM, Constants::OCR_DRIVING_LICENSE, Constants::OCR_FORM_RECOGNIZE, Constants::OCR_FORM_RESULT_GET, Constants::OCR_GENERAL, Constants::OCR_GENERAL_BASIC, Constants::OCR_GENERAL_ENHANCED, Constants::OCR_IDCARD, Constants::OCR_LICENSE_PLATE, Constants::OCR_RECEIPT, Constants::OCR_VEHICLE_LICENSE, Constants::OCR_WEB_IMAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#clientObject

Returns the value of attribute client.



11
12
13
# File 'lib/baidu/aip/base.rb', line 11

def client
  @client
end

Instance Method Details

#get_paramsObject



20
21
22
# File 'lib/baidu/aip/base.rb', line 20

def get_params
  custom_params.merge intrinsic_params
end

#get_tokenObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/baidu/aip/base.rb', line 42

def get_token
  if client.access_token && from_now(hour(1)) < client.expire_time
    client.access_token
  else
    token_hash = {
      :grant_type => 'client_credentials',
      :client_id => client.api_key,
      :client_secret => client.secret_key
    }

    begin
      token_response = RestClient.post "#{TOKEN_URL}?#{build_url(token_hash)}", {}, {}
      json = JSON(token_response.body)
      client.access_token = json['access_token']
      client.expire_time = Time.now + json['expires_in']
      client.access_token
    rescue RestClient::ExceptionWithResponse => e
      log e.response
      return nil
    end
  end
end

#headersObject



28
29
30
# File 'lib/baidu/aip/base.rb', line 28

def headers
  {accept: :json}
end

#post_paramsObject



24
25
26
# File 'lib/baidu/aip/base.rb', line 24

def post_params
  {}
end

#processObject



32
33
34
35
36
37
38
39
40
# File 'lib/baidu/aip/base.rb', line 32

def process
  begin
    response = RestClient.post "#{service_url}?#{build_url(get_params)}", post_params, headers
    JSON(response.body)
  rescue RestClient::ExceptionWithResponse => e
    log e.response
    JSON(e.response.body)
  end
end

#service_urlObject



15
16
17
18
# File 'lib/baidu/aip/base.rb', line 15

def service_url
  name = underscore(self.class.name.sub('Baidu::Aip::', '').sub('::', '')).upcase
  self.class.const_get(name)
end