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



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
# File 'lib/shopify-client/client.rb', line 14

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|
    # 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)


11
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
# File 'lib/shopify-client/client.rb', line 11

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|
      # 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)


11
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
# File 'lib/shopify-client/client.rb', line 11

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|
      # 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



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

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

#deleteResponse

Returns:

See Also:

  • Faraday::Connection#delete


68
69
70
# File 'lib/shopify-client/client.rb', line 68

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

#getResponse

Returns:

See Also:

  • Faraday::Connection#get


75
76
77
# File 'lib/shopify-client/client.rb', line 75

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

#get_cachedObject



80
81
82
# File 'lib/shopify-client/client.rb', line 80

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:



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

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)


115
116
117
118
119
# File 'lib/shopify-client/client.rb', line 115

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

  block ? op.(&block) : op
end

#inspectString

Returns:

  • (String)


122
123
124
# File 'lib/shopify-client/client.rb', line 122

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

#postResponse

Returns:

See Also:

  • Faraday::Connection#post


87
88
89
# File 'lib/shopify-client/client.rb', line 87

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

#putResponse

Returns:

See Also:

  • Faraday::Connection#put


94
95
96
# File 'lib/shopify-client/client.rb', line 94

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