使用Citrix ShareFile API,如何获取最近上传的文件?

时间:2019-05-20 22:58:27

标签: rest citrix sharefile

我想每小时运行一次或两次,以检索已上传到客户根目录中的所有文件。这将包括嵌套2或3个文件夹的文件。

当前,我们的客户要求有人每小时一次通过仪表板界面手动运行报告,以生成列出这些报告的CSV文件。这很麻烦。

在对ShareFile API docs进行一些谷歌搜索和嗅探之后,我最好的主意是使用Items resource。具体来说,我打算利用高级搜索查询界面中的CreateStartDate字段:

我已经有了我的API密钥,并且我即将开始进行一些测试(将Ruby用于Rails应用程序)。这样做的时候,我想问一下这里是否有更直接或更有效的方法。

1 个答案:

答案 0 :(得分:0)

这是Ruby代码(在Rails应用程序中),该代码返回在过去一小时内发布到帐户主文件夹中的文件。

请注意所需的机密。我对必须包含用户名和密码感到失望。

class ShareFileService
  include HTTParty
  base_uri 'https://YOUR-SUBDOMAIN.sharefile.com'
  attr_reader :auth_token, :response

  def initialize; end

  def authenticate
    return @auth_token if @auth_token.present?

    api_endpoint = '/oauth/token'
    params = {
      'grant_type'    => 'password',
      'client_id'     => Rails.application.secrets.sharefile_client_id,
      'client_secret' => Rails.application.secrets.sharefile_client_secret,
      'username'      => Rails.application.secrets.sharefile_user,
      'password'      => Rails.application.secrets.sharefile_pass
    }
    body = { body: params }
    @response = self.class.post(api_endpoint, body)

    # This is the auth token
    @auth_token = JSON.parse(@response.body)
    @auth_token
  end

  def home_folder
    # https://api.sharefile.com/rest/docs/resource.aspx?name=Items
    # "Returns: home folder for current user"
    token = authenticate
    api_uri = format('https://%s.sf-api.com/sf/v3/Items', token['subdomain'])
    auth_header = format('Bearer %s', token['access_token'])

    headers = { 'authorization' => auth_header }
    params = { '$expand' => 'Children' }
    options = {
      headers: headers,
      query: params
    }
    @response = self.class.get(api_uri, options)

    JSON.parse(@response.body)
  end

  def recent_files
    # http://api.sharefile.com/rest/docs/resource.aspx?name=Items#Advanced_Simple_Search
    # "Returns: home folder for current user"
    token = authenticate
    api_uri = format('https://%s.sf-api.com/sf/v3/Items/AdvancedSimpleSearch', token['subdomain'])
    auth_header = format('Bearer %s', token['access_token'])
    an_hour_ago = (Time.zone.now - 1.hour).iso8601

    # Based on https://stackoverflow.com/a/44565283/9381758
    params = {
      'Query': {
        'ItemType': '',
        'ParentID': '',
        'CreatorID': '',
        'SearchQuery': '',
        'CreateStartDate': an_hour_ago,
        'CreateEndDate': '',
        'ItemNameOnly': false
      },
      'Paging': {
        'Count': 100,
        'Skip': 0
      },
      'Sort': {
        'SortBy': 'CreateStartDate',
        'Ascending': false
      },
      'TimeoutInSeconds': 10
    }
    options = {
      headers: { 'authorization' => auth_header },
      body: params
    }
    @response = self.class.post(api_uri, options)

    JSON.parse(@response.body)
  end
end

样品用量:

$ rails console
> sharefile = ShareFileService.new
> body = sharefile.recent_files
=> {"PartialResults"=>false, "Results"=> ...}
> body.keys
=> ["PartialResults", "Results", "TimedOut", "odata.metadata", "odata.type", "url"]  
> body['PartialResults']
=> false
> body['Results'].length 
=> 100
相关问题