o
    Q?h                     @   s   d dl Z d dl mZm Z  d dlmZ d dlmZ d dlmZ d dlm	Z	 dZ
d dlZd dlZG dd	 d	Zd
ede	dB fddZd dlZdedefddZdededefddZd dlZd dlmZ dededefddZdS )    N)Clientgql)RequestsHTTPTransport)db)SHOPS_CREDENTIALS_COLLECTION)Shopz2025-01c                   @   s$   e Zd ZdefddZdddZdS )ShopifyGraphQLClientshopc                 C   s4   |j  dt d| _d|jd| _|j| _|| _dS )z
        Initialize the Shopify GraphQL Client.
        
        :param shop_url: Your Shopify store URL (e.g., 'your-store.myshopify.com')
        :param access_token: The Shopify access token
        z/admin/api/z/graphql.jsonzapplication/json)zContent-TypezX-Shopify-Access-TokenN)urlSHOPIFY_API_VERSIONendpointaccessTokenheadersidr	   )selfr	    r   N/var/www/html/riverr-enterprise-integrations-main/V2/functions/Shopify/main.py__init__   s   
zShopifyGraphQLClient.__init__Nc                 C   sL   ||pi d}t j| j| j|d}|jdkr"td|j d|j | S )a  
        Execute a GraphQL query or mutation.

        :param operation_type: The type of operation ("query" or "mutation")
        :param operation_name: The name of the GraphQL operation (e.g., "products", "productCreate")
        :param input_data: A dictionary of input data for mutations (optional)
        :param fields: A string specifying the fields to return in the response
        :param variables: A dictionary of variables for the query/mutation (optional)
        :return: Parsed JSON response from Shopify
        )query	variables)r   json   zGraphQL request failed:  )requestspostr   r   status_code	Exceptiontextr   )r   r   r   payloadresponser   r   r   execute   s   !
zShopifyGraphQLClient.execute)N)__name__
__module____qualname__r   r   r    r   r   r   r   r      s    r   domainreturnc                 C   s<   t tddddd|  }|rt|d  S dS )z+
    Get a Shopify shop by its domain.
    
platformIdz==2r
   r   N)r   
collectionr   wheregetr   	from_dictto_dict)r$   r   r   r   r   getShopifyShopByDomainO   s   "r-   gidc                 C   s   t d| }|r|dS dS )z
    Convert a Shopify Global ID (GID) to its numeric ID.

    :param gid: Shopify GID (e.g., "gid://shopify/Product/1234567890")
    :return: Extracted numeric ID as a string
    z(\d+)$   N)researchgroup)r.   matchr   r   r   shopifyNumIdY   s   r4   r   typec                 C   s   d| d|  S )a  
    Convert a numeric Shopify ID to a Shopify Global ID (GID).

    :param numeric_id: The numeric ID (e.g., "1234567890")
    :param type: The Shopify resource type (e.g., "Product", "ProductVariant", "Order")
    :return: Shopify GID (e.g., "gid://shopify/Product/1234567890")
    gid://shopify//r   )r   r5   r   r   r   
shopifyGidd   s   r8   )urlparse
shopify_idresource_typec                 C   sB   t | j} d| d}t|| r| S |  rd| d|  S | S )a~  
    Validate if the given Shopify ID is a GID or a numeric ID.
    If numeric, convert it to a valid Shopify GID.
    Also, removes any query parameters like '?id=true'.

    :param shopify_id: The ID to check (either GID or numeric ID)
    :param resource_type: The Shopify resource type (e.g., "Product", "ProductVariant", "Order", etc.)
    :return: The correct Shopify GID
    z^gid://shopify/z/\d+$r6   r7   )r9   pathr0   r3   isdigit)r:   r;   gid_patternr   r   r   validate_shopify_ids   s   
r?   )r   r   gql.transport.requestsr   configs.firebaser   constants.collectionsr   V2.functions.Shops.mainr   r   r   r   r   strr-   r0   r4   r8   urllib.parser9   r?   r   r   r   r   <module>   s"    @