Skip to content

Commit

Permalink
logic changed: service methods w operations as str
Browse files Browse the repository at this point in the history
  • Loading branch information
EfraimGENC committed Sep 12, 2022
1 parent 549eb2b commit a11b685
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 169 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ ups = UPSService('CUSTOMER_NUMBER', 'USERNAME', 'PASSWORD')
```
#### Service Helpers <a name="ServiceHelpers"></a>
```python
result = ups.ANY_OPERATIONS(**payload)
result = ups.create(**payload) # for "Createshipment V7" service
# or
result = ups.query(**payload) # for "QueryPackageInfo" service
```
Operartions resturn list of dict as `ResultList[Result]`. ResultList and Result have some helpers like below.
- `Result(dict)`
Expand Down Expand Up @@ -71,7 +73,7 @@ Operartions resturn list of dict as `ResultList[Result]`. ResultList and Result
- GetShipmentInfoByTrackingNumber_V1
- [GetShipmentInfoByTrackingNumber_V2](#GetShipmentInfoByTrackingNumber_V2)
- GetTiNTInformationByTrackingNumberList_V1
- GetTiNTInformationByTrackingNumber_V1
- [GetTiNTInformationByTrackingNumber_V1](#GetTiNTInformationByTrackingNumber_V1)
- GetTransactionsByCustomerCode_V1
- GetTransactionsByList_V1
- [GetTransactionsByList_V2](#GetTransactionsByList_V2)
Expand Down Expand Up @@ -149,7 +151,7 @@ payload = {
'ReturnLabelImage': True
}

shipment = ups.CreateShipment_Type2(**payload)
result = ups.create('CreateShipment_Type2', **payload)
```

##### GetShipmentInfoByTrackingNumber_V2 <a name="GetShipmentInfoByTrackingNumber_V2"></a>
Expand All @@ -158,8 +160,7 @@ payload = {
'InformationLevel': 1,
'TrackingNumber': 'YOUR_TRACKING_NUMBER'
}

result = ups.GetShipmentInfoByTrackingNumber_V2(**payload)
result = ups.query('GetShipmentInfoByTrackingNumber_V2', **payload)
```

##### GetTransactionsByTrackingNumber_V1 <a name="GetTransactionsByTrackingNumber_V1"></a>
Expand All @@ -169,7 +170,7 @@ payload = {
'TrackingNumber': 'YOUR_TRACKING_NUMBER'
}

result = ups.GetTransactionsByTrackingNumber_V1(**payload)
result = ups.query('GetTransactionsByTrackingNumber_V1', **payload)
```

##### GetTransactionsByList_V2 <a name="GetTransactionsByList_V2"></a>
Expand All @@ -183,5 +184,14 @@ payload = {
'trnType': 'ALL_TRANSACTIONS'
}

result = ups.GetTransactionsByList_V2(**payload)
result = ups.query('GetTransactionsByList_V2', **payload)
```

##### GetTiNTInformationByTrackingNumber_V1 <a name="GetTiNTInformationByTrackingNumber_V1"></a>
```python
payload = {
'InformationLevel': 1,
'TrackingNumber': '1ZE3184E6800393143'
}
result = ups.query('GetTiNTInformationByTrackingNumber_V1', **payload)
```
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
with open('README.md', 'r') as f:
long_description = f.read()

version = '0.2.0'

setuptools.setup(
name="ups_turkey",
version="0.1.2",
version=version,
author="Efraim GENC",
author_email='[email protected]',
description="Easy integration for UPS Turkey",
Expand All @@ -15,7 +17,7 @@
long_description=long_description,
long_description_content_type='text/markdown',
url="https://github.com/EfraimGENC/ups-turkey",
download_url = 'https://github.com/EfraimGENC/ups-turkey/archive/refs/tags/v0.1.2.tar.gz',
download_url=f'https://github.com/EfraimGENC/ups-turkey/archive/refs/tags/v{version}.tar.gz',
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
184 changes: 24 additions & 160 deletions ups_turkey/ups.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
import uuid
import zeep
from typing import OrderedDict, Tuple
from collections.abc import Iterable
from typing import Tuple
from ups_turkey.exceptions import UPSException
from ups_turkey.helpers import to_json

Expand Down Expand Up @@ -37,22 +35,26 @@ def json(self):

class UPSService:
base_url = 'https://ws.ups.com.tr'
wsdl_create = base_url + '/wsCreateShipment/wsCreateShipment.asmx?WSDL'
wsdl_query = base_url + '/QueryPackageInfo/wsQueryPackagesInfo.asmx?WSDL'
wsdl = {
'create': f'{base_url}/wsCreateShipment/wsCreateShipment.asmx?WSDL',
'query': f'{base_url}/QueryPackageInfo/wsQueryPackagesInfo.asmx?WSDL',
}

def __init__(self, customer_number, username, password) -> None:
self.customer_number = customer_number
self.username = username
self.password = password

def call_service(self, service:str, query=False, *args, **kwargs):
if not service.startswith('Login'):
kwargs['SessionID'] = self.get_session_id(query)
def _call(self, service:str, operation:str, *args, **kwargs):
"""
service: `create` or `query`
"""
if not operation.startswith('Login'):
kwargs['SessionID'] = self.get_session_id(service)

wdsl = self.wsdl_query if query else self.wsdl_create
client = zeep.Client(wdsl)
client = zeep.Client(self.wsdl[service])

result = getattr(client.service, service)(*args, **kwargs)
result = getattr(client.service, operation)(*args, **kwargs)
result = zeep.helpers.serialize_object(result)
result = result if isinstance(result, list) else [result]

Expand All @@ -61,158 +63,20 @@ def call_service(self, service:str, query=False, *args, **kwargs):
result_list.append(Result(r))
return result_list

def login(self, query=False):
def create(self, operation:str, *args, **kwargs):
return self._call('create', operation, *args, **kwargs)

def query(self, operation:str, *args, **kwargs):
return self._call('query', operation, *args, **kwargs)

def login(self, service:str) -> Result:
credentials = self.customer_number, self.username, self.password
if query:
result = self.call_service('Login_V1', True, *credentials)[0]
else:
result = self.call_service('Login_Type1', False, *credentials)[0]
login_opertaion = {'create': 'Login_Type1', 'query': 'Login_V1'}
result = self._call(service, login_opertaion[service], *credentials)[0]
error = result.get('ErrorCode'), result.get('ErrorDefinition')
if error[0]: raise UPSException(*error)
return result

def get_session_id(self, query=False):
result = self.login(query)
def get_session_id(self, service:str):
result = self.login(service)
return uuid.UUID(result['SessionID'])

# Creation Services #######################################################

def CreateShipment_Type2(self, *args, **kwargs):
"""
Create shipment with type 2
"""
return self.call_service(
'CreateShipment_Type2', False, *args, **kwargs)

def OnDemandPickupRequest_Type1(self, *args, **kwargs):
"""
OnDemand Pickup Request Type 1
"""
return self.call_service(
'OnDemandPickupRequest_Type1', False, *args, **kwargs)

def CustomerShipmentLimitDetail(self):
"""
Customer Shipment LimitDetail
"""
return self.call_service(
'CustomerShipmentLimitDetail', False)

# Query Services ##########################################################

def GetLastTransactionByTrackingNumber_V1(self, *args, **kwargs):
"""
This method return only the last transaction for a tracking number
"""
return self.call_service(
'GetLastTransactionByTrackingNumber_V1', True, *args, **kwargs)

def GetPackageInfoByDatePeriod_V1(self, *args, **kwargs):
"""
This method is used to query all of the packages information,
under an account number, in a given date period. As SessionId
obtained through Login is specific to a customer account number,
account number is not needed as a parameter.
Parameters: `SessionID`, `InformationLevel`, `Startdate`, `EndDate`
"""
return self.call_service(
'GetPackageInfoByDatePeriod_V1', True, *args, **kwargs)

def GetPackageInfoByTrackingNumber_V1(self, *args, **kwargs):
"""
This method is used to query package information by tracking number.
Parameters: `SessionID`, `InformationLevel`, `TrackingNumber`
"""
return self.call_service(
'GetPackageInfoByTrackingNumber_V1', True, *args, **kwargs)

def GetShipmentInfoByTrackingNumber_V1(self, *args, **kwargs):
"""
This method is used to query package information for all tracking
numbers in a shipment. Any tracking number in a shipment can be sent
as a parameter.
"""
return self.call_service(
'GetShipmentInfoByTrackingNumber_V1', True, *args, **kwargs)

def GetShipmentInfoByTrackingNumber_V2(self, *args, **kwargs):
"""
This method is used to query package information for all tracking
numbers in a shipment. Any tracking number in a shipment can be sent
as a parameter.
"""
return self.call_service(
'GetShipmentInfoByTrackingNumber_V2', True, *args, **kwargs)

def GetTiNTInformationByTrackingNumberList_V1(self, *args, **kwargs):
"""
This method used to query time in transit information for a package.
Pickup date also included in dataset.
Parameters: `SessionID`, `InformationLevel`,
`TrackingNumberList[WaybillList[Waybill]]`
"""
return self.call_service(
'GetTiNTInformationByTrackingNumberList_V1', True, *args, **kwargs)

def GetTiNTInformationByTrackingNumber_V1(self, *args, **kwargs):
"""
This method used to query time in transit information for a package.
Pickup date also included in dataset.
Parameters: `SessionID`, `InformationLevel`, `TrackingNumber`
"""
return self.call_service(
'GetTiNTInformationByTrackingNumber_V1', True, *args, **kwargs)

def GetTransactionsByCustomerCode_V1(self, *args, **kwargs):
"""
Parameters: `SessionID`, `InformationLevel`, `RecordId`
"""
return self.call_service(
'GetTransactionsByCustomerCode_V1', True, *args, **kwargs)

def GetTransactionsByList_V1(self, *args, **kwargs):
"""
This method returns requested transactions by provided list.
List can be customer referance number and tracking number.
Referance type must be set. Results can be set as last transaction,
all transaction and delivery transaction.
"""
return self.call_service(
'GetTransactionsByList_V1', True, *args, **kwargs)

def GetTransactionsByList_V2(self, *args, **kwargs):
"""
This method returns requested transactions by provided list.
List can be customer referance number and tracking number.
Referance type must be set. Results can be set as last transaction,
all transaction and delivery transaction.
"""
return self.call_service(
'GetTransactionsByList_V2', True, *args, **kwargs)

def GetTransactionsByPackagePickupDate_V1(self, *args, **kwargs):
"""
This method is used to query all of the packages transactions,
under an account number, in a given date period. Results can be set
as last transaction, all transaction and delivery transaction.
Parameters: `SessionID`, `InformationLevel`, `StartDate`, `EndDate`,
`TransactionType`
"""
return self.call_service(
'GetTransactionsByPackagePickupDate_V1', True, *args, **kwargs)

def GetTransactionsByTrackingNumber_V1(self, *args, **kwargs):
"""
This method return only the last transaction for a tracking number
"""
return self.call_service(
'GetTransactionsByTrackingNumber_V1', True, *args, **kwargs)

def GetUnreadTransactionsByTrackingNumber_V1(self, *args, **kwargs):
"""
This method return all transactions with RecordIds greater than the
RecordId supplied as a parameter. RecordIds are returned as part of
transaction information.
"""
return self.call_service(
'GetUnreadTransactionsByTrackingNumber_V1', True, *args, **kwargs)

0 comments on commit a11b685

Please sign in to comment.