From dfa7393f4d81146e168210fff95aad61d167e914 Mon Sep 17 00:00:00 2001 From: k4th Date: Wed, 16 Jul 2025 14:06:41 +0200 Subject: [PATCH 1/2] Upload image by url --- .../services/product_management/image.rb | 8 + .../services/product_management/image_spec.rb | 9 ++ .../uploads_an_image_from_an_external_URI.yml | 145 ++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 spec/vcr_cassettes/BeyondApi_ProductManagement_Image/_upload/uploads_an_image_from_an_external_URI.yml diff --git a/lib/beyond_api/services/product_management/image.rb b/lib/beyond_api/services/product_management/image.rb index b681d9f..f827811 100644 --- a/lib/beyond_api/services/product_management/image.rb +++ b/lib/beyond_api/services/product_management/image.rb @@ -60,6 +60,14 @@ def upload_multiple(product_id, image_paths, image_names) { image: Utils.faraday_file_parts(image_paths) }, # body { file_name: Utils.encode_filenames(image_names) }) # params end + + # Upload an image from an external resource and add it to a product. The body of the request must contain a single URI of the image to be uploaded. + # + # @see https://developer.epages.com/beyond-docs/#upload_external_product_image + def upload_external(product_id, uri, file_name = '') + file_name = File.basename(URI.parse(uri).path) if file_name.empty? + post("products/#{product_id}/external-images", { data_uri: uri }, { file_name: }) + end end end end diff --git a/spec/beyond_api/services/product_management/image_spec.rb b/spec/beyond_api/services/product_management/image_spec.rb index 87e2555..66bb2ee 100644 --- a/spec/beyond_api/services/product_management/image_spec.rb +++ b/spec/beyond_api/services/product_management/image_spec.rb @@ -30,5 +30,14 @@ expect(response).not_to be nil expect(response.dig(:embedded, :images)).to be_kind_of(Array) end + + it 'uploads an image from an external URI' do + response = client.upload_external('4bf6d53d-dfb2-4468-b6f9-f6e6265bc0bc', + 'https://epages.com/wp-content/uploads/2019/08/video-thumbnail-sell-online-supportpage_button_2.jpg', + 'external-img.jpg') + + expect(response).not_to be nil + expect(response.dig(:links, :data, :href)).to include('external-img.jpg') + end end end diff --git a/spec/vcr_cassettes/BeyondApi_ProductManagement_Image/_upload/uploads_an_image_from_an_external_URI.yml b/spec/vcr_cassettes/BeyondApi_ProductManagement_Image/_upload/uploads_an_image_from_an_external_URI.yml new file mode 100644 index 0000000..a2e6b80 --- /dev/null +++ b/spec/vcr_cassettes/BeyondApi_ProductManagement_Image/_upload/uploads_an_image_from_an_external_URI.yml @@ -0,0 +1,145 @@ +--- +http_interactions: +- request: + method: post + uri: https://team42-beyond-api.beyondshop.cloud/api/oauth/token?grant_type=client_credentials + body: + encoding: UTF-8 + string: "{}" + headers: + Accept: + - application/json + Content-Type: + - application/json + User-Agent: + - Faraday v2.10.1 + Authorization: + - Basic + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 200 + message: OK + headers: + Date: + - Wed, 16 Jul 2025 12:05:03 GMT + Content-Type: + - application/json;charset=utf-8 + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Cache-Control: + - no-store + Pragma: + - no-cache + X-Content-Type-Options: + - nosniff + - nosniff + X-Xss-Protection: + - 1; mode=block + Strict-Transport-Security: + - max-age=31536000 ; includeSubDomains + - max-age=86400 + X-Frame-Options: + - DENY + Server: + - epages-beyond + X-Request-Time: + - '0.088' + X-B3-Traceid: + - aea7997aca63052b44872400907399b9 + X-Hello-Human: + - Come work with us! https://developer.epages.com/devjobs/ + body: + encoding: UTF-8 + string: |- + { + "access_token" : "", + "token_type" : "bearer", + "expires_in" : 899, + "scope" : "orde:r prat:dcur pypr:cur prod:urdc lcnt:u pymt:ur loca:urcd sctg:m shat:cdru rfpr:ur prad:rcd shpz:dcur shad:u ordr:cur shop:u shim:cd cust:urcd clpr:cr legl:ur prda:ru rtpr:rc oset:ur shpr:rcu cset:ru ordp:r catg:cdur nltg:m", + "tenantId" : 8542, + "iat" : 1752667503, + "jti" : "njVOoSe5AKsPaQhXD8IJbXhFWMc=" + } + recorded_at: Wed, 16 Jul 2025 12:05:03 GMT +- request: + method: post + uri: https://team42-beyond-api.beyondshop.cloud/api/products/4bf6d53d-dfb2-4468-b6f9-f6e6265bc0bc/external-images?fileName=external-img.jpg + body: + encoding: UTF-8 + string: '{"dataUri":"https://epages.com/wp-content/uploads/2019/08/video-thumbnail-sell-online-supportpage_button_2.jpg"}' + headers: + Accept: + - application/json + Content-Type: + - application/json + User-Agent: + - Faraday v2.10.1 + Authorization: + - Bearer + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + response: + status: + code: 201 + message: Created + headers: + Date: + - Wed, 16 Jul 2025 12:05:03 GMT + Content-Type: + - application/json + Transfer-Encoding: + - chunked + Connection: + - keep-alive + Location: + - https://team42-beyond-api.beyondshop.cloud/api/products/4bf6d53d-dfb2-4468-b6f9-f6e6265bc0bc/images/ea6bcf98-09bf-4bf9-888a-cd46b6a5fe38 + X-Content-Type-Options: + - nosniff + - nosniff + X-Xss-Protection: + - 1; mode=block + Cache-Control: + - no-cache, no-store, max-age=0, must-revalidate + Pragma: + - no-cache + Expires: + - '0' + X-Frame-Options: + - DENY + Server: + - epages-beyond + X-Request-Time: + - '0.294' + X-B3-Traceid: + - a7d000db594bf37e3b357f3ae0cdb722 + X-Hello-Human: + - Come work with us! https://developer.epages.com/devjobs/ + Strict-Transport-Security: + - max-age=86400 + body: + encoding: UTF-8 + string: |- + { + "position" : 4, + "width" : 426, + "height" : 240, + "_id" : "ea6bcf98-09bf-4bf9-888a-cd46b6a5fe38", + "_links" : { + "self" : { + "href" : "https://team42-beyond-api.beyondshop.cloud/api/products/4bf6d53d-dfb2-4468-b6f9-f6e6265bc0bc/images/ea6bcf98-09bf-4bf9-888a-cd46b6a5fe38" + }, + "data" : { + "href" : "https://team42-beyond-api.beyondshop.cloud/api/core-storage/images/external-img.jpg?hash=976ca6025199cead5b5eea7de0ea0b5d83544d5f{&width,height,upscale}", + "templated" : true + }, + "image" : { + "href" : "https://team42-beyond-api.beyondshop.cloud/api/products/4bf6d53d-dfb2-4468-b6f9-f6e6265bc0bc/images/ea6bcf98-09bf-4bf9-888a-cd46b6a5fe38" + } + } + } + recorded_at: Wed, 16 Jul 2025 12:05:04 GMT +recorded_with: VCR 6.2.0 From 537c41a26ff191516c0dccd4a8831c27f424b495 Mon Sep 17 00:00:00 2001 From: k4th Date: Wed, 16 Jul 2025 15:21:40 +0200 Subject: [PATCH 2/2] Update documentation --- lib/beyond_api/services/product_management/image.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/beyond_api/services/product_management/image.rb b/lib/beyond_api/services/product_management/image.rb index f827811..b1c9423 100644 --- a/lib/beyond_api/services/product_management/image.rb +++ b/lib/beyond_api/services/product_management/image.rb @@ -64,6 +64,17 @@ def upload_multiple(product_id, image_paths, image_names) # Upload an image from an external resource and add it to a product. The body of the request must contain a single URI of the image to be uploaded. # # @see https://developer.epages.com/beyond-docs/#upload_external_product_image + # + # @param product_id [String] the product UUID + # @param uri [String] the URI of the image to be uploaded + # @param file_name [String] the file name of the image (optional) + # + # @return [Hash] + # + # @example + # @client.upload_external('4125b993-49fc-47c8-b9b3-76d8871e4e06', + # 'https://epages.com/wp-content/uploads/2019/08/my-image.jpg', + # 'external-img.jpg') def upload_external(product_id, uri, file_name = '') file_name = File.basename(URI.parse(uri).path) if file_name.empty? post("products/#{product_id}/external-images", { data_uri: uri }, { file_name: })