From 787e838a5977fc4f1f2de479e09a818224020bde Mon Sep 17 00:00:00 2001
From: Nemo <commits@captnemo.in>
Date: Thu, 07 Mar 2024 12:01:25 +0530
Subject: [PATCH] Handle redirects properly.

Fixes redirect issues.

httparty sends request body at 302 redirects, so we
disable redirect follows for our fileoperations.redirect
call.

minor changes in client as well:

1. drops the token from the body, and sends it in header instead
   as per the new api
2. support additional options
---
 exe/outliner-export    | 18 +++++++++++++-----
 lib/outliner/client.rb | 20 +++++++++++++++-----
 2 files changed, 19 insertions(+), 19 deletions(-)

diff --git a/exe/outliner-export b/exe/outliner-export
index 20e261b..fa47e67 100755
--- a/exe/outliner-export
+++ a/exe/outliner-export
@@ -36,14 +36,14 @@
   break if fop_info_response['data']['state'] == 'complete'
 end
 
-fop_redirect_response = CLIENT.fileOperations__redirect(id: file_operation_id)
+begin
+  fop_redirect_response = CLIENT.fileOperations__redirect({id: file_operation_id}, {no_follow: true})
+rescue HTTParty::RedirectionTooDeep => e
+  response = HTTParty.get e.response.header['location']
+  file = Tempfile.new('download.zip')
+  File.open(file.path, 'w') { |f| f.write(response.body) }
+  `unzip -o "#{file.path}" -d "#{local_directory}"`
+  file.unlink
+end
 
 
-# Extract it to a tempfle
-file = Tempfile.new('download.zip')
-File.open(file.path, 'w') { |f| f.write(fop_redirect_response) }
-
-`unzip -o "#{file.path}" -d "#{local_directory}"`
-
-# Delete tempfile
-file.unlink
diff --git a/lib/outliner/client.rb b/lib/outliner/client.rb
index 0882855..76801d2 100644
--- a/lib/outliner/client.rb
+++ a/lib/outliner/client.rb
@@ -20,19 +20,19 @@
       end
     end
 
-    def method_missing(method_name, params = {})
-      method_name = '/' + method_name.to_s.sub('__', '.')
-      puts method_name
-      body = {token: @token}.merge(params).to_json
+    def method_missing(method_name, params = {}, options = {})
+      method_name = "/#{method_name.to_s.sub('__', '.')}"
+
       options = {
-        body: body,
+        body: params.to_json,
         headers: {
-          'Accept'=>'application/json',
-          'Content-Type': 'application/json',
-          'User-Agent': "Outliner/#{Outliner::VERSION}"
+          'Accept' => 'application/json',
+          'Content-Type' => 'application/json',
+          'User-Agent' => "Outliner/#{Outliner::VERSION}",
+          'Authorization' => "Bearer #{@token}"
         },
-        format: :json
-      }
+        format: :json,
+      }.merge!(options)
 
       self.class.post(method_name, options)
     end
--
rgit 0.1.5