V Shopware 6 admin API client
This is a pure
V
Requires at least Shopware version 6.4.
Shopware Admin API credentials can be generated in the shopware backend (Settings->System->Integrations).
Shopware 6 Admin API Endpoint Reference
Shopware 6 Developer Documentation
I recommend to configure the api credentials via .env - take a look at my
dotenv module
Features
- built-in oauth token renewal
- useful helper functions for file upload and search
Why V
- can handle big imports that may take several hours
- parallel processing
- errors during compile time
Installation
Install and use this module as a dependency via v.mod (recommended)
Run "v init" to auto-generate your v.mod file.
v init
Then edit the dependencies in your v.mod file to look like this:
dependencies: ['thomaspeissl.shopwareac']
And install with:
v install
To update your dependencies later just run "v install" again.
Or via VPM:
v install thomaspeissl.shopwareac
Or through Git:
git clone https://github.com/thomaspeissl/v-shopware-api-client.git ~/.vmodules/thomaspeissl/shopwareac
Running the examples
Fill in your api credentials in the code placeholders an then run.
cd examples
v run simple.v
v run search.v
Example
This example gets products from the admin api and prints out their product ids.
module main
import thomaspeissl.shopwareac
import json
struct ShopResponse {
data []ShopResponseData
}
struct ShopResponseData {
id string
}
fn main() {
mut sw_api := shopwareac.Login{ // mut is needed for the automated oauth2 token renewal
api_url: 'http://localhost:8000/api/'
client_id: 'XXXXXXXXXXXXXXXXXXXXXXXXXX' // get this from Shopware 6 backend Settings->System->Integrations
client_secret: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
}
response := sw_api.get('product')
response_data := json.decode(ShopResponse, response) or { panic(err) }
for product in response_data.data {
println(product.id)
}
}
Module documentation
Contents
- date_time
- encode
- strip
-
Login
- add_media_to_product
- auth
- delete
- find_category_by_customfield
- find_media_by_name
- find_product_by_customfield
- find_product_by_productnumber
- find_property_by_name
- find_subcategory_by_name
- get
- get_default_media_folder
- get_default_payment_method
- get_default_sales_channel
- get_default_tax
- get_last_sync
- get_raw
- patch
- post
- resend_sync
- search
- sync
- sync_delete
- sync_upsert
- update_media_from_url
- upload
- upload_file
- ShopResponseData
date_time
fn date_time() string
current time formatted for Shopware date time custom fields eg. "2022-01-16T12:00:00+00:00"
encode
fn encode(s string) string
Percent-encoding reserved characters eg. for filter parameters
strip
fn strip(s string) string
strip not allowed chars
Login
struct Login {
mut:
token AuthToken
pub:
client_id string
client_secret string
pub mut:
api_url string
}
add_media_to_product
fn (mut l Login) add_media_to_product(media_id string, product_id string, set_as_cover bool, position int)
add_media_to_product position should begin with 0
auth
fn (mut l Login) auth() bool
auth get's called automatic and renews the oauth token if needed
delete
fn (mut l Login) delete(endpoint string, id string)
find_category_by_customfield
fn (mut l Login) find_category_by_customfield(field string, value string) !ShopResponseData
find_media_by_name
fn (mut l Login) find_media_by_name(name string) !ShopResponseData
find_product_by_customfield
fn (mut l Login) find_product_by_customfield(field string, value string) !ShopResponseData
find_product_by_productnumber
fn (mut l Login) find_product_by_productnumber(productnumber string) !ShopResponseData
find_property_by_name
fn (mut l Login) find_property_by_name(name string, group string) !ShopResponseData
find_subcategory_by_name
fn (mut l Login) find_subcategory_by_name(name string, parent string) !ShopResponseData
get
fn (mut l Login) get(endpoint string) string
get_default_media_folder
fn (mut l Login) get_default_media_folder() string
get_default_payment_method
fn (mut l Login) get_default_payment_method() string
get_default_sales_channel
fn (mut l Login) get_default_sales_channel() string
get_default_tax
fn (mut l Login) get_default_tax() string
get_last_sync
fn (mut l Login) get_last_sync() string
get_last_sync returns the last sync payload
get_raw
fn (mut l Login) get_raw(endpoint string) http.Response
patch
fn (mut l Login) patch(endpoint string, data string)
post
fn (mut l Login) post(endpoint string, data string) string
post returns the id of the created content on success
resend_sync
fn (mut l Login) resend_sync()
resend_sync sends the last sync operation (sync saves data into a file) again to the shop api - useful for debugging or temporary errors
search
fn (mut l Login) search(entity string, data string) string
sync
fn (mut l Login) sync(data string) !string
sync API is an add-on to the Admin API that allows you to perform multiple write operations (creating/updating and deleting) simultaneously
sync_delete
fn (mut l Login) sync_delete(entity string, data []string)
sync_delete is a shorthand function for sync with data chunking for large arrays
sync_upsert
fn (mut l Login) sync_upsert(entity string, data []string)
sync_upsert is a shorthand function for sync with data chunking for large arrays
update_media_from_url
fn (mut l Login) update_media_from_url(media_id string, url string)
Attach resource data to the media object from the given url
upload
fn (mut l Login) upload(file_url string, name string, media_folder_id string) !string
upload returns the mediaId of the uploaded file on success
upload_file
fn (mut l Login) upload_file(media_id string, name string, _ext string, data string) !
upload_file via binary blob
ShopResponseData
struct ShopResponseData {
pub:
id string
attributes Attributes
}
Powered by vdoc. Generated on: 14 Mar 2023 12:37:36
v doc -f md .