-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAppStore_InfoApp.class.go
90 lines (74 loc) · 1.92 KB
/
AppStore_InfoApp.class.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Developer by : Azozz ALFiras
package main
import (
"fmt"
"io/ioutil"
"net/http"
"encoding/json"
)
type AppStoreInfoApp struct {
httpClient http.Client
}
func (app *AppStoreInfoApp) SentRequest(bundleID string) (map[string]interface{}, error) {
url := fmt.Sprintf("http://itunes.apple.com/lookup?bundleId=%s", bundleID)
response, err := app.httpClient.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
var result map[string]interface{}
err = json.Unmarshal(data, &result)
if err != nil {
return nil, err
}
if results, ok := result["results"].([]interface{}); ok && len(results) > 0 {
if appData, ok := results[0].(map[string]interface{}); ok {
return appData, nil
}
}
return nil, fmt.Errorf("Invalid response data")
}
func (app *AppStoreInfoApp) GetInfoApp(bundleID string, version string) (map[string]interface{}, error) {
data, err := app.SentRequest(bundleID)
if err != nil {
return nil, err
}
vAppStore, ok := data["version"].(string)
if !ok {
return nil, fmt.Errorf("Invalid version data")
}
appLink, ok := data["trackViewUrl"].(string)
if !ok {
return nil, fmt.Errorf("Invalid app link data")
}
if vAppStore == version {
return app.ResponseAPI("Yes", appLink), nil
}
return app.ResponseAPI("No", appLink), nil
}
func (app *AppStoreInfoApp) ResponseAPI(status string, url string) map[string]interface{} {
if status == "Yes" {
return map[string]interface{}{
"status": "success",
"status_message": "There is no application update",
}
}
return map[string]interface{}{
"status": "failed",
"status_message": "The version does not match. An update is required",
"app_link": url,
}
}
func main() {
app := AppStoreInfoApp{}
result, err := app.GetInfoApp("your_bundle_id", "your_version")
if err != nil {
fmt.Println(err)
} else {
fmt.Println(result)
}
}