Example #1
0
        /// <summary>
        /// Returns a list of events for the given subject and subject type. A full list of supported subject types can be found at https://help.shopify.com/api/reference/event
        /// </summary>
        /// <param name="options">Options for filtering the result.</param>
        /// <param name="subjectId">Restricts results to just one subject item, e.g. all changes on a product.</param>
        /// <param name="subjectType">The subject's type, e.g. 'Order' or 'Product'. Known subject types are 'Articles', 'Blogs', 'Custom_Collections', 'Comments', 'Orders', 'Pages', 'Products' and 'Smart_Collections'.  A current list of subject types can be found at https://help.shopify.com/api/reference/event </param>
        public async Task <IEnumerable <ShopifyEvent> > ListAsync(long subjectId, string subjectType, ShopifyEventListFilter options = null)
        {
            // Ensure the subject type is plural
            if (!subjectType.Substring(subjectType.Length - 1).Equals("s", System.StringComparison.OrdinalIgnoreCase))
            {
                subjectType = subjectType + "s";
            }

            var req = RequestEngine.CreateRequest($"{subjectType?.ToLower()}/{subjectId}/events.json", Method.GET, "events");

            //Add optional parameters to request
            if (options != null)
            {
                req.Parameters.AddRange(options.ToParameters(ParameterType.GetOrPost));
            }

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyEvent> >(_RestClient, req));
        }
Example #2
0
        /// <summary>
        /// Gets a list of up to 250 of the customer's orders.
        /// </summary>
        /// <param name="customerId">The id of the customer to list orders for.</param>
        /// <param name="options">Options for filtering the list.</param>
        /// <returns>The list of orders matching the filter.</returns>
        public virtual async Task <IEnumerable <ShopifyOrder> > ListForCustomerAsync(long customerId, ShopifyOrderFilter options = null)
        {
            IRestRequest req = RequestEngine.CreateRequest("orders.json", Method.GET, "orders");

            //Add the customer id to the filter
            req.Parameters.Add(new Parameter()
            {
                Name = "customer_id", Value = customerId, Type = ParameterType.GetOrPost
            });

            //Add optional parameters to request
            if (options != null)
            {
                req.Parameters.AddRange(options.ToParameters(ParameterType.GetOrPost));
            }

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyOrder> >(_RestClient, req));
        }
Example #3
0
        /// <summary>
        /// Gets a list of the metafields for a specified resource. Leave both resourceType and resourceId null for shop metafields.
        /// </summary>
        /// <param name="resourceType">The type of shopify resource to obtain metafields for. This could be variants, products, orders, customers, custom_collections.</param>
        /// <param name="resourceId">The Id for the resource type.</param>
        /// <param name="options">The <see cref="ShopifyMetaFieldFilter"/> used to filter results</param>
        /// <returns></returns>
        public virtual async Task <IEnumerable <ShopifyMetaField> > ListAsync(long?resourceId, string resourceType = null, ShopifyMetaFieldFilter options = null)
        {
            string reqPath = "metafields.json";

            if (resourceType != null && resourceId != null)
            {
                reqPath = $"{resourceType}/{resourceId}/metafields.json";
            }
            IRestRequest req = RequestEngine.CreateRequest(reqPath, Method.GET, "metafields");

            //Add optional parameters to request
            if (options != null)
            {
                req.Parameters.AddRange(options.ToParameters(ParameterType.GetOrPost));
            }

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyMetaField> >(_RestClient, req));
        }
Example #4
0
        /// <summary>
        /// Gets a count of all of the shop's webhooks.
        /// </summary>
        /// <param name="address">An optional filter for the address property. When used, this method will only count webhooks with the given address.</param>
        /// <param name="topic">An optional filter for the topic property. When used, this method will only count webhooks with the given topic. A full list of topics can be found at https://help.shopify.com/api/reference/webhook. </param>
        /// <returns>The count of all webhooks for the shop.</returns>
        public virtual async Task <int> CountAsync(string address = null, string topic = null)
        {
            IRestRequest req = RequestEngine.CreateRequest("webhooks/count.json", Method.GET);

            //Add optional parameters to request
            if (string.IsNullOrEmpty(address) == false)
            {
                req.AddParameter("address", address);
            }
            if (!string.IsNullOrEmpty(topic))
            {
                req.AddParameter("topic", topic);
            }

            JToken responseObject = await RequestEngine.ExecuteRequestAsync(_RestClient, req);

            //Response looks like { "count" : 123 }. Does not warrant its own class.
            return(responseObject.Value <int>("count"));
        }
        /// <summary>
        /// Gets a count of all of the shop's redirects.
        /// </summary>
        /// <param name="path">An optional parameter that filters the result to redirects with the given path.</param>
        /// <param name="target">An optional parameter that filters the result to redirects with the given target.</param>
        /// <returns>The count of all redirects for the shop.</returns>
        public async Task <int> CountAsync(string path = null, string target = null)
        {
            IRestRequest req = RequestEngine.CreateRequest("redirects/count.json", Method.GET);

            //Add optional parameters to request
            if (string.IsNullOrEmpty(path) == false)
            {
                req.AddParameter("path", path);
            }
            if (string.IsNullOrEmpty(target) == false)
            {
                req.AddParameter("target", target);
            }

            JToken responseObject = await RequestEngine.ExecuteRequestAsync(_RestClient, req);

            //Response looks like { "count" : 123 }. Does not warrant its own class.
            return(responseObject.Value <int>("count"));
        }
Example #6
0
        /// <summary>
        /// Creates a new <see cref="ShopifyMetaField"/> associated with the provided resource and resource id. Leave both resourceType and resourceId null for shop metafields.
        /// </summary>
        /// <param name="metafield">A new <see cref="ShopifyMetaField"/>. Id should be set to null.</param>
        /// <param name="resourceId">The Id of the resource the metafield will be associated with. This can be variants, products, orders, customers, custom_collections, etc.</param>
        /// <param name="resourceType">The resource type the metaifeld will be associated with. This can be variants, products, orders, customers, custom_collections, etc.</param>
        /// <returns>The new <see cref="ShopifyMetaField"/>.</returns>
        public async Task <ShopifyMetaField> CreateAsync(ShopifyMetaField metafield, long?resourceId, string resourceType = null)
        {
            string reqPath = "metafields.json";

            if (resourceType != null && resourceId != null)
            {
                reqPath = $"{resourceType}/{resourceId}/metafields.json";
            }
            IRestRequest req = RequestEngine.CreateRequest(reqPath, Method.POST, "metafield");

            //Build the request body
            Dictionary <string, object> body = new Dictionary <string, object>()
            {
                { "metafield", metafield }
            };

            req.AddJsonBody(body);

            return(await RequestEngine.ExecuteRequestAsync <ShopifyMetaField>(_RestClient, req));
        }
Example #7
0
        /// <summary>
        /// Updates the given <see cref="ShopifyCustomer"/>. Id must not be null.
        /// </summary>
        /// <param name="customer">The <see cref="ShopifyCustomer"/> to update.</param>
        /// <param name="options">Options for updating the customer.</param>
        /// <returns>The updated <see cref="ShopifyCustomer"/>.</returns>
        public async Task <ShopifyCustomer> UpdateAsync(ShopifyCustomer customer, ShopifyCustomerUpdateOptions options = null)
        {
            IRestRequest req = RequestEngine.CreateRequest($"customers/{customer.Id.Value}.json", Method.PUT, "customer");

            var customerBody = customer.ToDictionary();

            if (options != null)
            {
                foreach (var keyValuePair in options.ToDictionary())
                {
                    customerBody.Add(keyValuePair);
                }
            }

            var requestBody = new { customer = customerBody };

            req.AddJsonBody(requestBody);

            return(await RequestEngine.ExecuteRequestAsync <ShopifyCustomer>(_RestClient, req));
        }
Example #8
0
        /// <summary>
        /// Gets a count of the metafields for the given entity type and filter options. Leave both resourceType and resourceId null for shop metafields.
        /// </summary>
        /// <param name="resourceType">The type of shopify resource to obtain metafields for. This could be variants, products, orders, customers, custom_collections.</param>
        /// <param name="resourceId">The Id for the resource type.</param>
        /// <param name="filter">The <see cref="ShopifyMetaFieldFilter"/> used to filter results</param>
        /// <returns>The count of all metafields for the given entity and filter options.</returns>
        public async Task <int> CountAsync(long?resourceId, string resourceType = null, ShopifyMetaFieldFilter filter = null)
        {
            string reqPath = "metafields/count.json";

            if (resourceType != null && resourceId != null)
            {
                reqPath = $"{resourceType}/{resourceId}/metafields/count.json";
            }
            IRestRequest req = RequestEngine.CreateRequest(reqPath, Method.GET);

            //Add optional parameters to request
            if (filter != null)
            {
                req.Parameters.AddRange(filter.ToParameters(ParameterType.GetOrPost));
            }

            JToken responseObject = await RequestEngine.ExecuteRequestAsync(_RestClient, req);

            //Response looks like { "count" : 123 }. Does not warrant its own class.
            return(responseObject.Value <int>("count"));
        }
        /// <summary>
        /// Gets a list of all blogs.
        /// </summary>
        /// <param name="sinceId">Restrict results to after the specified ID</param>
        /// <param name="handle">Filter by Blog handle</param>
        /// <param name="fields">comma-separated list of fields to include in the response</param>
        public async Task <IEnumerable <ShopifyBlog> > ListAsync(long?sinceId = null, string handle = null, string fields = null)
        {
            var request = RequestEngine.CreateRequest("blogs.json", RestSharp.Method.GET, "blogs");

            if (sinceId.HasValue)
            {
                request.AddParameter("since_id", sinceId.Value, RestSharp.ParameterType.GetOrPost);
            }

            if (!string.IsNullOrEmpty(handle))
            {
                request.AddParameter("handle", handle);
            }

            if (!string.IsNullOrEmpty(fields))
            {
                request.AddParameter("fields", fields);
            }

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyBlog> >(_RestClient, request));
        }
Example #10
0
        /// <summary>
        /// Builds an authorization URL for Shopify OAuth integration.
        /// </summary>
        /// <param name="scopes">An array of <see cref="ShopifyAuthorizationScope"/> — the permissions that your app needs to run.</param>
        /// <param name="myShopifyUrl">The shop's *.myshopify.com URL.</param>
        /// <param name="shopifyApiKey">Your app's public API key.</param>
        /// <param name="redirectUrl">An optional URL to redirect the user to after integration. Overrides the Shopify app's default redirect URL.</param>
        /// <returns>The authorization url.</returns>
        public static Uri BuildAuthorizationUrl(IEnumerable <ShopifyAuthorizationScope> scopes, string myShopifyUrl, string shopifyApiKey, string redirectUrl = null)
        {
            //Prepare a uri builder for the shop URL
            UriBuilder builder = new UriBuilder(RequestEngine.BuildShopUri(myShopifyUrl));

            //Build the querystring
            List <KeyValuePair <string, string> > qs = new List <KeyValuePair <string, string> >()
            {
                new KeyValuePair <string, string>("client_id", shopifyApiKey),
                new KeyValuePair <string, string>("scope", string.Join(",", scopes.Select(s => s.ToSerializedString()))),
            };

            if (string.IsNullOrEmpty(redirectUrl) == false)
            {
                qs.Add(new KeyValuePair <string, string>("redirect_uri", redirectUrl));
            }

            builder.Path  = "admin/oauth/authorize";
            builder.Query = string.Join("&", qs.Select(s => $"{s.Key}={s.Value}"));

            return(builder.Uri);
        }
Example #11
0
        /// <summary>
        /// Creates a new <see cref="ShopifyProduct"/> on the store.
        /// </summary>
        /// <param name="product">A new <see cref="ShopifyProduct"/>. Id should be set to null.</param>
        /// <param name="options">Options for creating the product.</param>
        /// <returns>The new <see cref="ShopifyProduct"/>.</returns>
        public virtual async Task <ShopifyProduct> CreateAsync(ShopifyProduct product, ShopifyProductCreateOptions options = null)
        {
            IRestRequest req = RequestEngine.CreateRequest("products.json", Method.POST, "product");

            //Build the request body as a dictionary. Necessary because the create options must be added to the
            //'product' property.
            var productBody = product.ToDictionary();

            if (options != null)
            {
                foreach (var kvp in options.ToDictionary())
                {
                    productBody.Add(kvp);
                }
            }

            var requestBody = new { product = productBody };

            req.AddJsonBody(requestBody);

            return(await RequestEngine.ExecuteRequestAsync <ShopifyProduct>(_RestClient, req));
        }
Example #12
0
        /*
         * I have implemented the API as best as I can but this method keeps returning nothing
         * /// <summary>
         * /// Returns a list of Events on a paticular item eg. all events on a product
         * /// </summary>
         * /// <param name="subjectType">Resticts resluts to just one subject type</param>
         * /// <param name="subjectId">Restics results to just one subject id</param>
         * /// <param name="options">(optional) Filters</param>
         * /// <returns></returns>
         * public async Task<IEnumerable<ShopifyEvent>> ListAsync(string subjectType, long subjectId, ShopifyEventListFilter options = null)
         * {
         *  return await ListAsync(options, subjectType, subjectId);
         * }
         */

        /// <summary>
        /// Returns a list of Events
        /// </summary>
        /// <param name="options">(optional) Filters</param>
        /// <param name="subjectType">(optional) Resticts resluts to just one subject type</param>
        /// <param name="subjectId">(optional) Restics results to just one subject item eg.all changes on a product (subjectType must not be null for this to work)</param>
        /// <returns></returns>
        private async Task <IEnumerable <ShopifyEvent> > ListAsync(ShopifyEventListFilter options = null, string subjectType = null, long?subjectId = null)
        {
            IRestRequest req;

            if (subjectType == null)
            {
                req = RequestEngine.CreateRequest("events.json", Method.GET, "events");
            }
            else
            {
                // As noted above this is currently always returing nothing I am unsure why
                var subjectTypeName = subjectType.ToLower();
                req = RequestEngine.CreateRequest(string.Format("{0}s/#{1}/events.json", subjectTypeName, subjectId), Method.GET, "events");
            }

            //Add optional parameters to request
            if (options != null)
            {
                req.Parameters.AddRange(options.ToParameters(ParameterType.GetOrPost));
            }

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyEvent> >(_RestClient, req));
        }
Example #13
0
        /// <summary>
        /// Deletes a customer with the given Id.
        /// </summary>
        /// <param name="customerId">The customer object's Id.</param>
        public async Task DeleteAsync(long customerId)
        {
            IRestRequest req = RequestEngine.CreateRequest("customers/{0}.json".FormatWith(customerId), Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
        /// <summary>
        /// Lists abandoned checkouts through an async call
        /// </summary>
        /// <returns></returns>
        public virtual async Task <ShopifyAbandonedCheckout> ListAsync()
        {
            var req = RequestEngine.CreateRequest($"checkouts.json", Method.GET, "checkout");

            return(await RequestEngine.ExecuteRequestAsync <ShopifyAbandonedCheckout>(_RestClient, req));
        }
        /// <summary>
        /// Activates a <see cref="ShopifyCharge"/> that the shop owner has accepted.
        /// </summary>
        /// <param name="id">The id of the charge to activate.</param>
        public async Task ActivateAsync(long id)
        {
            IRestRequest req = RequestEngine.CreateRequest($"application_charges/{id}/activate.json", Method.POST);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
        /// <summary>
        /// Cancels a pending fulfillment with the given id.
        /// </summary>
        /// <param name="orderId">The order id to which the fulfillments belong.</param>
        /// <param name="fulfillmentId">The fulfillment's id.</param>
        public async Task <ShopifyFulfillment> CancelAsync(long orderId, long fulfillmentId)
        {
            var req = RequestEngine.CreateRequest($"orders/{orderId}/fulfillments/{fulfillmentId}/cancel.json", Method.POST, "fulfillment");

            return(await RequestEngine.ExecuteRequestAsync <ShopifyFulfillment>(_RestClient, req));
        }
Example #17
0
        /// <summary>
        /// Deletes the webhook with the given Id.
        /// </summary>
        /// <param name="webhookId">The order object's Id.</param>
        public virtual async Task DeleteAsync(long webhookId)
        {
            IRestRequest req = RequestEngine.CreateRequest($"webhooks/{webhookId}.json", Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
Example #18
0
        /// <summary>
        /// Deletes a metafield with the given Id.
        /// </summary>
        /// <param name="metafieldId">The metafield object's Id.</param>
        public async Task DeleteAsync(long metafieldId)
        {
            IRestRequest req = RequestEngine.CreateRequest($"metafields/{metafieldId}.json", Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
        /// <summary>
        /// Retrieves the collection of <see cref="ShopifyMetaField"/> for the given page id.
        /// </summary>
        /// <param name="pageId">The id of the page to retrieve.</param>
        /// <returns>The <see cref="ShopifyPage"/>.</returns>
        public virtual async Task <List <ShopifyMetaField> > GetMetaFieldsAsync(long pageId)
        {
            IRestRequest req = RequestEngine.CreateRequest($"pages/{pageId}/metafields.json", Method.GET, "metafields");

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyMetaField> >(_RestClient, req));
        }
        /// <summary>
        /// Deletes an order with the given Id.
        /// </summary>
        /// <param name="orderId">The order the risk belongs to.</param>
        /// <param name="riskId">The risk's id.</param>
        public virtual async Task DeleteAsync(long orderId, long riskId)
        {
            var req = RequestEngine.CreateRequest($"orders/{orderId}/risks/{riskId}.json", Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
Example #21
0
        /// <summary>
        /// Deletes the <see cref="ShopifyScriptTag"/> with the given Id.
        /// </summary>
        /// <param name="tagId">The tag's Id.</param>
        public async Task DeleteAsync(long tagId)
        {
            IRestRequest req = RequestEngine.CreateRequest("script_tags/{0}.json".FormatWith(tagId), Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
        /// <summary>
        /// Retrieves the <see cref="ShopifyOrderRisk"/> with the given id.
        /// </summary>
        /// <param name="orderId">The order the risk belongs to.</param>
        /// <param name="riskId">The id of the risk to retrieve.</param>
        public virtual async Task <ShopifyOrderRisk> GetAsync(long orderId, long riskId)
        {
            var req = RequestEngine.CreateRequest($"orders/{orderId}/risks/{riskId}.json", Method.GET, "risk");

            return(await RequestEngine.ExecuteRequestAsync <ShopifyOrderRisk>(_RestClient, req));
        }
        /// <summary>
        /// Gets a blog with the given id.
        /// </summary>
        /// <param name="id">The id of the blog you want to retrieve.</param>
        public async Task <ShopifyBlog> GetAsync(long id)
        {
            var request = RequestEngine.CreateRequest($"blogs/{id}.json", RestSharp.Method.GET, "blog");

            return(await RequestEngine.ExecuteRequestAsync <ShopifyBlog>(_RestClient, request));
        }
Example #24
0
        /// <summary>
        /// Forces the shop to uninstall your Shopify app. Uninstalling an application is an irreversible operation. Be entirely sure that you no longer need to make API calls for the shop in which the application has been installed.
        /// </summary>
        public virtual async Task UninstallAppAsync()
        {
            var request = RequestEngine.CreateRequest("api_permissions/current.json", Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, request);
        }
Example #25
0
        /// <summary>
        /// Opens a closed order.
        /// </summary>
        /// <param name="id">The order's id.</param>
        public async Task <ShopifyOrder> OpenAsync(long id)
        {
            var req = RequestEngine.CreateRequest($"orders/{id}/open.json", Method.POST, "order");

            return(await RequestEngine.ExecuteRequestAsync <ShopifyOrder>(_RestClient, req));
        }
        /// <summary>
        /// Deletes a blog with the given id.
        /// </summary>
        /// <param name="id">The id of the blog you want to delete.</param>
        public async Task DeleteAsync(long id)
        {
            var request = RequestEngine.CreateRequest($"blogs/{id}.json", RestSharp.Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, request);
        }
Example #27
0
        /// <summary>
        /// Deletes a ProductImage with the given Id.
        /// </summary>
        /// <param name="productId">The id of the product that counted images belong to.</param>
        /// <param name="imageId">The ProductImage object's Id.</param>
        public virtual async Task DeleteAsync(long productId, long imageId)
        {
            var req = RequestEngine.CreateRequest($"products/{productId}/images/{imageId}.json", Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
        /// <summary>
        /// Gets a count of all blogs.
        /// </summary>
        public async Task <int> CountAsync()
        {
            var request = RequestEngine.CreateRequest("blogs/count.json", RestSharp.Method.GET, "count");

            return(await RequestEngine.ExecuteRequestAsync <int>(_RestClient, request));
        }
        /// <summary>
        /// Deletes a Theme with the given Id.
        /// </summary>
        /// <param name="themeId">The Theme object's Id.</param>
        public async Task DeleteAsync(long themeId)
        {
            IRestRequest req = RequestEngine.CreateRequest($"themes/{themeId}.json", Method.DELETE);

            await RequestEngine.ExecuteRequestAsync(_RestClient, req);
        }
        /// <summary>
        /// Gets a list of all order risks for an order.
        /// </summary>
        /// <param name="orderId">The order the risks belong to.</param>
        public virtual async Task <IEnumerable <ShopifyOrderRisk> > ListAsync(long orderId)
        {
            var req = RequestEngine.CreateRequest($"orders/{orderId}/risks.json", Method.GET, "risks");

            return(await RequestEngine.ExecuteRequestAsync <List <ShopifyOrderRisk> >(_RestClient, req));
        }