<commit_before># -*- coding: utf-8 -*-

import mimetypes
import logging, time, hashlib, hmac, datetime

from icebergsdk.exceptions import IcebergMissingApplicationSettingsError
from icebergsdk.exceptions import IcebergMissingSsoData

from icebergsdk import resources
from icebergsdk.managers import ResourceManager, UserResourceManager, CartResourceManager
from icebergsdk.mixins.request_mixin import IcebergRequestBase

logger = logging.getLogger('icebergsdk')

class IcebergAPI(IcebergRequestBase):
    def __init__(self, *args, **kwargs):
        super(IcebergAPI, self).__init__(*args, **kwargs)

        self.define_resources() # Resources definition
        self._objects_store = {} # Will store the object for relationship management


    def define_resources(self):
        """
        For faster initialization, set the handler in the resources classes
        """
        resource_classes_list = [
            resources.Application,
            resources.ApplicationCommissionSettings,
            resources.ApplicationPaymentSettings,
            resources.ApplicationPermission,
            resources.ApplicationMerchantPolicies,
            resources.ApplicationTransaction,
            resources.ApplicationUrls,
            resources.Address,
            resources.Brand,
            resources.ChannelPropagationPolicy,
            resources.Country,
            resources.Category,
            resources.MarketPlaceTransaction,
            resources.MerchantAddress,
            resources.MerchantCommissionSettings,
            resources.MerchantOrder,
            resources.MerchantFeed,
            resources.MerchantShippingPolicy,
            resources.MerchantTransaction,
            resources.Message,
            resources.MerchantReview,
            resources.Order,
            resources.UserShoppingPreference,
            resources.Review,
            resources.Return,
            resources.Refund,
            resources.Store,
            resources.StoreBankAccount,
            resources.Transaction,
            resources.ProductChannel,
            resources.ProductChannelLogEvent,
            resources.ProductVariation,
            resources.ProductOffer,
            resources.ProductOfferImage,
            resources.Product,
            resources.Profile,
            resources.Payment,
            resources.Permission,
            resources.ProductFamily,
            resources.ProductFamilySelector,
            resources.Webhook,
            resources.WebhookTrigger,
            resources.WebhookTrigger,
            resources.WebhookTriggerAttempt,
        ]

        for resource_class in resource_classes_list:
            setattr(self, resource_class.__name__, ResourceManager(resource_class=resource_class, api_handler=self))


        self.Cart = CartResourceManager(resource_class=resources.Cart, api_handler=self)
        self.User = UserResourceManager(resource_class=resources.User, api_handler=self)

        ### Missing

        # Return
        # Store Reviews
        # Product Reviews
        # Invoices
        # Currencies
        # Webhooks
        # Feed Management

    def auth_user(self, username, email, first_name = '', last_name = '', is_staff = False, is_superuser = False):
        """
        Method for Iceberg Staff to get or create a user into the platform and get the access_token .

        For authentication, please use the SSO method.
        """
        if not self.conf.ICEBERG_API_PRIVATE_KEY:
            raise IcebergMissingApplicationSettingsError()

        timestamp = int(time.time())
        secret_key = self.conf.ICEBERG_API_PRIVATE_KEY

        to_compose = [username, email, first_name, last_name, str(is_staff), str(is_superuser), str(timestamp)]
        hash_obj = hmac.new(b"%s" % secret_key, b";".join(str(x.encode('utf-8')) for x in to_compose), digestmod = hashlib.sha1)  # Expect strings
        message_auth = hash_obj.hexdigest()

        data = {
            'username': username,
            'email': email,
            'first_name': first_name,
            'last_name': last_name,
            'is_staff': is_staff,
            'is_superuser': is_superuser,
            'timestamp': timestamp,
            'message_auth': message_auth
        }

        response = self.request('user/auth/', args = data)<commit_msg>auth_user encode('utf-8') only if unicode<commit_after># -*- coding: utf-8 -*-

import mimetypes
import logging, time, hashlib, hmac, datetime

from icebergsdk.exceptions import IcebergMissingApplicationSettingsError
from icebergsdk.exceptions import IcebergMissingSsoData

from icebergsdk import resources
from icebergsdk.managers import ResourceManager, UserResourceManager, CartResourceManager
from icebergsdk.mixins.request_mixin import IcebergRequestBase

logger = logging.getLogger('icebergsdk')

class IcebergAPI(IcebergRequestBase):
    def __init__(self, *args, **kwargs):
        super(IcebergAPI, self).__init__(*args, **kwargs)

        self.define_resources() # Resources definition
        self._objects_store = {} # Will store the object for relationship management


    def define_resources(self):
        """
        For faster initialization, set the handler in the resources classes
        """
        resource_classes_list = [
            resources.Application,
            resources.ApplicationCommissionSettings,
            resources.ApplicationPaymentSettings,
            resources.ApplicationPermission,
            resources.ApplicationMerchantPolicies,
            resources.ApplicationTransaction,
            resources.ApplicationUrls,
            resources.Address,
            resources.Brand,
            resources.ChannelPropagationPolicy,
            resources.Country,
            resources.Category,
            resources.MarketPlaceTransaction,
            resources.MerchantAddress,
            resources.MerchantCommissionSettings,
            resources.MerchantOrder,
            resources.MerchantFeed,
            resources.MerchantShippingPolicy,
            resources.MerchantTransaction,
            resources.Message,
            resources.MerchantReview,
            resources.Order,
            resources.UserShoppingPreference,
            resources.Review,
            resources.Return,
            resources.Refund,
            resources.Store,
            resources.StoreBankAccount,
            resources.Transaction,
            resources.ProductChannel,
            resources.ProductChannelLogEvent,
            resources.ProductVariation,
            resources.ProductOffer,
            resources.ProductOfferImage,
            resources.Product,
            resources.Profile,
            resources.Payment,
            resources.Permission,
            resources.ProductFamily,
            resources.ProductFamilySelector,
            resources.Webhook,
            resources.WebhookTrigger,
            resources.WebhookTrigger,
            resources.WebhookTriggerAttempt,
        ]

        for resource_class in resource_classes_list:
            setattr(self, resource_class.__name__, ResourceManager(resource_class=resource_class, api_handler=self))


        self.Cart = CartResourceManager(resource_class=resources.Cart, api_handler=self)
        self.User = UserResourceManager(resource_class=resources.User, api_handler=self)

        ### Missing

        # Return
        # Store Reviews
        # Product Reviews
        # Invoices
        # Currencies
        # Webhooks
        # Feed Management

    def auth_user(self, username, email, first_name = '', last_name = '', is_staff = False, is_superuser = False):
        """
        Method for Iceberg Staff to get or create a user into the platform and get the access_token .

        For authentication, please use the SSO method.
        """
        if not self.conf.ICEBERG_API_PRIVATE_KEY:
            raise IcebergMissingApplicationSettingsError()

        timestamp = int(time.time())
        secret_key = self.conf.ICEBERG_API_PRIVATE_KEY

        to_compose = [username, email, first_name or '', last_name or '', is_staff, is_superuser, timestamp]
        
        to_compose_str = []
        for elem in to_compose:
            if type(elem) == unicode:
                to_compose_str.append(elem.encode('utf-8'))
            else:
                to_compose_str.append(str(elem))

        hash_obj = hmac.new(b"%s" % secret_key, b";".join(to_compose_str), digestmod = hashlib.sha1)  # Expect strings
        message_auth = hash_obj.hexdigest()

        data = {
            'username': username,
            'email': email,
            'first_name': first_name,
            'last_name': last_name,
            'is_staff': is_staff,
            'is_superuser': is_superuser,
            'timestamp': timestamp,
            'message_auth': message_auth
        }

        response = self.request('user/auth/', args = data)
{'fraction_non_alphanumeric': 0.0758053615679207, 'fraction_numerical': 0.0007884658706915972, 'mean_word_length': 2.330457614403601, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5957939', 'n_tokens_mistral': 2210, 'n_tokens_neox': 2151, 'n_words': 458}