Ejemplo n.º 1
0
 def _get_sf(self):
     sf = SimpleSalesforce(username=self.configuration['username'],
                           password=self.configuration['password'],
                           security_token=self.configuration['token'],
                           sandbox=self.configuration.get('sandbox', False),
                           client_id='Redash')
     return sf
Ejemplo n.º 2
0
def get_salesforce_api():
    """Get an instance of the Salesforce REST API."""
    url = settings.SALESFORCE_LOGIN_URL
    if settings.SALESFORCE_SANDBOX:
        url = url.replace('login', 'test')
    payload = {
        'alg': 'RS256',
        'iss': settings.SALESFORCE_CLIENT_ID,
        'sub': settings.SALESFORCE_USERNAME,
        'aud': url,
        'exp': timegm(datetime.utcnow().utctimetuple()),
    }
    encoded_jwt = jwt.encode(
        payload,
        settings.SALESFORCE_JWT_PRIVATE_KEY,
        algorithm='RS256',
    )
    data = {
        'grant_type': 'urn:ietf:params:oauth:grant-type:jwt-bearer',
        'assertion': encoded_jwt,
    }
    headers = {'Content-Type': 'application/x-www-form-urlencoded'}
    auth_url = urljoin(url, 'services/oauth2/token')
    response = requests.post(url=auth_url, data=data, headers=headers)
    response.raise_for_status()  # maybe VPN or auth problem!
    response_data = response.json()
    return SimpleSalesforce(
        instance_url=response_data['instance_url'],
        session_id=response_data['access_token'],
        domain="test" if settings.SALESFORCE_SANDBOX else None,
        version=settings.SALESFORCE_API_VERSION,
        client_id='sfdoc',
    )
Ejemplo n.º 3
0
def sf_session(jwt):
    return SimpleSalesforce(
        instance_url=jwt["instance_url"],
        session_id=jwt["access_token"],
        client_id="metaci",
        version="42.0",
    )
Ejemplo n.º 4
0
def sf_session(jwt):
    return SimpleSalesforce(
            instance_url=jwt['instance_url'],
            session_id=jwt['access_token'],
            client_id='metaci',
            version='42.0'
    )
Ejemplo n.º 5
0
 def _get_sf(self):
     sf = SimpleSalesforce(
         username=self.configuration["username"],
         password=self.configuration["password"],
         security_token=self.configuration["token"],
         sandbox=self.configuration.get("sandbox", False),
         version=self.configuration.get("api_version", DEFAULT_API_VERSION),
         client_id="Redash",
     )
     return sf
Ejemplo n.º 6
0
def get_devhub_api(*, devhub_username):
    """
    Get an access token (session) for the specified dev hub username.
    This only works if the user has already authorized the connected app
    via an interactive login flow, such as the django-allauth login.
    """
    jwt = jwt_session(SF_CLIENT_ID, SF_CLIENT_KEY, devhub_username)
    return SimpleSalesforce(
        instance_url=jwt["instance_url"],
        session_id=jwt["access_token"],
        client_id="Metecho",
        version="47.0",
    )
Ejemplo n.º 7
0
    def auth_with_token(self, username: str, password: str, api_token: str) -> None:
        """Authorize to Salesforce with security token, username
        and password creating instance.

        :param username: Salesforce API username
        :param password: Salesforce API password
        :param api_token: Salesforce API security token
        """
        self.session = requests.Session()
        self.sf = SimpleSalesforce(
            username=username,
            password=password,
            security_token=api_token,
            domain=self.domain,
            session=self.session,
        )
        self.logger.debug("Salesforce session id: %s", self.session_id)
Ejemplo n.º 8
0
def _get_devhub_api(scratch_org=None):
    """Get an access token.

    Get an access token (session) using the global dev hub username.
    """
    if not settings.DEVHUB_USERNAME:
        raise ImproperlyConfigured(
            "You must set the DEVHUB_USERNAME to connect to a Salesforce organization."
        )
    try:
        jwt = jwt_session(SF_CLIENT_ID, SF_CLIENT_KEY, settings.DEVHUB_USERNAME)
        return SimpleSalesforce(
            instance_url=jwt["instance_url"],
            session_id=jwt["access_token"],
            client_id="MetaDeploy",
            version="49.0",
        )
    except HTTPError as err:
        _handle_sf_error(err, scratch_org=scratch_org)
Ejemplo n.º 9
0
 def test_database_query(self):
     sf = SimpleSalesforce(**settings.SALESFORCE)
     contact_info = sf.query(
         "SELECT Id FROM Contact WHERE Accounts_ID__c = '0'")
     self.assertEqual(contact_info['records'][0]['Id'],
                      u'003U000001erXyqIAE')
Ejemplo n.º 10
0
 def test_login(self):
     sf = SimpleSalesforce(**settings.SALESFORCE)
     self.assertEqual(sf.sf_instance, u'na12.salesforce.com')
Ejemplo n.º 11
0
 def test_database_query(self):
     sf = SimpleSalesforce(**settings.SALESFORCE)
     contact_info = sf.query("SELECT Id FROM Contact")
     self.assertIsNot(contact_info, None)
Ejemplo n.º 12
0

def get_sorted_case_numbers(sf_cases, db_cases):
    set_a = set(sf_cases)
    set_b = set(db_cases)
    new_cases = list(set_a - set_b)
    duplicate_cases = list(set_a.intersection(set_b))
    sorted_cases_dict = {'new_cases': [], 'duplicate_cases': []}
    sorted_cases_dict['new_cases'] = new_cases
    sorted_cases_dict['duplicate_cases'] = duplicate_cases
    return sorted_cases_dict


try:
    salesforce = SimpleSalesforce(username=c.SF_USERNAME,
                                  password=c.SF_PASSWORD,
                                  security_token=c.SF_TOKEN)
except SalesforceError as e:
    util.update_error_log_transactions('sf', e)

sf = Salesforce(salesforce)
records = sf.get_unassigned_cases(q.TEST_MULTI_RECORD_QUERY)

if records:
    conn = db.create_connection('cases.db')
    sf_case_numbers = sf.get_case_numbers(records)
    db_case_numbers = db.get_case_numbers(conn, q.GET_CASE_NUMBERS_QUERY)

    sorted_case_numbers = get_sorted_case_numbers(sf_case_numbers,
                                                  db_case_numbers)