Files
XQuery-Allegro-API/XQuery Allegro API/API/api.xqm
2019-08-21 19:30:57 +02:00

227 lines
7.7 KiB
Plaintext

xquery version "3.1";
(:
: Module Name: XQuery Allegro API Application Library Module
:
: Module Version: 1.0
:
: Date: June 26, 2019
:
: Copyright: release11.com
:
: Properietary
: Extensions: none
:
: XQuery
: Specification: April 2019
:
: Module Overview: This module contains XQuery Allegro API functions.
:)
(:~
: This module provides basic Allegro RESTApi functions that allow
: to search offers, categories and categories id.
:
: @author Tomasz Kaleta
: @since June 26, 2019
: @version 1.0
:)
module namespace allegro="http://release11.com/xquery-allegro-api/api";
import module namespace hc="http://expath.org/ns/http-client"; (: include in docs why its expath:)
(:~
: FOR SPECIFIC DESCRIPTION OF EACH PARAMETER GO TO THIS LINK: https://developer.allegro.pl/documentation/#operation/getListing
:)
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter identifies category in which offers are searched.
:)
declare variable $allegro:categoryId as xs:string := "category.id";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter identifies phrase to search for.
:)
declare variable $allegro:phrase as xs:string := "phrase";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter limits search results to offers from one seller.
:)
declare variable $allegro:sellerId as xs:string := "seller.id";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter indicates fields to include in search.
:)
declare variable $allegro:searchMode as xs:string := "searchMode";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter sets index of first returned offer.
:)
declare variable $allegro:offset as xs:string := "offset";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter limits returned offers.
:)
declare variable $allegro:limit as xs:string := "limit";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter identifies category in which offers are searched.
:)
declare variable $allegro:sort as xs:string := "sort";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter determinates which entities will be returned in results.
:)
declare variable $allegro:include as xs:string := "include";
(:~
: This variable contains parameter name that is available in Allegro RESTApi to search offers.
: This parameter determinates what happens when no results found.
:)
declare variable $allegro:fallback as xs:string := "fallback";
(:~
: This variable contains all parameters names that are available in Allegro RESTApi to search offers.
: Variable is used to validate maps with parameters passed by user.
:)
declare variable $allegro:availableParams as xs:string* :=
("category.id",
"phrase",
"sort",
"seller.id",
"searchMode",
"offset",
"limit",
"include",
"fallback"
);
(:~
: This function gets offers from allegro based on user's criterias.
:
: @param $authorizationHeader token type and encoded access token to authorize user.
: @param $parametersMap search parameters.
: @return result of the search.
:)
declare function allegro:get-offers($authorizationHeader as xs:string,
$offersUrl as xs:string,
$parametersMap as map(*))
as item()+
{
if (allegro:validate-parameters-map($parametersMap) = 1) then
let $parameters := string-join(map:for-each($parametersMap,
function($key, $value) {string-join(($key, $value), "=")}),
"&")
let $url := concat($offersUrl, $parameters)
return
http:send-request(
<hc:request method="get" href ="{$url}">
<hc:header name="Authorization" value="{$authorizationHeader}"/>,
<hc:header name="Accept" value="application/vnd.allegro.public.v1+json"/>
</hc:request>
)
else ()
};
(:~
: This function gets categories list available in Allegro.
:
: @param $authorizationHeader token type and encoded access token to authorize user.
: @return available categories.
:)
declare function allegro:get-categories($authorizationHeader as xs:string,
$categoriesUrl as xs:string)
as item()+
{
allegro:get-category-private($authorizationHeader, $categoriesUrl, ())
};
(:~
: This function gets category params by given id.
:
: @param $authorizationHeader token type and encoded access token to authorize user.
: @param $categoryId category id.
: @return category with given id.
:)
declare function allegro:get-category-by-id($authorizationHeader as xs:string,
$categoriesUrl as xs:string,
$categoryId as xs:integer)
as item()+
{
allegro:get-category-private($authorizationHeader, $categoriesUrl, $categoryId)
};
(:~
: This function gets category parameters by given id.
:
: @param $authorizationHeader token type and encoded access token to authorize user.
: @param $categoryId category id
: @return list of parameters that are supported by the given category
:)
declare function allegro:get-category-params($authorizationHeader as xs:string,
$categoriesUrl as xs:string,
$categoryId as xs:integer)
as item()+
{
http:send-request(
<hc:request method="get" href="https://api.allegro.pl/sale/categories/{$categoryId}/parameters">
<hc:header name="Authorization" value="{$authorizationHeader}"/>,
<hc:header name="Accept" value="application/vnd.allegro.public.v1+json"/>
</hc:request>
)
};
(:~
: This is private function that builds URL to use category functions depends on what type of search is called.
:
: @param $authorizationHeader token type and encoded access token to authorize user.
: @param $categoryId category id
: @return list of categories or category with given id
:)
declare %private function allegro:get-category-private($authorizationHeader as xs:string,
$categoriesUrl as xs:string,
$categoryId as xs:integer*)
as item()+
{
let $url :=
if ($categoryId) then
concat($categoriesUrl, $categoryId)
else
$categoriesUrl
return
http:send-request(
<hc:request method="get" href="{$url}">
<hc:header name="Authorization" value="{$authorizationHeader}"/>,
<hc:header name="Accept" value="application/vnd.allegro.public.v1+json"/>
</hc:request>
)
};
(:~
: This function checks if map with parameters passed by user is valid.
: If map contains parameters that are not supported by Allegro RESTApi error is thrown.
:
: @param $parametersMap map containing search parameters.
: @return 1 if map is valid or 0 if it is not.
:)
declare %private function allegro:validate-parameters-map($parametersMap as map(*))
as xs:integer
{
let $user-keys := map:keys($parametersMap)
return
if (distinct-values($user-keys[not(.=$allegro:availableParams)])) then
error()
else
1
};