loading

This Plugin Crashed!

Error: Error: must not create an existing file {"type":"CREATE_FILE","wid":"0.4751956379782143","path":"main.py","file":{"path":"main.py","content":{"asEncoding":{"base64":"IyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMKCiMgRnVuY3Rpb246IFB5dGhvbiBQcm9ncmFtIGZvciBDcmVhdGluZyBCdXNpbmVzcyBPYmplY3RzIGluIFNBUCBCeSBEZXNpZ24KIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjCiMgR2xvYmFsIGltcG9ydHMKaW1wb3J0IHN5cwpmcm9tIGRhdGV0aW1lIGltcG9ydCBkYXRldGltZQppbXBvcnQgcmVxdWVzdHMKaW1wb3J0IGxvZ2dpbmcuY29uZmlnCgojIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMKIyBDcmVkZW50aWFscyBmb3IgQXV0aGVudGljYXRpb24KIyBTQVAgQ3VzdG9tIE9EYXRhIFNlcnZpY2VzIGRvIG5vdCBzdXBwb3J0IHRlY2huaWNhbCBVc2VyIFBhcnRzJk1vcmUKCnN5c3RlbSA9ICJodHRwczovL215MzIwMDY0LnNhcGJ5ZGVzaWduLmNvbSIKdGFyZ2V0X3NlcnZpY2UgPSBzeXN0ZW0gKyAiL3NhcC9ieWQvb2RhdGEvY3VzdC92MS9vcHBvcnR1bml0eSIKdXNlcm5hbWUgPSAiQjRCX0NIQUxMRU5HRSIKcGFzc3dvcmQgPSAiV2VsY29tZTk5IgoKCnBlcmZvcm1fbG9nZ2luZyA9IDAKCmlmIHBlcmZvcm1fbG9nZ2luZzoKICAgIGxvZ2dpbmcuY29uZmlnLmRpY3RDb25maWcoewogICAgICAgICd2ZXJzaW9uJzogMSwKICAgICAgICAnZm9ybWF0dGVycyc6IHsKICAgICAgICAgICAgJ3ZlcmJvc2UnOiB7CiAgICAgICAgICAgICAgICAnZm9ybWF0JzogJyUobmFtZSlzOiAlKG1lc3NhZ2UpcycKICAgICAgICAgICAgfQogICAgICAgIH0sCiAgICAgICAgJ2hhbmRsZXJzJzogewogICAgICAgICAgICAnY29uc29sZSc6IHsKICAgICAgICAgICAgICAgICdsZXZlbCc6ICdERUJVRycsCiAgICAgICAgICAgICAgICAnY2xhc3MnOiAnbG9nZ2luZy5TdHJlYW1IYW5kbGVyJywKICAgICAgICAgICAgICAgICdmb3JtYXR0ZXInOiAndmVyYm9zZScsCiAgICAgICAgICAgIH0sCiAgICAgICAgfSwKICAgICAgICAnbG9nZ2Vycyc6IHsKICAgICAgICAgICAgJ3plZXAudHJhbnNwb3J0cyc6IHsKICAgICAgICAgICAgICAgICdsZXZlbCc6ICdERUJVRycsCiAgICAgICAgICAgICAgICAncHJvcGFnYXRlJzogVHJ1ZSwKICAgICAgICAgICAgICAgICdoYW5kbGVycyc6IFsnY29uc29sZSddLAogICAgICAgICAgICB9LAogICAgICAgIH0KICAgIH0pCgojIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMKIyBGZXRjaCBYQ3NyZlRva2VuIGZvciBQT1NUIFJlcXVlc3QKZGVmIGdldFhDc3JmVG9rZW4oaGVhZGVyKToKCiAgICByID0gcmVxdWVzdHMuZ2V0KHRhcmdldF9zZXJ2aWNlICsgIi8kbWV0YWRhdGEiLCBhdXRoPSh1c2VybmFtZSwgcGFzc3dvcmQpLCBoZWFkZXJzPWhlYWRlcikKICAgIHJldHVybiByCgojIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMKIyBQcmVwYXJlIE9wcG9ydHVuaXR5IEl0ZW1zCmRlZiBwYWNrYWdlSXRlbXMoaXRlbXNfZGljdCk6CiAgICBpdGVtcyA9IFtdCiAgICBmb3Iga2V5LCB2YWx1ZSBpbiBpdGVtc19kaWN0Lml0ZW1zKCk6CiAgICAgICAgaXRlbXMuYXBwZW5kKAogICAgICAgICAgICB7CiAgICAgICAgICAgICA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gICAgICBqc29uPWRhdGEsIGF1dGg9KHVzZXJuYW1lLCBwYXNzd29yZCksCiAgICAgICAgaGVhZGVycz1oZWFkZXIsIGNvb2tpZXM9Y29va2llcwogICAgKQogICAgcmV0dXJuIHIKCiMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIwojIFBhcnNpbmcgSW5wdXQgRmlsZQpkZWYgcmVhZEluRmlsZShhRmlsZSk6CiAgICBmaXJzdExpbmUgPSBUcnVlCgogICAgaGVhZGVyX2RpY3QgPSB7fQogICAgaXRlbXNfZGljdCA9IHt9CiAgICBpdGVtX2tleSA9IDEwCiAgICB0cnk6CiAgICAgICAgd2l0aCBvcGVuKGFGaWxlICxtb2RlPSdyJyAsZW5jb2Rpbmc9InV0Zi04IikgYXMgaW5maWxlOgogICAgICAgICAgICBmb3IgbGluZSBpbiBpbmZpbGU6CiAgICAgICAgICAgICAgICBhbl9hcnIgPSBsaW5lLnN0cmlwKCkuc3BsaXQoJ1x0JykKICAgICAgICAgICAgICAgICMgVGhlIFByb3BlcnRpZXMgaW4gb3Bwb3J0dW5pdHkudHh0IGFyZSBzZXBlcmF0ZWQgYnkgInRhYnVsYXRvciIKICAgICAgICAgICAgICAgIGlmIGZpcnN0TGluZToKICAgICAgICAgICAgICAgICAgICBpZiBsZW4oYW5fYXJyKSA+IDE6CiAgICAgICAgICAgICAgICAgICAgICAgICNwcmludChhbl9hcnJbMF0pCiAgICAgICAgICAgICAgICAgICAgICAgIGhlYWRlcl9kaWN0WyJLTlIiXSA9IGFuX2FyclswXSAgICAgICAgICAjS3VuZGVubnVtbWVyCiAgICAgICAgICAgICAgICAgICAgICAgIGhlYWRlcl9kaWN0WyJEQVRVTSJdID0gYW5fYXJyWzFdICAgICAgICAjRGF0dW0gZGVyIE9wcG9ydHVuaXR5CiAgICAgICAgICAgICAgICAgICAgICAgIGhlYWRlcl9kaWN0WyJNUlBIIl0gPSBhbl9hcnJbMl0gICAgICAgICAjQmVzY2hyZWlidW5nIGRlciBPcHBvcnR1bml0eQogICAgICAgICAgICAgICAgICAgICAgICBoZWFkZXJfZGljdFsiTVJQSDIiXSA9IGFuX2Fycls0XSAgICAgICAgI0dld2lubndhaHJzY2hlaW5saWNoa2VpdAoKICAgICAgICAgICAgICAgICAgICBmaXJzdExpbmUgPSBGYWxzZQogICAgICAgICAgICAgICAgZWxzZToKICAgICAgICAgICAgICAgICAgICBpZiBsaW5lLnN0cmlwKCkgIT0gIiI6CiAgICAgICAgICAgICAgICAgICAgICAgIGl0ZW1fYXJyID0gW3N0cihpdGVtX2tleSldCiAgICAgICAgICAgICAgICAgICAgICAgIGZvciB2YWx1ZSBpbiBhbl9hcnI6CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBpdGVtX2Fyci5hcHBlbmQodmFsdWUpCiAgICAgICAgICAgICAgICAgICAgICAgIGl0ZW1zX2RpY3Rbc3RyKGl0ZW1fa2V5KV0gPSBpdGVtX2FycgogICAgICAgICAgICAgICAgICAgICAgICBpdGVtX2tleSArPSAxMAogICAgZXhjZXB0OgogICAgICAgIHByaW50KCJFcnJvciB3aGlsZSByZWFkaW5nIGlucHV0ZmlsZSIgLGZpbGU9c3lzLnN0ZGVycikKICAgICAgICBzeXMuZXhpdCgxKQogICAgcmV0dXJuIFtoZWFkZXJfZGljdCAsaXRlbXNfZGljdF0KCiMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIwojIG1haW4gZXhlY3V0aW9uIG1ldGhvZAoKZGVmIHByb2Nlc3NEYXRhKGlucHV0QXJyKToKCiAgICAjIyBGaXJzdCBIVFRQIFJlcXVlc3QKICAgIGhlYWRlciA9IHsKICAgICAgICAieC1jc3JmLXRva2VuIjogIkZldGNoIiwKICAgICAgICAiQ29udGVudC1UeXBlIjogImFwcGxpY2F0aW9uL2pzb24iCiAgICB9CiAgICBodHRwX3Jlc3BvbnNlID0gZ2V0WENzcmZUb2tlbihoZWFkZXIpCiAgICBwcmludERlYnVnKGh0dHBfcmVzcG9uc2UpCgogICA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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
################################

# Function: Python Program for Creating Business Objects in SAP By Design
######################################################
# Global imports
import sys
from datetime import datetime
import requests
import logging.config

######################################################
# Credentials for Authentication
# SAP Custom OData Services do not support technical User Parts&More

system = "https://my320064.sapbydesign.com"
target_service = system + "/sap/byd/odata/cust/v1/opportunity"
username = "B4B_CHALLENGE"
password = "Welcome99"


perform_logging = 0

if perform_logging:
    logging.config.dictConfig({
        'version': 1,
        'formatters': {
            'verbose': {
                'format': '%(name)s: %(message)s'
            }
        },
        'handlers': {
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',
                'formatter': 'verbose',
            },
        },
        'loggers': {
            'zeep.transports': {
                'level': 'DEBUG',
                'propagate': True,
                'handlers': ['console'],
            },
        }
    })

###########################
# Fetch XCsrfToken for POST Request
def getXCsrfToken(header):

    r = requests.get(target_service + "/$metadata", auth=(username, password), headers=header)
    return r

###########################
# Prepare Opportunity Items
def packageItems(items_dict):
    items = []
    for key, value in items_dict.items():
        items.append(
            {
                "ProductID": value[1],
                "Quantity": value[2]
            })
    return items

###########################
# Prepare POST Content
def preparePOSTContent(items, header_dict):
    opp = {'Name': header_dict["MRPH"],
           'ProspectPartyID': str(header_dict['KNR']),
           'ExpectedRevenueAmount': '1',
           'EndDate' : convertDate(header_dict['DATUM']),
           'StartDate': str(datetime.now())[0:10] + "T00:00:00",
           'currencyCode': 'EUR',
           'ProbabilityPercent': getProbability(header_dict["MRPH2"]),
           'OpportunityItem': items
           }
    return opp

###########################
# Get Probability in percent
def getProbability(percent):
    try:
        if int(percent) <= 100 and int(percent) >= 0:
            return str(percent)
    except Exception as ex:
        return str(100)

###########################
# Prepare Update Content
def preparePUTContent(param):
    opp = {
        "ExpectedRevenueAmount" : ""
    }
    return opp

###########################
# Format Date String
def convertDate(date):
    return date[0:4] + "-" + date[4:6] + "-" + date[6:8] + "T00:00:00"

###########################
# Find XML Tag
def parseXMLTag(tag, response):
    start = str(response.text).find(tag) + len(tag)
    tag = tag[0:1] + "/" + tag[1:len(tag)]
    end = str(response.text).find(tag)
    return str(response.text)[start: end]

###########################
# PrintXMLResponse
def printDebug(response):
    if response.status_code == 200 or response.status_code == 201 or response.status_code == 204:
        print("Request successful with status code " + str(response.status_code))
    else:
        print("Request seemed to have failed with status code " + str(response.status_code))
        exit(1)

###########################
# send POST
def postOpportunity(data, header, cookies):
    r = requests.post(
        target_service + "/OpportunityCollection",
        json=data, auth=(username, password),
        headers=header, cookies=cookies
    )
    return r

###########################
# send GET
def getOpportunity(oppID, header, cookies):
    r = requests.get(
        target_service + "/OpportunityCollection('" + oppID + "')", auth=(username, password),
        headers=header, cookies=cookies
    )
    return r

###########################
# send MERGE
def mergeOpportunity(oppID, data, header, cookies):
    r = requests.post(
        target_service + "/OpportunityCollection('" + oppID + "')",
        json=data, auth=(username, password),
        headers=header, cookies=cookies
    )
    return r

###########################
# Parsing Input File
def readInFile(aFile):
    firstLine = True

    header_dict = {}
    items_dict = {}
    item_key = 10
    try:
        with open(aFile ,mode='r' ,encoding="utf-8") as infile:
            for line in infile:
                an_arr = line.strip().split('\t')
                # The Properties in opportunity.txt are seperated by "tabulator"
                if firstLine:
                    if len(an_arr) > 1:
                        #print(an_arr[0])
                        header_dict["KNR"] = an_arr[0]          #Kundennummer
                        header_dict["DATUM"] = an_arr[1]        #Datum der Opportunity
                        header_dict["MRPH"] = an_arr[2]         #Beschreibung der Opportunity
                        header_dict["MRPH2"] = an_arr[4]        #Gewinnwahrscheinlichkeit

                    firstLine = False
                else:
                    if line.strip() != "":
                        item_arr = [str(item_key)]
                        for value in an_arr:
                            item_arr.append(value)
                        items_dict[str(item_key)] = item_arr
                        item_key += 10
    except:
        print("Error while reading inputfile" ,file=sys.stderr)
        sys.exit(1)
    return [header_dict ,items_dict]

###########################
# main execution method

def processData(inputArr):

    ## First HTTP Request
    header = {
        "x-csrf-token": "Fetch",
        "Content-Type": "application/json"
    }
    http_response = getXCsrfToken(header)
    printDebug(http_response)

    ## Second HTTP Request
    csrf_token = http_response.headers.get("x-csrf-token")
    cookies = http_response.cookies
    header = {
        "x-csrf-token": "",
        "Content-Type": "application/json"
    }

    items_dict = inputArr[1]
    items = packageItems(items_dict)
    header_dict = inputArr[0]

    opp = preparePOSTContent(items, header_dict)
    http_response = postOpportunity(opp, header, cookies)
    printDebug(http_response)

    ## Third HTTP Request
    opp_id = parseXMLTag("<d:ObjectID>", http_response)
    http_response = getOpportunity(opp_id, header, cookies)
    printDebug(http_response)

    ## Fourth HTTP Request
    header = {
        "x-csrf-token": csrf_token,
        "Content-Type": "application/json",
        "x-http-method" : "MERGE"
    }
    exp_net_amount = parseXMLTag("<d:TotalExpectedNetAmount>", http_response)
    data = preparePUTContent(exp_net_amount)

    http_response = mergeOpportunity(opp_id, data, header, cookies)
    printDebug(http_response)

    return opp_id

###########################
# Main
# Your goal is to debug the errors within this files
# There are 4 errors in the entire script
# To get started: The first TODO is very simple: Declare the filename to read in your data

parsedFile = readInFile(filename)

retID = processData(parsedFile)

print("Created new SAP ByD Object with ID: " + str(retID))
sys.exit(0)

################################
###################################################################################