Python + ZeepでGaroon SOAP APIからスケジュールの取得

備忘録。

###前回の記事

Python + ZeepでGaroon SOAP APIからユーザー情報取得
https://qiita.com/yamashi6/items/d360546eb4ccf2495951

###参照

Garoon SOAP APIの共通仕様
https://developer.cybozu.io/hc/ja/articles/202228464

予定を検索する
https://developer.cybozu.io/hc/ja/articles/202235474

###ソース


```Python
from zeep import Client
from zeep import xsd

client = Client(wsdl='https://(サブドメイン名).cybozu.com/g/index.csp?WSDL')

# bindしてやらないとスケジュールの取得など使えない
sc_client = client.bind('ScheduleService', 'SchedulePort')

header = xsd.ComplexType([
                xsd.Element('Action', xsd.String()),
                xsd.Element('Security', 
                    xsd.ComplexType([xsd.Element(
                        'UsernameToken',
                        xsd.ComplexType([
                            xsd.Element('Username', xsd.String()),
                            xsd.Element('Password', xsd.String()),
                        ])
                    )]
                )),

                xsd.Element('Timestamp', 
                    xsd.ComplexType([
                        xsd.Element('Created', xsd.String()),
                        xsd.Element('Expires', xsd.String()),
                    ])
                ),

                xsd.Element('Locale', xsd.String()),
            ])

header_value = header(
        Action="ScheduleSearchEvents", 
        # ユーザーとパスワードを変更すること
        Security={'UsernameToken':{'Username':'','Password':''}}, 
        Timestamp={'Created':'2010-08-12T14:45:00Z','Expires':'2037-08-12T14:45:00Z'},
        Locale="jp")

# こちらの書き方でもOK
# header_value = header("ScheduleSearchEvents", 
#         {'UsernameToken':{'Username':'', 'Password':''}}, 
#         {'Created':'2010-08-12T14:45:00Z','Expires':'2037-08-12T14:45:00Z'},
#         'jp') 

request = xsd.Element('parameters',
            xsd.ComplexType([
                xsd.Attribute('text', xsd.String()),
                xsd.Attribute('start', xsd.String()),
                xsd.Attribute('end', xsd.String()),
                xsd.Attribute('title_search', xsd.Boolean()),
                xsd.Attribute('customer_search', xsd.Boolean()),
                xsd.Attribute('memo_search', xsd.Boolean()),
                xsd.Attribute('follow_search', xsd.Boolean()),
                xsd.Attribute('all_repeat_events', xsd.Boolean())])
        )


request_data = request("test","2019-06-06T02:00:00Z", "2019-07-07T02:00:00Z", True, False, False, False, False)

# こちらの書き方でもOK
# request_data = request(
#     text='test', 
#     start= "2019-06-06T02:00:00Z", 
#     end="2019-10-07T02:00:00Z",
#     title_search= True,
#     customer_search=False,
#     memo_search=False,
#     follow_search=False,
#     all_repeat_events=False   
# )

try:
    response = sc_client.ScheduleSearchEvents(request_data, _soapheaders=[header_value])
    # listで返却される
    print(response)

except:
    import traceback
    traceback.print_exc()

```

###実行結果

listで返却される

```Python
[{
    'members': {
        'member': [
            {
                'user': {
                    'id': '1',
                    'order': 0,
                    '_attr_1': {
                        'name': '山田 太郎'
                    }
                },
                'organization': None,
                'facility': None,
                '_value_1': None,
                '_attr_1': None
            }
        ],
        '_value_1': None,
        '_attr_1': None
    },
    'observers': None,
    'customer': None,
    'repeat_info': None,
    'when': {
        '_attr_1': None,
        'datetime': [
            {
                '_value_1': None,
                'start': datetime.datetime(2019, 6, 20, 10, 0, tzinfo=<isodate.tzinfo.Utc object at 0x000001B4773B0F08>),
                'end': datetime.datetime(2019, 6, 20, 12, 0, tzinfo=<isodate.tzinfo.Utc object at 0x000001B4773B0F08>),
                'facility_id': None,
                '_attr_1': {
            }
            }
        ],
        'date': None
    },
    'follows': None,
    'file': [],
    'remove_file_id': [],
    '_value_1': None,
    'id': '11111',
    'event_type': 'normal',
    'version': '111111111',
    'public_type': 'public',
    'plan': '打合',
    'detail': 'test',
    'description': 'test',
    'timezone': 'Asia/Tokyo',
    'end_timezone': 'Asia/Tokyo',
    'allday': False,
    'start_only': False,
    'hidden_private': None,
    'facility_using_purpose': None,
    '_attr_1': {
}
}]

```