|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "runtime" |
| 6 | +) |
| 7 | + |
| 8 | +const ( |
| 9 | + HEADER_NAME_USER_AGENT = "User-Agent" |
| 10 | + |
| 11 | + SDK_NAME = "go-sdk-template" |
| 12 | +) |
| 13 | + |
| 14 | +// |
| 15 | +// GetSdkHeaders - returns the set of SDK-specific headers to be included in an outgoing request. |
| 16 | +// |
| 17 | +// This function is invoked by generated service methods (i.e. methods which implement the REST API operations |
| 18 | +// defined within the API definition). The purpose of this function is to give the SDK implementor the opportunity |
| 19 | +// to provide SDK-specific HTTP headers that will be sent with an outgoing REST API request. |
| 20 | +// This function is invoked for each invocation of a generated service method, |
| 21 | +// so the set of HTTP headers could be request-specific. |
| 22 | +// As an optimization, if your SDK will be returning the same set of HTTP headers for each invocation of this |
| 23 | +// function, it is recommended that you initialize the returned map just once (perhaps by using |
| 24 | +// lazy initialization) and simply return it each time the function is invoked, instead of building it each time |
| 25 | +// as in the example below. |
| 26 | +// |
| 27 | +// Parameters: |
| 28 | +// serviceName - the name of the service as defined in the API definition (e.g. "MyService1") |
| 29 | +// serviceVersion - the version of the service as defined in the API definition (e.g. "V1") |
| 30 | +// operationId - the operationId as defined in the API definition (e.g. getContext) |
| 31 | +// |
| 32 | +// Returns: |
| 33 | +// a Map which contains the set of headers to be included in the REST API request |
| 34 | +// |
| 35 | +func GetSdkHeaders(serviceName string, serviceVersion string, operationId string) map[string]string { |
| 36 | + sdkHeaders := make(map[string]string) |
| 37 | + |
| 38 | + sdkHeaders[HEADER_NAME_USER_AGENT] = GetUserAgentInfo() |
| 39 | + |
| 40 | + return sdkHeaders |
| 41 | +} |
| 42 | + |
| 43 | +var userAgent string = fmt.Sprintf("%s-%s %s", SDK_NAME, Version, GetSystemInfo()) |
| 44 | + |
| 45 | +func GetUserAgentInfo() string { |
| 46 | + return userAgent |
| 47 | +} |
| 48 | + |
| 49 | +var systemInfo = fmt.Sprintf("(arch=%s; os=%s; go.version=%s)", runtime.GOARCH, runtime.GOOS, runtime.Version()) |
| 50 | + |
| 51 | +func GetSystemInfo() string { |
| 52 | + return systemInfo |
| 53 | +} |
0 commit comments