Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Handle also path in the response for a get #47

Merged
merged 4 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/sharepoint/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,21 @@ def list_documents_page(url)
# - `:link_url` [String] if the requested file is a link, this returns the destination file url
def download(file_path: nil, site_path: nil, link_credentials: {})
meta = get_document(file_path, site_path)
if meta.url.nil?
meta_path = meta.url || meta.path
if meta_path.nil?
url = computed_web_api_url(site_path)
server_relative_url = odata_escape_single_quote "#{site_path}#{file_path}"
download_url "#{url}GetFileByServerRelativeUrl('#{server_relative_url}')/$value"
else # requested file is a link
paths = extract_paths(meta.url)
paths = extract_paths(meta_path)
link_config = { uri: paths[:root] }
if link_credentials.empty?
link_config = config.to_h.merge(link_config)
else
link_config.merge!(link_credentials)
end
link_client = self.class.new(link_config)
result = link_client.download_url meta.url
result = link_client.download_url meta_path
result[:link_url] = meta.url if result[:link_url].nil?
result
end
Expand Down
83 changes: 83 additions & 0 deletions spec/fixtures/responses/get_document_having_path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{
"d": {
"__metadata": {
"id": "Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)",
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)",
"etag": "\"4\"",
"type": "SP.Data.DocumentsItem"
},
"FirstUniqueAncestorSecurableObject": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FirstUniqueAncestorSecurableObject"
}
},
"RoleAssignments": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/RoleAssignments"
}
},
"AttachmentFiles": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/AttachmentFiles"
}
},
"ContentType": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/ContentType"
}
},
"FieldValuesAsHtml": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FieldValuesAsHtml"
}
},
"FieldValuesAsText": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FieldValuesAsText"
}
},
"FieldValuesForEdit": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/FieldValuesForEdit"
}
},
"File": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/File"
}
},
"Folder": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/Folder"
}
},
"ParentList": {
"__deferred": {
"uri": "http://win-jamsdpuujgc/_api/Web/Lists(guid'3314c0cf-d5b0-4d1e-a5f1-9a10fca08bc3')/Items(6)/ParentList"
}
},
"FileSystemObjectType": 0,
"Id": 6,
"ContentTypeId": "0x0101002E97C997BC5CAC4785AA3F57D34080E3",
"Title": "Bari-Rome boarding-pass",
"OData__dlc_DocId": "QHWH36P2KKT2-1-6",
"OData__dlc_DocIdUrl": {
"__metadata": {
"type": "SP.FieldUrlValue"
},
"Description": "QHWH36P2KKT2-1-6",
"Path": "http://win-jamsdpuujgc/_layouts/15/DocIdRedir.aspx?ID=QHWH36P2KKT2-1-6"
},
"URL": null,
"DocumentSetDescription": null,
"EditorId": 1,
"ID": 6,
"Created": "2016-07-17T08:26:35",
"AuthorId": 1,
"Modified": "2016-07-25T05:45:18",
"OData__CopySource": null,
"CheckoutUserId": null,
"OData__UIVersionString": "0.2",
"GUID": "e35ac090-3f54-46aa-8ffd-fb14a0aa1610"
}
}
36 changes: 27 additions & 9 deletions spec/lib/sharepoint/client_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -282,20 +282,38 @@
describe '#download' do
subject(:download) { client.download file_path: file_path }

let(:document_json) { File.read('spec/fixtures/responses/get_document.json') }
let(:document_meta) { client.send :parse_get_document_response, document_json, [] }
let(:file_path) { '/Documents/document.docx' }
let(:expected_content) { File.read('spec/fixtures/responses/document.docx') }
let(:document_meta) { client.send :parse_get_document_response, document_json, [] }

before do
allow(client).to receive(:get_document).and_return(document_meta)
mock_responses('document.docx')
context 'when meta contains URL' do
let(:document_json) { File.read('spec/fixtures/responses/get_document.json') }

before do
allow(client).to receive(:get_document).and_return(document_meta)
mock_responses('document.docx')
end

it 'returns expected hash' do
expect(download).to have_key :file_contents
expect(download).to have_key :link_url
expect(download[:file_contents]).to eq expected_content
end
end

it 'returns expected hash' do
expect(download).to have_key :file_contents
expect(download).to have_key :link_url
expect(download[:file_contents]).to eq expected_content
context 'when meta contains Path' do
let(:document_json) { File.read('spec/fixtures/responses/get_document_having_path.json') }

before do
allow(client).to receive(:get_document).and_return(document_meta)
mock_responses('document.docx')
end

it 'returns expected hash' do
expect(download).to have_key :file_contents
expect(download).to have_key :link_url
expect(download[:file_contents]).to eq expected_content
end
end
end

Expand Down