Class: ShopifyClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify-client/client.rb,
lib/shopify-client/client/logging.rb,
lib/shopify-client/client/normalise_path.rb

Defined Under Namespace

Classes: Logging, NormalisePath

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(myshopify_domain, access_token = nil) ⇒ Client

Returns a new instance of Client.

Parameters:

  • myshopify_domain (String)
  • access_token (String, nil) (defaults to: nil)

    if request is authenticated



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/shopify-client/client.rb', line 15

def initialize(myshopify_domain, access_token = nil)
  @conn = Faraday.new(
    headers: {
      'X-Shopify-Access-Token' => access_token,
    },
    url: "https://#{myshopify_domain}/admin/api/#{ShopifyClient.config.api_version}",
  ) do |conn|
    conn.adapter :async_http
    # Request throttling to avoid API rate limit.
    conn.use default_throttling_strategy
    # Retry for 429, too many requests.
    conn.use Faraday::Request::Retry, {
      backoff_factor: 2,
      interval: 0.5,
      retry_statuses: [429],
    }
    # Retry for 5xx, server errors.
    conn.use Faraday::Request::Retry, {
      exceptions: [
        Faraday::ConnectionFailed,
        Faraday::RetriableResponse,
        Faraday::ServerError,
        Faraday::SSLError,
        Faraday::TimeoutError,
      ],
      backoff_factor: 2,
      interval: 0.5,
      retry_statuses: (500..599).to_a,
    }
    conn.use FaradayMiddleware::EncodeJson
    conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
    # Add .json suffix if not present (all endpoints use this).
    conn.use NormalisePath
    conn.use Logging
  end

  @myshopify_domain = myshopify_domain
  @access_token = access_token
end

Instance Attribute Details

#access_tokenString (readonly)

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/shopify-client/client.rb', line 12

class Client
  # @param myshopify_domain [String]
  # @param access_token [String, nil] if request is authenticated
  def initialize(myshopify_domain, access_token = nil)
    @conn = Faraday.new(
      headers: {
        'X-Shopify-Access-Token' => access_token,
      },
      url: "https://#{myshopify_domain}/admin/api/#{ShopifyClient.config.api_version}",
    ) do |conn|
      conn.adapter :async_http
      # Request throttling to avoid API rate limit.
      conn.use default_throttling_strategy
      # Retry for 429, too many requests.
      conn.use Faraday::Request::Retry, {
        backoff_factor: 2,
        interval: 0.5,
        retry_statuses: [429],
      }
      # Retry for 5xx, server errors.
      conn.use Faraday::Request::Retry, {
        exceptions: [
          Faraday::ConnectionFailed,
          Faraday::RetriableResponse,
          Faraday::ServerError,
          Faraday::SSLError,
          Faraday::TimeoutError,
        ],
        backoff_factor: 2,
        interval: 0.5,
        retry_statuses: (500..599).to_a,
      }
      conn.use FaradayMiddleware::EncodeJson
      conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
      # Add .json suffix if not present (all endpoints use this).
      conn.use NormalisePath
      conn.use Logging
    end

    @myshopify_domain = myshopify_domain
    @access_token = access_token
  end

  # @return [Throttling::Strategy]
  def default_throttling_strategy
    if defined?(Redis)
      Throttling::RedisStrategy
    else
      Throttling::ThreadLocalStrategy
    end
  end

  attr_reader :myshopify_domain
  attr_reader :access_token

  # @see Faraday::Connection#delete
  #
  # @return [Response]
  def delete(...)
    Response.from_faraday_response(@conn.delete(...), self)
  end

  # @see Faraday::Connection#get
  #
  # @return [Response]
  def get(...)
    Response.from_faraday_response(@conn.get(...), self)
  end

  # @see CachedRequest#initialize
  def get_cached(...)
    CachedRequest.new(...).(self)
  end

  # @see Faraday::Connection#post
  #
  # @return [Response]
  def post(...)
    Response.from_faraday_response(@conn.post(...), self)
  end

  # @see Faraday::Connection#put
  #
  # @return [Response]
  def put(...)
    Response.from_faraday_response(@conn.put(...), self)
  end

  # @param query [String] the GraphQL query
  # @param variables [Hash] the GraphQL variables (if any)
  #
  # @return [Response]
  def graphql(query, variables = {})
    Response.from_faraday_response(@conn.post('graphql', {
      query: query,
      variables: variables,
    }), self)
  end

  # If called with a block, calls {BulkRequest::Operation#call} immediately,
  # else, returns the {BulkRequest::Operation}.
  #
  # @param query [String] the GraphQL query
  #
  # @return [Operation]
  def graphql_bulk(query, &block)
    op = BulkRequest.new.(self, query)

    block ? op.(&block) : op
  end

  # @return [String]
  def inspect
    "#<ShopifyClient::Client (#{@myshopify_domain})>"
  end
end

#myshopify_domainString (readonly)

Returns:

  • (String)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/shopify-client/client.rb', line 12

class Client
  # @param myshopify_domain [String]
  # @param access_token [String, nil] if request is authenticated
  def initialize(myshopify_domain, access_token = nil)
    @conn = Faraday.new(
      headers: {
        'X-Shopify-Access-Token' => access_token,
      },
      url: "https://#{myshopify_domain}/admin/api/#{ShopifyClient.config.api_version}",
    ) do |conn|
      conn.adapter :async_http
      # Request throttling to avoid API rate limit.
      conn.use default_throttling_strategy
      # Retry for 429, too many requests.
      conn.use Faraday::Request::Retry, {
        backoff_factor: 2,
        interval: 0.5,
        retry_statuses: [429],
      }
      # Retry for 5xx, server errors.
      conn.use Faraday::Request::Retry, {
        exceptions: [
          Faraday::ConnectionFailed,
          Faraday::RetriableResponse,
          Faraday::ServerError,
          Faraday::SSLError,
          Faraday::TimeoutError,
        ],
        backoff_factor: 2,
        interval: 0.5,
        retry_statuses: (500..599).to_a,
      }
      conn.use FaradayMiddleware::EncodeJson
      conn.use FaradayMiddleware::ParseJson, content_type: 'application/json'
      # Add .json suffix if not present (all endpoints use this).
      conn.use NormalisePath
      conn.use Logging
    end

    @myshopify_domain = myshopify_domain
    @access_token = access_token
  end

  # @return [Throttling::Strategy]
  def default_throttling_strategy
    if defined?(Redis)
      Throttling::RedisStrategy
    else
      Throttling::ThreadLocalStrategy
    end
  end

  attr_reader :myshopify_domain
  attr_reader :access_token

  # @see Faraday::Connection#delete
  #
  # @return [Response]
  def delete(...)
    Response.from_faraday_response(@conn.delete(...), self)
  end

  # @see Faraday::Connection#get
  #
  # @return [Response]
  def get(...)
    Response.from_faraday_response(@conn.get(...), self)
  end

  # @see CachedRequest#initialize
  def get_cached(...)
    CachedRequest.new(...).(self)
  end

  # @see Faraday::Connection#post
  #
  # @return [Response]
  def post(...)
    Response.from_faraday_response(@conn.post(...), self)
  end

  # @see Faraday::Connection#put
  #
  # @return [Response]
  def put(...)
    Response.from_faraday_response(@conn.put(...), self)
  end

  # @param query [String] the GraphQL query
  # @param variables [Hash] the GraphQL variables (if any)
  #
  # @return [Response]
  def graphql(query, variables = {})
    Response.from_faraday_response(@conn.post('graphql', {
      query: query,
      variables: variables,
    }), self)
  end

  # If called with a block, calls {BulkRequest::Operation#call} immediately,
  # else, returns the {BulkRequest::Operation}.
  #
  # @param query [String] the GraphQL query
  #
  # @return [Operation]
  def graphql_bulk(query, &block)
    op = BulkRequest.new.(self, query)

    block ? op.(&block) : op
  end

  # @return [String]
  def inspect
    "#<ShopifyClient::Client (#{@myshopify_domain})>"
  end
end

Instance Method Details

#default_throttling_strategyThrottling::Strategy



56
57
58
59
60
61
62
# File 'lib/shopify-client/client.rb', line 56

def default_throttling_strategy
  if defined?(Redis)
    Throttling::RedisStrategy
  else
    Throttling::ThreadLocalStrategy
  end
end

#deleteResponse

Returns:

See Also:

  • Faraday::Connection#delete


70
71
72
# File 'lib/shopify-client/client.rb', line 70

def delete(...)
  Response.from_faraday_response(@conn.delete(...), self)
end

#getResponse

Returns:

See Also:

  • Faraday::Connection#get


77
78
79
# File 'lib/shopify-client/client.rb', line 77

def get(...)
  Response.from_faraday_response(@conn.get(...), self)
end

#get_cachedObject



82
83
84
# File 'lib/shopify-client/client.rb', line 82

def get_cached(...)
  CachedRequest.new(...).(self)
end

#graphql(query, variables = {}) ⇒ Response

Parameters:

  • query (String)

    the GraphQL query

  • variables (Hash) (defaults to: {})

    the GraphQL variables (if any)

Returns:



104
105
106
107
108
109
# File 'lib/shopify-client/client.rb', line 104

def graphql(query, variables = {})
  Response.from_faraday_response(@conn.post('graphql', {
    query: query,
    variables: variables,
  }), self)
end

#graphql_bulk(query, &block) ⇒ Operation

If called with a block, calls BulkRequest::Operation#call immediately, else, returns the BulkRequest::Operation.

Parameters:

  • query (String)

    the GraphQL query

Returns:

  • (Operation)


117
118
119
120
121
# File 'lib/shopify-client/client.rb', line 117

def graphql_bulk(query, &block)
  op = BulkRequest.new.(self, query)

  block ? op.(&block) : op
end

#inspectString

Returns:

  • (String)


124
125
126
# File 'lib/shopify-client/client.rb', line 124

def inspect
  "#<ShopifyClient::Client (#{@myshopify_domain})>"
end

#postResponse

Returns:

See Also:

  • Faraday::Connection#post


89
90
91
# File 'lib/shopify-client/client.rb', line 89

def post(...)
  Response.from_faraday_response(@conn.post(...), self)
end

#putResponse

Returns:

See Also:

  • Faraday::Connection#put


96
97
98
# File 'lib/shopify-client/client.rb', line 96

def put(...)
  Response.from_faraday_response(@conn.put(...), self)
end