huginn_bigcommerce_product_agent 1.10.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c6e30baa91c2309f76692a19fcba067b76749dbf0a43a608c01c8b67e77e2151
4
+ data.tar.gz: c234d8636bf50a12cf170494d7a479cc171482b54c724307c317e059233e723f
5
+ SHA512:
6
+ metadata.gz: 8d787746d57c3eb0a1643da3b586c4e2ad31860912409b0b17e7e44cf5f23d986f5c834231bd54eb2c18249639e02fa0860858e6c609820d1c6e84172c95e078
7
+ data.tar.gz: 2b045dcce2130e261a4fbf3912efe019d87cfb169788d922315a4f8fd016f4addc82eb238be1964cdb7c8bb5ae80342aae82f46d75b7c3263b047eef713065c0
@@ -0,0 +1,89 @@
1
+
2
+ module BigcommerceProductAgent
3
+ module Client
4
+ class AbstractClient
5
+ @client = nil
6
+ @uri_base = ''
7
+
8
+ def initialize(
9
+ store_hash,
10
+ client_id,
11
+ access_token,
12
+ params = {
13
+ endpoint: 'https://api.bigcommerce.com',
14
+ api_version: 'v3',
15
+ }
16
+ )
17
+ @headers = {
18
+ 'X-Auth-Client' => client_id,
19
+ 'X-Auth-Token' => access_token,
20
+ 'Content-Type' => 'application/json',
21
+ 'Accept' => 'application/json'
22
+ }
23
+ @store_hash = store_hash
24
+ @endpoint = params[:endpoint]
25
+ @api_version = params[:api_version]
26
+ end
27
+
28
+ def self.uri_base
29
+ @uri_base
30
+ end
31
+
32
+ def uri_base
33
+ self.class.uri_base
34
+ end
35
+
36
+ def client
37
+ if !@client
38
+ @client = Faraday.new({ url: @endpoint, headers: @headers }) do |conn|
39
+ conn.use Faraday::Response::RaiseError
40
+ conn.response :logger, nil, { headers: true, bodies: true }
41
+ conn.response :json, :content_type => 'application/json'
42
+ conn.adapter Faraday.default_adapter
43
+ end
44
+ end
45
+
46
+ return @client
47
+ end
48
+
49
+ def uri(params = {})
50
+ u = "/stores/#{@store_hash}/#{@api_version}/#{uri_base}"
51
+
52
+ params.each do |key,val|
53
+ u = u.gsub(":#{key.to_s}", val.to_s)
54
+ end
55
+
56
+ # remove params that weren't provided
57
+ u = u.gsub(/(\/\:[^\/]+)/, '')
58
+
59
+ return u
60
+ end
61
+
62
+ def index(params = {})
63
+ response = client.get(uri, params)
64
+ return response.body['data']
65
+ end
66
+
67
+ def create(payload)
68
+ raise "not implemented yet."
69
+ end
70
+
71
+ def update(payload)
72
+ raise "not implemented yet."
73
+ end
74
+
75
+ def delete(payload)
76
+ raise "not implemented yet."
77
+ end
78
+
79
+ def upsert(payload)
80
+ raise "not implemented yet."
81
+ end
82
+
83
+ def get(url_params = {}, params = {})
84
+ response = client.get(uri(url_params), params)
85
+ return response.body['data']
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,36 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class CustomField < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/custom-fields/:custom_field_id'
5
+
6
+ def create(product_id, payload)
7
+ response = client.post(uri(product_id: product_id), payload.to_json)
8
+ return response.body['data']
9
+ end
10
+
11
+ def update(product_id, payload)
12
+ id = payload.delete('id')
13
+ response = client.put(uri(product_id: product_id, custom_field_id: id), payload.to_json)
14
+ return response.body['data']
15
+ end
16
+
17
+ def delete(product_id, custom_field_id)
18
+ client.delete(uri(product_id: product_id, custom_field_id: custom_field_id))
19
+ end
20
+
21
+ def upsert(product_id, payload)
22
+ begin
23
+ payload['id'] = payload.delete(:id) unless payload[:id].nil?
24
+ if payload['id']
25
+ return update(product_id, payload)
26
+ else
27
+ return create(product_id, payload)
28
+ end
29
+ rescue Faraday::Error::ClientError => e
30
+ puts e.inspect
31
+ raise e
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,53 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class MetaField < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/metafields/:meta_field_id'
5
+
6
+ def get_for_product(product_id)
7
+ return [] if product_id.blank?
8
+
9
+ response = client.get(uri(product_id: product_id))
10
+ return response.body['data']
11
+ end
12
+
13
+ def create(meta_field)
14
+ response = client.post(
15
+ uri(product_id: meta_field[:resource_id]),
16
+ meta_field.to_json
17
+ )
18
+
19
+ return response.body['data']
20
+ end
21
+
22
+ def update(meta_field)
23
+ response = client.put(
24
+ uri(product_id: meta_field[:resource_id], meta_field_id: meta_field[:id]),
25
+ meta_field.to_json
26
+ )
27
+
28
+ return response.body['data']
29
+ end
30
+
31
+ def upsert(meta_field)
32
+ begin
33
+ if meta_field[:id]
34
+ return update(meta_field)
35
+ else
36
+ return create(meta_field)
37
+ end
38
+ rescue Faraday::Error::ClientError => e
39
+ puts e.inspect
40
+ raise e
41
+ end
42
+ end
43
+
44
+ def delete(product_id, meta_field_id)
45
+ begin
46
+ client.delete(uri(product_id: product_id, meta_field_id: meta_field_id))
47
+ rescue Faraday::Error::ClientError => e
48
+ raise e, "\n#{e.message}\nFailed to delete meta_field with id = #{meta_field_id}\nfor product with id = #{product_id}\n", e.backtrace
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class Modifier < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/modifiers/:modifier_id'
5
+
6
+ def delete(product_id, modifier_id)
7
+ client.delete(uri(product_id: product_id, modifier_id: modifier_id))
8
+ end
9
+
10
+ def upsert(product_id, modifier)
11
+ begin
12
+ if modifier[:id] || modifier['id']
13
+ modifier['id'] = modifier[:id] unless modifier[:id].nil?
14
+ return update(product_id, modifier)
15
+ else
16
+ return create(product_id, modifier)
17
+ end
18
+ rescue Faraday::Error::ClientError => e
19
+ puts e.inspect
20
+ raise e
21
+ end
22
+ end
23
+
24
+ def update(product_id, modifier)
25
+ response = client.put(uri(product_id: product_id, modifier_id: modifier['id']), modifier.to_json)
26
+ return response.body['data']
27
+ end
28
+
29
+ def create(product_id, modifier)
30
+ response = client.post(uri(product_id: product_id), modifier.to_json)
31
+ return response.body['data']
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,11 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class ModifierValue < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/modifiers/:modifier_id/values/:value_id'
5
+
6
+ def delete(product_id, modifier_id, value_id)
7
+ client.delete(uri(product_id: product_id, modifier_id: modifier_id, value_id: value_id))
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,60 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class Product < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id'
5
+
6
+ def update(id, payload, params={})
7
+ begin
8
+ response = client.put(uri(product_id: id), payload.to_json) do |request|
9
+ request.params.update(params) if params
10
+ end
11
+ rescue Faraday::Error::ClientError => e
12
+ raise e, "\n#{e.message}\nFailed to update product with payload = #{payload.to_json}\n", e.backtrace
13
+ end
14
+
15
+ return response.body['data']
16
+ end
17
+
18
+ def delete(id)
19
+ response = client.delete(uri(product_id: id))
20
+ return true
21
+ end
22
+
23
+ def create(payload, params={})
24
+ begin
25
+ response = client.post(uri, payload.to_json) do |request|
26
+ request.params.update(params) if params
27
+ end
28
+ rescue Faraday::Error::ClientError => e
29
+ raise e, "\n#{e.message}\nFailed to create product with payload = #{payload.to_json}\n", e.backtrace
30
+ end
31
+
32
+ return response.body['data']
33
+ end
34
+
35
+ def upsert(payload, params={})
36
+ if payload[:id]
37
+ return update(payload[:id], payload, params)
38
+ else
39
+ return create(payload, params)
40
+ end
41
+ end
42
+
43
+ def get_by_skus(skus, include = %w[custom_fields modifiers])
44
+ products = index({
45
+ 'sku:in': skus.join(','),
46
+ include: include.join(','),
47
+ })
48
+
49
+ map = {}
50
+
51
+ products.each do |product|
52
+ map[product['sku']] = product
53
+ end
54
+
55
+ map
56
+ end
57
+
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,45 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class ProductOption < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/options/:option_id'
5
+
6
+ def delete(product_id, option_id)
7
+ client.delete(uri(product_id: product_id, option_id: option_id))
8
+ end
9
+
10
+ def delete_all(options)
11
+ options.each do |option|
12
+ delete(option['product_id'], option['id'])
13
+ end
14
+ end
15
+
16
+ def upsert(product_id, option)
17
+ begin
18
+ if option[:id] || option['id']
19
+ option['id'] = option[:id] unless option[:id].nil?
20
+ return update(product_id, option)
21
+ else
22
+ return create(product_id, option)
23
+ end
24
+ rescue Faraday::Error::ClientError => e
25
+ puts e.inspect
26
+ raise e
27
+ end
28
+ end
29
+
30
+ def update(product_id, option)
31
+ response = client.put(uri(product_id: product_id, option_id: option['id']), option.to_json)
32
+ return response.body['data']
33
+ end
34
+
35
+ def create(product_id, option)
36
+ begin
37
+ response = client.post(uri(product_id: product_id), option.to_json)
38
+ return response.body['data']
39
+ rescue Faraday::Error::ClientError => e
40
+ raise e, "\n#{e.message}\nFailed to upsert product_option = #{option.to_json}\nfor product with id = #{product_id}\n", e.backtrace
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,72 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class ProductOptionValue < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/options/:option_id/values/:value_id'
5
+
6
+ def delete(product_id, option_id, value_id)
7
+ client.delete(uri(product_id: product_id, option_id: option_id, value_id: value_id))
8
+ end
9
+
10
+ def delete_all(option, option_values)
11
+ option_values.each do |option_value|
12
+ delete(option['product_id'], option['id'], option_value['id'])
13
+ end
14
+ end
15
+
16
+ def upsert(option, option_value)
17
+ begin
18
+ if option[:product_id] || option['product_id']
19
+ option['product_id'] = option[:product_id] unless option[:product_id].nil?
20
+ end
21
+
22
+ if option[:id] || option['id']
23
+ option['id'] = option[:id] unless option[:id].nil?
24
+ end
25
+
26
+ if option_value[:id] || option_value['id']
27
+ option_value['id'] = option_value[:id] unless option_value[:id].nil?
28
+ return update(option, option_value)
29
+ else
30
+ return create(option, option_value)
31
+ end
32
+ rescue Faraday::Error::ClientError => e
33
+ puts e.inspect
34
+ raise e
35
+ end
36
+ end
37
+
38
+ def upsert_all(option, option_values)
39
+ results = []
40
+ option_values.each do |option_value|
41
+ result = upsert(option, option_value)
42
+ results.push(result)
43
+ end
44
+
45
+ return results
46
+ end
47
+
48
+ def update(option, option_value)
49
+ response = client.put(
50
+ uri(
51
+ product_id: option['product_id'],
52
+ option_id: option['id'],
53
+ value_id: option_value['id'],
54
+ ),
55
+ option_value.to_json,
56
+ )
57
+ return response.body['data']
58
+ end
59
+
60
+ def create(option, option_value)
61
+ response = client.post(
62
+ uri(
63
+ product_id: option['product_id'],
64
+ option_id: option['id'],
65
+ ),
66
+ option_value.to_json,
67
+ )
68
+ return response.body['data']
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,40 @@
1
+ module BigcommerceProductAgent
2
+ module Client
3
+ class ProductVariant < AbstractClient
4
+ @uri_base = 'catalog/products/:product_id/variants/:variant_id'
5
+
6
+ def index(product_id, params = {})
7
+ response = client.get(uri(product_id: product_id), params)
8
+ return response.body['data']
9
+ end
10
+
11
+ def delete(product_id, variant_id)
12
+ client.delete(uri(product_id: product_id, variant_id: variant_id))
13
+ end
14
+
15
+ def upsert(product_id, variant)
16
+ begin
17
+ if variant[:id] || variant['id']
18
+ variant['id'] = variant[:id] unless variant[:id].nil?
19
+ return update(product_id, variant)
20
+ else
21
+ return create(product_id, variant)
22
+ end
23
+ rescue Faraday::Error::ClientError => e
24
+ puts e.inspect
25
+ raise e
26
+ end
27
+ end
28
+
29
+ def update(product_id, variant)
30
+ response = client.put(uri(product_id: product_id, variant_id: variant['id']), variant.to_json)
31
+ return response.body['data']
32
+ end
33
+
34
+ def create(product_id, variant)
35
+ response = client.post(uri(product_id: product_id), variant.to_json)
36
+ return response.body['data']
37
+ end
38
+ end
39
+ end
40
+ end