1// Package spellcheck implements the Azure ARM Spellcheck service API version 1.0.
2//
3// The Spell Check API - V7 lets you check a text string for spelling and grammar errors.
4package spellcheck
5
6// Copyright (c) Microsoft Corporation. All rights reserved.
7// Licensed under the MIT License. See License.txt in the project root for license information.
8//
9// Code generated by Microsoft (R) AutoRest Code Generator.
10// Changes may cause incorrect behavior and will be lost if the code is regenerated.
11
12import (
13	"context"
14	"github.com/Azure/go-autorest/autorest"
15	"github.com/Azure/go-autorest/autorest/azure"
16	"github.com/Azure/go-autorest/tracing"
17	"net/http"
18)
19
20const (
21	// DefaultEndpoint is the default value for endpoint
22	DefaultEndpoint = "https://api.cognitive.microsoft.com"
23)
24
25// BaseClient is the base client for Spellcheck.
26type BaseClient struct {
27	autorest.Client
28	Endpoint string
29}
30
31// New creates an instance of the BaseClient client.
32func New() BaseClient {
33	return NewWithoutDefaults(DefaultEndpoint)
34}
35
36// NewWithoutDefaults creates an instance of the BaseClient client.
37func NewWithoutDefaults(endpoint string) BaseClient {
38	return BaseClient{
39		Client:   autorest.NewClientWithUserAgent(UserAgent()),
40		Endpoint: endpoint,
41	}
42}
43
44// SpellCheckerMethod sends the spell checker request.
45// Parameters:
46// textParameter - the text string to check for spelling and grammar errors. The combined length of the text
47// string, preContextText string, and postContextText string may not exceed 10,000 characters. You may specify
48// this parameter in the query string of a GET request or in the body of a POST request. Because of the query
49// string length limit, you'll typically use a POST request unless you're checking only short strings.
50// acceptLanguage - a comma-delimited list of one or more languages to use for user interface strings. The list
51// is in decreasing order of preference. For additional information, including expected format, see
52// [RFC2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html). This header and the setLang query
53// parameter are mutually exclusive; do not specify both. If you set this header, you must also specify the cc
54// query parameter. Bing will use the first supported language it finds from the list, and combine that
55// language with the cc parameter value to determine the market to return results for. If the list does not
56// include a supported language, Bing will find the closest language and market that supports the request, and
57// may use an aggregated or default market for the results instead of a specified one. You should use this
58// header and the cc query parameter only if you specify multiple languages; otherwise, you should use the mkt
59// and setLang query parameters. A user interface string is a string that's used as a label in a user
60// interface. There are very few user interface strings in the JSON response objects. Any links in the response
61// objects to Bing.com properties will apply the specified language.
62// pragma - by default, Bing returns cached content, if available. To prevent Bing from returning cached
63// content, set the Pragma header to no-cache (for example, Pragma: no-cache).
64// userAgent - the user agent originating the request. Bing uses the user agent to provide mobile users with an
65// optimized experience. Although optional, you are strongly encouraged to always specify this header. The
66// user-agent should be the same string that any commonly used browser would send. For information about user
67// agents, see [RFC 2616](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).
68// clientID - bing uses this header to provide users with consistent behavior across Bing API calls. Bing often
69// flights new features and improvements, and it uses the client ID as a key for assigning traffic on different
70// flights. If you do not use the same client ID for a user across multiple requests, then Bing may assign the
71// user to multiple conflicting flights. Being assigned to multiple conflicting flights can lead to an
72// inconsistent user experience. For example, if the second request has a different flight assignment than the
73// first, the experience may be unexpected. Also, Bing can use the client ID to tailor web results to that
74// client ID’s search history, providing a richer experience for the user. Bing also uses this header to help
75// improve result rankings by analyzing the activity generated by a client ID. The relevance improvements help
76// with better quality of results delivered by Bing APIs and in turn enables higher click-through rates for the
77// API consumer. IMPORTANT: Although optional, you should consider this header required. Persisting the client
78// ID across multiple requests for the same end user and device combination enables 1) the API consumer to
79// receive a consistent user experience, and 2) higher click-through rates via better quality of results from
80// the Bing APIs. Each user that uses your application on the device must have a unique, Bing generated client
81// ID. If you do not include this header in the request, Bing generates an ID and returns it in the
82// X-MSEdge-ClientID response header. The only time that you should NOT include this header in a request is the
83// first time the user uses your app on that device. Use the client ID for each Bing API request that your app
84// makes for this user on the device. Persist the client ID. To persist the ID in a browser app, use a
85// persistent HTTP cookie to ensure the ID is used across all sessions. Do not use a session cookie. For other
86// apps such as mobile apps, use the device's persistent storage to persist the ID. The next time the user uses
87// your app on that device, get the client ID that you persisted. Bing responses may or may not include this
88// header. If the response includes this header, capture the client ID and use it for all subsequent Bing
89// requests for the user on that device. If you include the X-MSEdge-ClientID, you must not include cookies in
90// the request.
91// clientIP - the IPv4 or IPv6 address of the client device. The IP address is used to discover the user's
92// location. Bing uses the location information to determine safe search behavior. Although optional, you are
93// encouraged to always specify this header and the X-Search-Location header. Do not obfuscate the address (for
94// example, by changing the last octet to 0). Obfuscating the address results in the location not being
95// anywhere near the device's actual location, which may result in Bing serving erroneous results.
96// location - a semicolon-delimited list of key/value pairs that describe the client's geographical location.
97// Bing uses the location information to determine safe search behavior and to return relevant local content.
98// Specify the key/value pair as <key>:<value>. The following are the keys that you use to specify the user's
99// location. lat (required): The latitude of the client's location, in degrees. The latitude must be greater
100// than or equal to -90.0 and less than or equal to +90.0. Negative values indicate southern latitudes and
101// positive values indicate northern latitudes. long (required): The longitude of the client's location, in
102// degrees. The longitude must be greater than or equal to -180.0 and less than or equal to +180.0. Negative
103// values indicate western longitudes and positive values indicate eastern longitudes. re (required): The
104// radius, in meters, which specifies the horizontal accuracy of the coordinates. Pass the value returned by
105// the device's location service. Typical values might be 22m for GPS/Wi-Fi, 380m for cell tower triangulation,
106// and 18,000m for reverse IP lookup. ts (optional): The UTC UNIX timestamp of when the client was at the
107// location. (The UNIX timestamp is the number of seconds since January 1, 1970.) head (optional): The client's
108// relative heading or direction of travel. Specify the direction of travel as degrees from 0 through 360,
109// counting clockwise relative to true north. Specify this key only if the sp key is nonzero. sp (optional):
110// The horizontal velocity (speed), in meters per second, that the client device is traveling. alt (optional):
111// The altitude of the client device, in meters. are (optional): The radius, in meters, that specifies the
112// vertical accuracy of the coordinates. Specify this key only if you specify the alt key. Although many of the
113// keys are optional, the more information that you provide, the more accurate the location results are.
114// Although optional, you are encouraged to always specify the user's geographical location. Providing the
115// location is especially important if the client's IP address does not accurately reflect the user's physical
116// location (for example, if the client uses VPN). For optimal results, you should include this header and the
117// X-Search-ClientIP header, but at a minimum, you should include this header.
118// actionType - a string that's used by logging to determine whether the request is coming from an interactive
119// session or a page load. The following are the possible values. 1) Edit—The request is from an interactive
120// session 2) Load—The request is from a page load
121// appName - the unique name of your app. The name must be known by Bing. Do not include this parameter unless
122// you have previously contacted Bing to get a unique app name. To get a unique name, contact your Bing
123// Business Development manager.
124// countryCode - a 2-character country code of the country where the results come from. This API supports only
125// the United States market. If you specify this query parameter, it must be set to us. If you set this
126// parameter, you must also specify the Accept-Language header. Bing uses the first supported language it finds
127// from the languages list, and combine that language with the country code that you specify to determine the
128// market to return results for. If the languages list does not include a supported language, Bing finds the
129// closest language and market that supports the request, or it may use an aggregated or default market for the
130// results instead of a specified one. You should use this query parameter and the Accept-Language query
131// parameter only if you specify multiple languages; otherwise, you should use the mkt and setLang query
132// parameters. This parameter and the mkt query parameter are mutually exclusive—do not specify both.
133// clientMachineName - a unique name of the device that the request is being made from. Generate a unique value
134// for each device (the value is unimportant). The service uses the ID to help debug issues and improve the
135// quality of corrections.
136// docID - a unique ID that identifies the document that the text belongs to. Generate a unique value for each
137// document (the value is unimportant). The service uses the ID to help debug issues and improve the quality of
138// corrections.
139// market - the market where the results come from. You are strongly encouraged to always specify the market,
140// if known. Specifying the market helps Bing route the request and return an appropriate and optimal response.
141// This parameter and the cc query parameter are mutually exclusive—do not specify both.
142// sessionID - a unique ID that identifies this user session. Generate a unique value for each user session
143// (the value is unimportant). The service uses the ID to help debug issues and improve the quality of
144// corrections
145// setLang - the language to use for user interface strings. Specify the language using the ISO 639-1 2-letter
146// language code. For example, the language code for English is EN. The default is EN (English). Although
147// optional, you should always specify the language. Typically, you set setLang to the same language specified
148// by mkt unless the user wants the user interface strings displayed in a different language. This parameter
149// and the Accept-Language header are mutually exclusive—do not specify both. A user interface string is a
150// string that's used as a label in a user interface. There are few user interface strings in the JSON response
151// objects. Also, any links to Bing.com properties in the response objects apply the specified language.
152// userID - a unique ID that identifies the user. Generate a unique value for each user (the value is
153// unimportant). The service uses the ID to help debug issues and improve the quality of corrections.
154// mode - the type of spelling and grammar checks to perform. The following are the possible values (the values
155// are case insensitive). The default is Proof. 1) Proof—Finds most spelling and grammar mistakes. 2)
156// Spell—Finds most spelling mistakes but does not find some of the grammar errors that Proof catches (for
157// example, capitalization and repeated words)
158// preContextText - a string that gives context to the text string. For example, the text string petal is
159// valid. However, if you set preContextText to bike, the context changes and the text string becomes not
160// valid. In this case, the API suggests that you change petal to pedal (as in bike pedal). This text is not
161// checked for grammar or spelling errors. The combined length of the text string, preContextText string, and
162// postContextText string may not exceed 10,000 characters. You may specify this parameter in the query string
163// of a GET request or in the body of a POST request.
164// postContextText - a string that gives context to the text string. For example, the text string read is
165// valid. However, if you set postContextText to carpet, the context changes and the text string becomes not
166// valid. In this case, the API suggests that you change read to red (as in red carpet). This text is not
167// checked for grammar or spelling errors. The combined length of the text string, preContextText string, and
168// postContextText string may not exceed 10,000 characters. You may specify this parameter in the query string
169// of a GET request or in the body of a POST request.
170func (client BaseClient) SpellCheckerMethod(ctx context.Context, textParameter string, acceptLanguage string, pragma string, userAgent string, clientID string, clientIP string, location string, actionType ActionType, appName string, countryCode string, clientMachineName string, docID string, market string, sessionID string, setLang string, userID string, mode Mode, preContextText string, postContextText string) (result SpellCheck, err error) {
171	if tracing.IsEnabled() {
172		ctx = tracing.StartSpan(ctx, fqdn+"/BaseClient.SpellCheckerMethod")
173		defer func() {
174			sc := -1
175			if result.Response.Response != nil {
176				sc = result.Response.Response.StatusCode
177			}
178			tracing.EndSpan(ctx, sc, err)
179		}()
180	}
181	req, err := client.SpellCheckerMethodPreparer(ctx, textParameter, acceptLanguage, pragma, userAgent, clientID, clientIP, location, actionType, appName, countryCode, clientMachineName, docID, market, sessionID, setLang, userID, mode, preContextText, postContextText)
182	if err != nil {
183		err = autorest.NewErrorWithError(err, "spellcheck.BaseClient", "SpellCheckerMethod", nil, "Failure preparing request")
184		return
185	}
186
187	resp, err := client.SpellCheckerMethodSender(req)
188	if err != nil {
189		result.Response = autorest.Response{Response: resp}
190		err = autorest.NewErrorWithError(err, "spellcheck.BaseClient", "SpellCheckerMethod", resp, "Failure sending request")
191		return
192	}
193
194	result, err = client.SpellCheckerMethodResponder(resp)
195	if err != nil {
196		err = autorest.NewErrorWithError(err, "spellcheck.BaseClient", "SpellCheckerMethod", resp, "Failure responding to request")
197		return
198	}
199
200	return
201}
202
203// SpellCheckerMethodPreparer prepares the SpellCheckerMethod request.
204func (client BaseClient) SpellCheckerMethodPreparer(ctx context.Context, textParameter string, acceptLanguage string, pragma string, userAgent string, clientID string, clientIP string, location string, actionType ActionType, appName string, countryCode string, clientMachineName string, docID string, market string, sessionID string, setLang string, userID string, mode Mode, preContextText string, postContextText string) (*http.Request, error) {
205	urlParameters := map[string]interface{}{
206		"Endpoint": client.Endpoint,
207	}
208
209	queryParameters := map[string]interface{}{
210		"Text": autorest.Encode("query", textParameter),
211	}
212	if len(string(actionType)) > 0 {
213		queryParameters["ActionType"] = autorest.Encode("query", actionType)
214	}
215	if len(appName) > 0 {
216		queryParameters["AppName"] = autorest.Encode("query", appName)
217	}
218	if len(countryCode) > 0 {
219		queryParameters["cc"] = autorest.Encode("query", countryCode)
220	}
221	if len(clientMachineName) > 0 {
222		queryParameters["ClientMachineName"] = autorest.Encode("query", clientMachineName)
223	}
224	if len(docID) > 0 {
225		queryParameters["DocId"] = autorest.Encode("query", docID)
226	}
227	if len(market) > 0 {
228		queryParameters["mkt"] = autorest.Encode("query", market)
229	}
230	if len(sessionID) > 0 {
231		queryParameters["SessionId"] = autorest.Encode("query", sessionID)
232	}
233	if len(setLang) > 0 {
234		queryParameters["SetLang"] = autorest.Encode("query", setLang)
235	}
236	if len(userID) > 0 {
237		queryParameters["UserId"] = autorest.Encode("query", userID)
238	}
239	if len(string(mode)) > 0 {
240		queryParameters["Mode"] = autorest.Encode("query", mode)
241	}
242	if len(preContextText) > 0 {
243		queryParameters["PreContextText"] = autorest.Encode("query", preContextText)
244	}
245	if len(postContextText) > 0 {
246		queryParameters["PostContextText"] = autorest.Encode("query", postContextText)
247	}
248
249	preparer := autorest.CreatePreparer(
250		autorest.AsPost(),
251		autorest.WithCustomBaseURL("{Endpoint}/bing/v7.0", urlParameters),
252		autorest.WithPath("/spellcheck"),
253		autorest.WithQueryParameters(queryParameters),
254		autorest.WithHeader("X-BingApis-SDK", "true"))
255	if len(acceptLanguage) > 0 {
256		preparer = autorest.DecoratePreparer(preparer,
257			autorest.WithHeader("Accept-Language", autorest.String(acceptLanguage)))
258	}
259	if len(pragma) > 0 {
260		preparer = autorest.DecoratePreparer(preparer,
261			autorest.WithHeader("Pragma", autorest.String(pragma)))
262	}
263	if len(userAgent) > 0 {
264		preparer = autorest.DecoratePreparer(preparer,
265			autorest.WithHeader("User-Agent", autorest.String(userAgent)))
266	}
267	if len(clientID) > 0 {
268		preparer = autorest.DecoratePreparer(preparer,
269			autorest.WithHeader("X-MSEdge-ClientID", autorest.String(clientID)))
270	}
271	if len(clientIP) > 0 {
272		preparer = autorest.DecoratePreparer(preparer,
273			autorest.WithHeader("X-MSEdge-ClientIP", autorest.String(clientIP)))
274	}
275	if len(location) > 0 {
276		preparer = autorest.DecoratePreparer(preparer,
277			autorest.WithHeader("X-Search-Location", autorest.String(location)))
278	}
279	return preparer.Prepare((&http.Request{}).WithContext(ctx))
280}
281
282// SpellCheckerMethodSender sends the SpellCheckerMethod request. The method will close the
283// http.Response Body if it receives an error.
284func (client BaseClient) SpellCheckerMethodSender(req *http.Request) (*http.Response, error) {
285	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
286}
287
288// SpellCheckerMethodResponder handles the response to the SpellCheckerMethod request. The method always
289// closes the http.Response Body.
290func (client BaseClient) SpellCheckerMethodResponder(resp *http.Response) (result SpellCheck, err error) {
291	err = autorest.Respond(
292		resp,
293		azure.WithErrorUnlessStatusCode(http.StatusOK),
294		autorest.ByUnmarshallingJSON(&result),
295		autorest.ByClosing())
296	result.Response = autorest.Response{Response: resp}
297	return
298}
299