rikas / zoho_hub

Zoho CRM API V2 Wrapper
MIT License
25 stars 30 forks source link

Attachment handling? #25

Closed artsyca closed 4 years ago

rikas commented 5 years ago

I'm not a Zoho expert, so I don't even know what you're talking about.

I'll try and look at that, but if someone could help (or point me in the right direction) I would appreciate that.

artsyca commented 5 years ago

We're all learning Zoho together, Ricardo --

Attachments are somewhat unique: there are endpoints to access them of which the most recent documentation I've been able to locate is covered here:

https://www.zoho.com/crm/developer/docs/api/get-attachments.html

The endpoints are straightforward enough however I'm not sure how they would best fit into the architecture framework of ZohoHub

TimCummings commented 5 years ago

There's a chance you could get them to work as-is by defining your own record class (inheriting from BaseRecord and adding methods to manipulate attachments by manually issuing requests via ZohoHub.connection. I've done something similar with Notes (which are, admittedly, simpler than attachments.) I can look at integrating one or both of these into ZohoHub, but I unfortunately have no ETA on when right now.

artsyca commented 5 years ago

Yes, ZohoHub.connection works very well, it's possible to query attachments with

ZohoHub.connection.get('https://www.zohoapis.com/crm/v2/{module_api_name}/{record_id}/Attachments')

and this returns a data payload -- testing the ability to post presently

artsyca commented 5 years ago

Alright, so uploading is a bit trickier -- there are instructions here for curl and php at https://www.zoho.com/crm/developer/docs/api/upload-attachment.html but I'm not sure how exactly that translates to ZohoHub.connection.post.

I realize under the hood it's using Faraday and UploadIO, but I simply can't get it to work. I've gotten various errors, including

ArgumentError (Content-Length not given and Transfer-Encoding is not `chunked')
artsyca commented 5 years ago

Here's some possibly relevant code from the 'official' Zoho library -> https://github.com/zoho/ruby-client-library/blob/eec21af93ba5704018e31d045aed48b4d98e7282/lib/ZCRMRecord.rb#L162

artsyca commented 5 years ago

So! I've gotten uploads successfully working, all it needs is for conn.request :multipart to be added to the adapter in connection.rb

Once that's in place, it doesn't seem to affect any other requests, but it allows one to upload a file

ZohoHub.connection.post(
  'https://www.zohoapis.com/crm/v2/{module_api_name}/{record_id}/Attachments', 
  { 
    file: Faraday::UploadIO.new(my_file, 'application/pdf', 'attachment.pdf') 
  }
)

whereupon we get:

{ data: [
    { code: 'SUCCESS', 
        details: { Modified_Time: '2019-09-12T00:23:29-04:00', 
            Modified_By: { name: 'My User', id: 'XXXXXXXXXXXXXXXXXXX' }, 
            Created_Time: '2019-09-12T00:23:29-04:00', 
            id: '1884783000059294219', 
            Created_By: { name: 'My User', id: 'XXXXXXXXXXXXXXXXXXX' } }, 
            message: 'attachment uploaded successfully', 
            status: 'success' 
        }
    ] 
}

Yay! 🎉🥳 -- I've monkey patched my local code, do you suppose we might add this code to the ZohoHub?

rikas commented 5 years ago

@artsyca thanks for the share, I'll try to incorporate this into the gem whenever I have time.

artsyca commented 5 years ago

Further bit of information, it doesn't work with the existing with_connection post method because it calls to_json on params which renders the payload incompatible with multipart

  def post(path, params = {})
    ZohoHub.connection.post(path, params.to_json)
  end
artsyca commented 5 years ago

I'm currently doing something like this --

def attachments
  body = get(File.join(self.class.request_path, id.to_s, 'Attachments'))
  build_response(body[:data])
end

I'm not exactly sure where that takes us, but it's rather helpful nonetheless to be able to do this.. In combination with a memoize plugin, it's a rather handy way of caching a record's attachments..

rikas commented 4 years ago

This was solved by #48