SalesforceXyToolsCore/Python上でSalesforceのREST APIへアクセス

# The original page link
[SalesforceXyToolsCore/Python上でSalesforceのREST APIへアクセス](http://salesforcexytools.com/SalesforceXyToolsCore-JP/SalesforceXyToolsCoreJP/008-run-rest-api.html)

# Topic

* [SalesforceXyToolsCore](http://salesforcexytools.com/categories/SalesforceXyToolsCore-JP/)を使ってSalesforceのREST APIへアクセス 


# Apexclass内容を取得する

* Salesforce組織のユーザ名、パスワード、Apiバージョン、Product/Sandboxを設定してください。

```python
from SalesforceXytoolsCore import *
import pprint

config = {
    "api_version": 42.0, 
    "username": "sfdc username", 
    "password": "sfdc password", 
    "security_token": "", 
    "is_sandbox": True
}

rest_api = RestApi(username=config["username"], 
                password=config["password"], 
                security_token=config["security_token"], 
                sandbox=config["is_sandbox"],
                version=config["api_version"]
                )

"""
Run Rest API : query apexclass
"""
sel_string = "Select Id, Name From ApexClass Limit 3"
params = {'q': sel_string}
result = rest_api.restful('tooling/query', params)
pprint.pprint(result)

```

**結果を確認**

```json
OrderedDict([('size', 3),
             ('totalSize', 3),
             ('done', True),
             ('queryLocator', None),
             ('entityTypeName', 'ApexClass'),
             ('records',
              [OrderedDict([('attributes',
                             OrderedDict([('type', 'ApexClass'),
                                          ('url',
                                           '/services/data/v42.0/tooling/sobjects/ApexClass/01pXXXXXXXXXXXXX')])),
                            ('Id', '01pXXXXXXXXXXXXX'),
                            ('Name', 'OpportXXXXXXX')]),
               OrderedDict([('attributes',
                             OrderedDict([('type', 'ApexClass'),
                                          ('url',
                                           '/services/data/v42.0/tooling/sobjects/ApexClass/01pXXXXXXXXXXXXX')])),
                            ('Id', '01pXXXXXXXXXXXXX'),
                            ('Name', 'DaoXXXXXXXXXXXXX')]),
               OrderedDict([('attributes',
                             OrderedDict([('type', 'ApexClass'),
                                          ('url',
                                           '/services/data/v42.0/tooling/sobjects/ApexClass/01pXXXXXXXXXXXXX')])),
                            ('Id', '01pXXXXXXXXXXXXX'),
                            ('Name', 'ExiaXXXXXXXXXXXXX')])])])
```


# ApexCodeCoverageカバー率を取得する
```python

"""
Run Rest API : query ApexCodeCoverage
"""
sel_string = "SELECT Id, ApexTestClassId, TestMethodName, ApexClassorTriggerId, NumLinesCovered, NumLinesUncovered, Coverage FROM ApexCodeCoverage"
params = {'q': sel_string}
result = rest_api.restful(
    path='tooling/query', 
    params=params,
    method='GET'
)
pprint.pprint(result)

```


# Sobjects一覧を取得する
```python

"""
Run Rest API : search sobjects
"""
result = rest_api.call_rest(
    method='GET',
    path='/services/data/v37.0/sobjects', 
    params={},
)
pprint.pprint(result)

```

結果を確認

```json
{'encoding': 'UTF-8',
 'maxBatchSize': 200,
 'sobjects': [
              {'activateable': False,
               'createable': True,
               'custom': False,
               'customSetting': False,
               'deletable': True,
               'deprecatedAndHidden': False,
               'feedEnabled': True,
               'keyPrefix': '006',
               'label': 'Opportunity',
               'labelPlural': 'Opportunities',
               'layoutable': True,
               'mergeable': False,
               'mruEnabled': True,
               'name': 'Opportunity',
               'queryable': True,
               'replicateable': True,
               'retrieveable': True,
               'searchable': True,
               'triggerable': True,
               'undeletable': True,
               'updateable': True,
               'urls': {'approvalLayouts': '/services/data/v37.0/sobjects/Opportunity/describe/approvalLayouts',
                        'compactLayouts': '/services/data/v37.0/sobjects/Opportunity/describe/compactLayouts',
                        'defaultValues': '/services/data/v37.0/sobjects/Opportunity/defaultValues?recordTypeId&fields',
                        'describe': '/services/data/v37.0/sobjects/Opportunity/describe',
                        'layouts': '/services/data/v37.0/sobjects/Opportunity/describe/layouts',
                        'listviews': '/services/data/v37.0/sobjects/Opportunity/listviews',
                        'quickActions': '/services/data/v37.0/sobjects/Opportunity/quickActions',
                        'rowTemplate': '/services/data/v37.0/sobjects/Opportunity/{ID}',
                        'sobject': '/services/data/v37.0/sobjects/Opportunity'}},
    ................
    ................
    .....省略.......
    ................
    ................
 ]
}
```