GeoService.ts 2.12 KB
Newer Older
1 2 3 4 5 6
import * as QueryString from 'query-string';
import * as UrlTemplate from 'url-template';

import {axiosBackend} from 'utilities/requestUtils';

import VocabularyTerm from 'model/vocabulary/VocabularyTerm';
Maxym Borodenko's avatar
Maxym Borodenko committed
7
import Country from 'model/geo/Country';
8

Maxym Borodenko's avatar
Maxym Borodenko committed
9 10
const URL_AUTOCOMPLETE_GEO_TERM = `/api/v1/geo/iso3166/autocomplete`;
const URL_GET = UrlTemplate.parse(`/api/v1/geo/iso3166/{code}`);
Maxym Borodenko's avatar
Maxym Borodenko committed
11
const URL_GET_COUNTRY = UrlTemplate.parse(`/api/v1/geo/country/{iso3code}`);
12 13 14 15 16 17 18

/*
* Defined in Swagger as 'geo'
*/
class GeoService {

    /**
Maxym Borodenko's avatar
Maxym Borodenko committed
19
     * autocompleteGeoTerm at /api/v1/geo/iso3166/autocomplete
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
     *
     * @param c code
     */
    public static autocompleteGeoTerm(c: string): Promise<VocabularyTerm[]> {

        const qs = QueryString.stringify({
            c: c || undefined,
        }, {});
        const apiUrl = URL_AUTOCOMPLETE_GEO_TERM + (qs ? `?${qs}` : '');
        // console.log(`Fetching from ${apiUrl}`);
        const content = {/* No content in request body */};

        return axiosBackend.request({
            url: apiUrl,
            method: 'GET',
            ...content,
        }).then(({data}) => data as VocabularyTerm[]);
    }

    /**
Maxym Borodenko's avatar
Maxym Borodenko committed
40
     * get at /api/v1/geo/iso3166/{code}
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
     *
     * @param code code
     */
    public static get(code?: string): Promise<any> {

        const apiUrl = URL_GET.expand({code});
        // console.log(`Fetching from ${apiUrl}`);
        const content = { /* No content in request body */ };

        return axiosBackend.request({
            url: apiUrl,
            method: 'GET',
            ...content,
        }).then(({ data }) => data as any);
    }

Maxym Borodenko's avatar
Maxym Borodenko committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    /**
     * getCountry at /api/v1/geo/country/{iso3code}
     *
     * @param iso3code iso3code
     */
    public static getCountry(iso3code: string): Promise<Country> {

        const apiUrl = URL_GET_COUNTRY.expand({iso3code});
        // console.log(`Fetching from ${apiUrl}`);
        const content = { /* No content in request body */ };

        return axiosBackend.request({
            url: apiUrl,
            method: 'GET',
            ...content,
        }).then(({ data }) => data as Country);
    }

75 76 77
}

export default GeoService;