DadataSuggestions
public class DadataSuggestions
DadataSuggestions performs all the interactions with Dadata API.
-
New instance of DadataSuggestions.
Required API key is read from Info.plist. Each init creates new instance using same token. If DadataSuggestions is used havily consider
DadataSuggestions.shared()
instead.Precondition
Token set with “IIDadataAPIToken” key in Info.plist.Throws
Call may throw if there isn’t a value for key “IIDadataAPIToken” set in Info.plist.Declaration
Swift
public convenience init() throws
-
This init checks connectivity once the class instance is set.
This init should not be called on main thread as it may take up long time as it makes request to server in a blocking manner. Throws if connection is impossible or request is timed out.
DispatchQueue.global(qos: .background).async { let dadata = try DadataSuggestions(apiKey: "<# Dadata API token #>", checkWithTimeout: 15) }
Throws
May throw on connectivity problems, missing or wrong API token, limits exeeded, wrong endpoint. May throw if request is timed out.
Declaration
Swift
public convenience init(apiKey: String, checkWithTimeout timeout: Int) throws
Parameters
apiKey
Dadata API token. Check it in account settings at dadata.ru.
checkWithTimeout
Time in seconds to wait for response.
-
New instance of DadataSuggestions.
Declaration
Swift
public convenience required init(apiKey: String)
Parameters
apiKey
Dadata API token. Check it in account settings at dadata.ru.
-
Get shared instance of DadataSuggestions class.
Call may throw if neither apiKey parameter is provided nor a value for key “IIDadataAPIToken” is set in Info.plist whenever shared instance weren’t instantiated earlier. If another apiKey provided new shared instance of DadataSuggestions recreated with the provided API token.
Declaration
Swift
public static func shared(apiKey: String? = nil) throws -> DadataSuggestions
Parameters
apiKey
Dadata API token. Check it in account settings at dadata.ru.
-
Basic address suggestions request with only rquired data.
Declaration
Swift
public func suggestAddress(_ query: String, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>) -> Void)
Parameters
query
Query string to send to API. String of a free-form e.g. address part.
completion
Result handler.
result
result of address suggestion query.
-
suggestAddress(_:
queryType: resultsCount: language: constraints: regionPriority: upperScaleLimit: lowerScaleLimit: trimRegionResult: completion: ) Address suggestions request.
Limitations, filters and constraints may be applied to query.
Declaration
Swift
public func suggestAddress(_ query: String, queryType: AddressQueryType = .address, resultsCount: Int? = 10, language: QueryResultLanguage? = nil, constraints: [AddressQueryConstraint]? = nil, regionPriority: [RegionPriority]? = nil, upperScaleLimit: ScaleLevel? = nil, lowerScaleLimit: ScaleLevel? = nil, trimRegionResult: Bool = false, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>)->Void)
Parameters
query
Query string to send to API. String of a free-form e.g. address part.
queryType
Lets select whether the request type. There are 3 query types available:
address
— standart address suggestion query;fiasOnly
— query to only search in FIAS database: less matches, state provided address data only;findByID
— takes KLADR or FIAS ID as a query parameter to lookup additional data.resultsCount
How many suggestions to return.
1
provides more data on a single object including latitude and longitude.20
is a maximum value.language
Suggested results may be in Russian or English.
constraints
List of
AddressQueryConstraint
objects to filter results.regionPriority
List of RegionPriority objects to prefer in lookup.
upperScaleLimit
Bigger
ScaleLevel
object in pair of scale limits.lowerScaleLimit
Smaller
ScaleLevel
object in pair of scale limits.trimRegionResult
Remove region and city names from suggestion top level.
completion
Result handler.
result
result of address suggestion query.
-
suggestAddress(_:
queryType: resultsCount: language: constraints: regionPriority: upperScaleLimit: lowerScaleLimit: trimRegionResult: completion: ) Address suggestions request.
Allows to pass most of arguments as a strings converting to internally used classes.
Declaration
Swift
public func suggestAddress(_ query: String, queryType: AddressQueryType = .address, resultsCount: Int? = 10, language: String? = nil, constraints: [String]? = nil, regionPriority: [String]? = nil, upperScaleLimit: String? = nil, lowerScaleLimit: String? = nil, trimRegionResult: Bool = false, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>)->Void)
Parameters
query
Query string to send to API. String of a free-form e.g. address part.
queryType
Lets select whether the request type. There are 3 query types available:
address
— standart address suggestion query;fiasOnly
— query to only search in FIAS database: less matches, state provided address data only;findByID
— takes KLADR or FIAS ID as a query parameter to lookup additional data.resultsCount
How many suggestions to return.
1
provides more data on a single object including latitude and longitude.20
is a maximum value.language
Suggested results in “ru” — Russian or “en” — English.
constraints
Literal JSON string formated according to Dadata online API documentation.
regionPriority
List of regions’ KLADR IDs to prefer in lookup as shown in Dadata online API documentation.
upperScaleLimit
Bigger sized object in pair of scale limits.
lowerScaleLimit
Smaller sized object in pair of scale limits. Both can take following values:
country
— Страна,region
— Регион,area
— Район,city
— Город,settlement
— Населенный пункт,street
— Улица,house
— Дом,country
— Страна,trimRegionResult
Remove region and city names from suggestion top level.
completion
Result handler.
result
result of address suggestion query.
-
Basic address suggestions request to only search in FIAS database: less matches, state provided address data only.
Declaration
Swift
public func suggestAddressFromFIAS(_ query: String, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>) -> Void)
Parameters
query
Query string to send to API. String of a free-form e.g. address part.
completion
Result handler.
result
result of address suggestion query.
-
Basic address suggestions request takes KLADR or FIAS ID as a query parameter to lookup additional data.
Declaration
Swift
public func suggestByKLADRFIAS(_ query: String, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>) -> Void)
Parameters
query
KLADR or FIAS ID.
completion
Result handler.
result
result of address suggestion query.
-
Address suggestion request with custom
AddressSuggestionQuery
.Declaration
Swift
public func suggestAddress(_ query: AddressSuggestionQuery, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>) -> Void)
Parameters
query
Query object.
completion
Result handler.
result
result of address suggestion query.
-
Reverse Geocode request with latitude and longitude as a single string.
Throws
May throw if query is malformed.
Declaration
Swift
public func reverseGeocode(query: String, delimeter: Character = ",", resultsCount: Int? = 10, language: String? = "ru", searchRadius: Int? = nil, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>)->Void) throws
Parameters
query
Latitude and longitude as a string. Should have single character separator.
delimeter
Character to separate latitude and longitude. Defaults to ‘
,
’resultsCount
How many suggestions to return.
1
provides more data on a single object including latitude and longitude.20
is a maximum value.language
Suggested results in “ru” — Russian or “en” — English.
searchRadius
Radius to suggest objects nearest to coordinates point.
completion
Result handler.
result
result of reverse geocode query.
-
Reverse Geocode request with latitude and longitude as a single string.
Declaration
Swift
public func reverseGeocode(latitude: Double, longitude: Double, resultsCount: Int? = 10, language: QueryResultLanguage? = nil, searchRadius: Int? = nil, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>)->Void)
Parameters
latitude
Latitude.
longitude
Longitude.
resultsCount
How many suggestions to return.
1
provides more data on a single object including latitude and longitude.20
is a maximum value.language
Suggested results may be in Russian or English.
searchRadius
Radius to suggest objects nearest to coordinates point.
completion
Result handler.
result
result of reverse geocode query.
-
Reverse geocode request with custom
ReverseGeocodeQuery
.Declaration
Swift
public func reverseGeocode(_ query: ReverseGeocodeQuery, completion: @escaping (_ result: Result<AddressSuggestionResponse, Error>) -> Void)
Parameters
query
Query object.
completion
Result handler.
result
result of reverse geocode query.