After setting up Python3 and a Python IDE import the python script below
Extract various positions
The script is ready to extract 3 different entity types and the corresponding sections need to be commented out/in (other entities can be added to the script):
InventoryPositions: lines 10-29 & 83-84
VariantProducts: lines 31-51 & 85-86
VirtualPositions: lines 54-75 & 87-88
result
After executing successfully a csv file is produced, which will follow the filename pattern `yyyymmdd_{{acccountId}}_{{entityType}}.cs` e.g. `20191122_FLUENT_virtualPositions.csv`
Bulk extract on Store Location Hours
another example code to extract Store Location Hours:
1import json
2import csv
3import requests
4from graphqlclient import GraphQLClient
5from http.client import IncompleteRead
6from datetime import datetime
78# ######## CONFIGURATION SECTION START
9page_query = '''
10query extractInventoryPositions($after:String, $first:Int) {11 inventoryPositions(after:$after, first:$first){12 edges {13 node {14 ref
15 status
16 productRef
17 locationRef
18 onHand
19 updatedOn
20 createdOn
21}22 cursor
23}24 pageInfo {25 hasNextPage
26}27}28}29'''
3031# page_query = '''
32# query extractVariantProducts($after:String, $first:Int) {33# variantProducts (after:$after, first:$first){34# edges {35# node {36# ref
37# status
38# type
39# name
40# gtin
41# summary
42# updatedOn
43# createdOn
44# }45# cursor
46# }47# pageInfo {48# hasNextPage
49# }50# }51# }52# '''
5354# page_query = '''
55# query extractVirtualPositions($after: String, $first: Int) {56# virtualPositions(after: $after, first: $first) {57# edges {58# node {59# ref
60# type
61# status
62# quantity
63# productRef
64# groupRef
65# updatedOn
66# createdOn
67# }68# cursor
69# }70# pageInfo {71# hasNextPage
72# }73# }74# }75# '''
76accountId = '{{accountId}}'
77oauth_url = '{{fluentApiHost}}/oauth/token?username={{retailerUsername}}&password={{retailerPassword}}&client_id={{accountId}}&client_secret={{clientSecret}}&grant_type=password&scope=api'
7879# needs to correspond to the GQL query fields.
80entity = "inventoryPositions"81fields = ["ref","status","productRef","locationRef","onHand","updatedOn","createdOn"]82# entity = "variantProducts"83# fields = ["ref","status","type","name","gtin","summary","updatedOn","createdOn"]84# entity = "virtualPositions"85# fields = ["ref","type","status","quantity","productRef","groupRef","updatedOn","createdOn"]86# ######## CONFIGURATION SECTION END
8788graphql_url = 'https://' + accountId + '.api.fluentretail.com/graphql'89filename = datetime.now().strftime("%Y%m%d") + '_' + accountId + '_' + entity + '.csv'
909192def get_page_of_data(client, after, first):93 res = client.execute(page_query,{'after': after, 'first': first})
94 data = json.loads(res)['data']95 return data
969798def get_token():99 auth_token_response = requests.post(oauth_url)
100 if auth_token_response.status_code == 200:101 print(auth_token_response.json())
102 else:103 print("Couldn't get auth token {}".format(auth_token_response.status_code))
104 access_token = 'Bearer ' + auth_token_response.json()['access_token']105 print("Access token: {}".format(access_token))
106 return access_token
107108109def init_client():110 client = GraphQLClient(graphql_url)
111 client.inject_token(get_token())
112 return client
113114115def get_all_data(client=init_client(), all_labels=[], cursor=None, first=5000, retries_left=5):116 global data
117 try:118 data = get_page_of_data(client, cursor, first)
119 except IncompleteRead:120 print('reconnect and keep tracking')
121 if retries_left > 0:122 retries_left = retries_left - 1123 client = init_client()
124 get_all_data(client, all_labels, cursor, retries_left)
125 else:126 print('retries exhausted')
127 return all_labels
128 except:129 print("error occurred for cursor: {} retrying with retries left:{}".format(cursor, retries_left))
130 if retries_left > 0:131 retries_left = retries_left - 1132 get_all_data(client, all_labels, cursor, retries_left)
133 else:134 print('retries exhausted')
135 return all_labels
136 if data is not None:137 new_labels = [] or data[entity]['edges']138 has_next_page = data[entity]['pageInfo']['hasNextPage']139 all_labels = all_labels + new_labels
140 if has_next_page:141 if len(new_labels) > 0:142 cursor = get_cursor(new_labels)
143 all_labels = get_all_data(client, all_labels, cursor)
144 return all_labels
145146147def get_cursor(new_labels):148 if len(new_labels) > 0:149 last = len(new_labels) - 1150 while True:151 if 'cursor' in new_labels[last]:152 return new_labels[last]['cursor']153 else:154 last -= 1155 if 'cursor' in new_labels[last]:156 return new_labels[last]['cursor']157 else:158 pass
159160161def write_to_csv(all_labels):162 file = csv.writer(open(filename,"wt"), delimiter=',')
163 file.writerow(fields)
164 for data in all_labels:165 if len(data.keys()) > 0:166 row = []167 for field in fields:168 row.append(data['node'][field])
169 file.writerow(row)
170171172result = get_all_data()
173write_to_csv(result)
1import json
2import csv
3import requests
4from graphqlclient import GraphQLClient
5from http.client import IncompleteRead
6from datetime import datetime
78# ######## CONFIGURATION SECTION START910page_query ='''
11 query extractLocations($after:String, $first:Int) {
12 locations (after:$after, first:$first){
13 edges {
14 node {
15 rc_location_id:id
16 rc_location_ref:ref
17 rc_location_name:name
18 primaryAddress{
19 addr_name:name
20 addr_city:city
21 addr_postcode:postcode
22 addr_state:state
23 }
24 openingSchedule{
25 rc_schedule_id:id
26 rc_allHours:allHours
27 rc_monStart:monStart
28 rc_monEnd:monEnd
29 rc_tueStart:tueStart
30 rc_tueEnd:tueEnd
31 rc_wedStart:wedStart
32 rc_wedEnd:wedEnd
33 rc_thuStart:thuStart
34 rc_thuEnd:thuEnd
35 rc_friStart:friStart
36 rc_friEnd:friEnd
37 rc_satStart:satStart
38 rc_satEnd:satEnd
39 rc_sunStart:sunStart
40 rc_sunEnd:sunEnd
41 }
42 }
43 cursor
44 }
45 pageInfo {
46 hasNextPage
47 }
48 }
49}
50'''5152accountId ='{{accountId}}'53oauth_url ='{{fluentApiHost}}/oauth/token?username={{retailerUsername}}&password={{retailerPassword}}&client_id={{accountId}}&client_secret={{clientSecret}}&grant_type=password&scope=api'5455# needs to correspond to the GQL query fields.56entity ="locations"57fields =["rc_location_id","rc_location_ref","rc_location_name",58"addr_name","addr_city","addr_postcode","addr_state",59"rc_schedule_id","rc_allHours",60"rc_monStart","rc_monEnd","rc_tueStart","rc_tueEnd","rc_wedStart","rc_wedEnd",61"rc_thuStart","rc_thuEnd","rc_friStart","rc_friEnd","rc_satStart","rc_satEnd",62"rc_sunStart","rc_sunEnd"]63# ######## CONFIGURATION SECTION END6465graphql_url ='https://'+ accountId +'.api.fluentretail.com/graphql'66filename = datetime.now().strftime("%Y%m%d")+'_'+ accountId +'_'+ entity +'.csv'676869defget_page_of_data(client, after, first):70 res = client.execute(page_query,{'after': after,'first': first})71 data = json.loads(res)['data']72return data
737475defget_token():76 auth_token_response = requests.post(oauth_url)77if auth_token_response.status_code ==200:78print(auth_token_response.json())79else:80print("Couldn't get auth token {}".format(auth_token_response.status_code))81 access_token ='Bearer '+ auth_token_response.json()['access_token']82print("Access token: {}".format(access_token))83return access_token
848586definit_client():87 client = GraphQLClient(graphql_url)88 client.inject_token(get_token())89return client
909192defget_all_data(client=init_client(), all_labels=[], cursor=None, first=5000, retries_left=5):93global data
94try:95 data = get_page_of_data(client, cursor, first)96except IncompleteRead:97print('reconnect and keep tracking')98if retries_left >0:99 retries_left = retries_left -1100 client = init_client()101 get_all_data(client, all_labels, cursor, retries_left)102else:103print('retries exhausted')104return all_labels
105except:106print("error occurred for cursor: {} retrying with retries left:{}".format(cursor, retries_left))107if retries_left >0:108 retries_left = retries_left -1109 get_all_data(client, all_labels, cursor, retries_left)110else:111print('retries exhausted')112return all_labels
113if data isnotNone:114 new_labels =[]or data[entity]['edges']115 has_next_page = data[entity]['pageInfo']['hasNextPage']116 all_labels = all_labels + new_labels
117if has_next_page:118iflen(new_labels)>0:119 cursor = get_cursor(new_labels)120 all_labels = get_all_data(client, all_labels, cursor)121return all_labels
122123124defget_cursor(new_labels):125iflen(new_labels)>0:126 last =len(new_labels)-1127whileTrue:128if'cursor'in new_labels[last]:129return new_labels[last]['cursor']130else:131 last -=1132if'cursor'in new_labels[last]:133return new_labels[last]['cursor']134else:135pass136137138defwrite_to_csv(all_labels):139file= csv.writer(open(filename,"wt"), delimiter=',')140file.writerow(fields)141for data in all_labels:142iflen(data.keys())>0:143 row =[]144for field in fields:145print("fields: {}".format(field))146if field =="rc_schedule_id":147 row.append(data['node']['openingSchedule']['rc_schedule_id'])148print("fields rc_schedule_id: {}".format(data['node']['openingSchedule']['rc_schedule_id']))149elif field =="rc_allHours":150 row.append(data['node']['openingSchedule']['rc_allHours'])151print("fields rc_allHours: {}".format(data['node']['openingSchedule']['rc_allHours']))152elif field =="rc_monStart":153 row.append(data['node']['openingSchedule']['rc_monStart'])154print("fields rc_monStart: {}".format(data['node']['openingSchedule']['rc_monStart']))155elif field =="rc_monEnd":156 row.append(data['node']['openingSchedule']['rc_monEnd'])157print("fields rc_monEnd: {}".format(data['node']['openingSchedule']['rc_monEnd']))158elif field =="rc_tueStart":159 row.append(data['node']['openingSchedule']['rc_tueStart'])160print("fields rc_tueStart: {}".format(data['node']['openingSchedule']['rc_tueStart']))161elif field =="rc_tueEnd":162 row.append(data['node']['openingSchedule']['rc_tueEnd'])163print("fields rc_tueEnd: {}".format(data['node']['openingSchedule']['rc_tueEnd']))164elif field =="rc_wedStart":165 row.append(data['node']['openingSchedule']['rc_wedStart'])166print("fields rc_wedStart: {}".format(data['node']['openingSchedule']['rc_wedStart']))167elif field =="rc_wedEnd":168 row.append(data['node']['openingSchedule']['rc_wedEnd'])169print("fields rc_wedEnd: {}".format(data['node']['openingSchedule']['rc_wedEnd']))170elif field =="rc_thuStart":171 row.append(data['node']['openingSchedule']['rc_thuStart'])172print("fields rc_thuStart: {}".format(data['node']['openingSchedule']['rc_thuStart']))173elif field =="rc_thuEnd":174 row.append(data['node']['openingSchedule']['rc_thuEnd'])175print("fields rc_thuEnd: {}".format(data['node']['openingSchedule']['rc_thuEnd']))176elif field =="rc_friStart":177 row.append(data['node']['openingSchedule']['rc_friStart'])178print("fields rc_friStart: {}".format(data['node']['openingSchedule']['rc_friStart']))179elif field =="rc_friEnd":180 row.append(data['node']['openingSchedule']['rc_friEnd'])181print("fields rc_friEnd: {}".format(data['node']['openingSchedule']['rc_friEnd']))182elif field =="rc_satStart":183 row.append(data['node']['openingSchedule']['rc_satStart'])184print("fields rc_satStart: {}".format(data['node']['openingSchedule']['rc_satStart']))185elif field =="rc_satEnd":186 row.append(data['node']['openingSchedule']['rc_satEnd'])187print("fields rc_satEnd: {}".format(data['node']['openingSchedule']['rc_satEnd']))188elif field =="rc_sunStart":189 row.append(data['node']['openingSchedule']['rc_sunStart'])190print("fields sunStart: {}".format(data['node']['openingSchedule']['rc_sunStart']))191elif field =="rc_sunEnd":192 row.append(data['node']['openingSchedule']['rc_sunEnd'])193print("fields rc_sunEnd: {}".format(data['node']['openingSchedule']['rc_sunEnd']))194elif field =="addr_name":195#row.append(data['node']['primaryAddress']['addr_name'])196 row.append(str(data['node']['primaryAddress']['addr_name']).replace(","," "))197print("fields addr_name: {}".format(data['node']['primaryAddress']['addr_name']))198elif field =="addr_city":199 row.append(str(data['node']['primaryAddress']['addr_city']).replace(","," "))200print("fields addr_city: {}".format(data['node']['primaryAddress']['addr_city']))201elif field =="addr_postcode":202 row.append(data['node']['primaryAddress']['addr_postcode'])203print("fields addr_postcode: {}".format(data['node']['primaryAddress']['addr_postcode']))204elif field =="addr_state":205 row.append(data['node']['primaryAddress']['addr_state'])206print("fields addr_state: {}".format(data['node']['primaryAddress']['addr_state']))207else:208 row.append(data['node'][field])209file.writerow(row)210print("fields: {}".format(data))211212result = get_all_data()213write_to_csv(result)