Skip to content

Commit

Permalink
Merge branch 'new_version' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Vorkytaka committed Apr 19, 2017
2 parents f811a47 + 735093e commit 719d8fa
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 87 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,41 @@ info, err := vk.Account.GetProfileInfo()
```
Set user status:
```go
userID := 1
// If you want to update status on your page
// then set ID to 0
userID := 0
ok, err := vk.Status.Set("New status", userID)
```
Post photo on wall:
```go
// your id or
// id of group
id := 0

server, err := vk.Photos.GetWallUploadServer(id)
server, err := vk.Photos.GetWallUploadServer(uint(id))
if err != nil {
log.Fatal(err)
}

// path to the image
path := "./example/x.png"
path := "D:/x.png"
uploaded, err := vk.Upload.PhotoWall(server.Response.UploadURL, path)
if err != nil {
log.Fatal(err)
}

saved, err := vk.Photos.SaveWallPhoto(0, id, uploaded.Photo, uploaded.Hash, "", uploaded.Server, 0, 0)
saved, err := vk.Photos.SaveWallPhoto(0, uint(id), uploaded.Photo, uploaded.Hash, "", uploaded.Server, 0, 0)
if err != nil {
log.Fatal(err)
}

text := "Caption for the post"
photoID := "photo" + fmt.Sprint(saved.Response[0].OwnerID) + "_" + fmt.Sprint(saved.Response[0].ID)

// -id if you post to group wall
postID, err := vk.Wall.Post(id, false, true, false, false, false, text, saved.Response[0].ID, "", "", 0, 0, 0, 0, 0)
x, err := vk.Wall.Post(id, false, true, false, false, false, text, photoID, "", "", 0, 0, 0, 0, 0)
if err != nil {
log.Fatal(err)
}
fmt.Println(x)
```

### If you need to call method that not done yet:
Expand Down
79 changes: 47 additions & 32 deletions easyvk/fave-response.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
package easyvk

// A FaveUsers describes a slice of users
// A FaveUsers describes a list of users
// whom the current user has bookmarked.
type FaveUsers []struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
UID int `json:"uid"`
type FaveUsers struct {
Response struct {
Count int `json:"count"`
Items []struct {
ID int `json:"id"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
} `json:"items"`
} `json:"response"`
}

// A FaveLinks describes a slice of links
// A FaveLinks describes a list of links
// that the current user has bookmarked.
type FaveLinks []struct {
ID string `json:"id"`
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
ImageSrc string `json:"image_src"`
ImageMiddle string `json:"image_middle"`
ImageBig string `json:"image_big"`
type FaveLinks struct {
Response struct {
Count int `json:"count"`
Items []struct {
ID string `json:"id"`
URL string `json:"url"`
Title string `json:"title"`
Description string `json:"description"`
Photo50 string `json:"photo_50"`
Photo100 string `json:"photo_100"`
Photo200 string `json:"photo_200"`
} `json:"items"`
} `json:"response"`
}

// A FavePhotos describes a slice of photos
// A FavePhotos describes a list of photos
// that the current user has bookmarked.
type FavePhotos []struct {
AccessKey string `json:"access_key"`
Aid int `json:"aid"`
Created int `json:"created"`
Height int `json:"height"`
OwnerID int `json:"owner_id"`
Pid int `json:"pid"`
PostID int `json:"post_id,omitempty"`
Src string `json:"src"`
SrcBig string `json:"src_big"`
SrcSmall string `json:"src_small"`
SrcXbig string `json:"src_xbig"`
SrcXxbig string `json:"src_xxbig"`
SrcXxxbig string `json:"src_xxxbig,omitempty"`
Text string `json:"text"`
UserID int `json:"user_id"`
Width int `json:"width"`
type FavePhotos struct {
Response struct {
Count int `json:"count"`
Items []struct {
ID int `json:"id"`
AlbumID int `json:"album_id"`
OwnerID int `json:"owner_id"`
UserID int `json:"user_id"`
Photo75 string `json:"photo_75"`
Photo130 string `json:"photo_130"`
Photo604 string `json:"photo_604"`
Photo807 string `json:"photo_807"`
Photo1280 string `json:"photo_1280"`
Photo2560 string `json:"photo_2560"`
Width int `json:"width"`
Height int `json:"height"`
Text string `json:"text"`
Date int `json:"date"`
PostID int `json:"post_id"`
AccessKey string `json:"access_key"`
} `json:"items"`
} `json:"response"`
}
34 changes: 15 additions & 19 deletions easyvk/fave.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,61 @@ type Fave struct {
}

// GetUsers returns a list of users whom the current user has bookmarked.
// Also this func return count of all bookmarked users.
func (f *Fave) GetUsers(offset, count uint) (FaveUsers, uint, error) {
func (f *Fave) GetUsers(offset, count uint) (FaveUsers, error) {
params := map[string]string{
"offset": fmt.Sprint(offset),
"count": fmt.Sprint(count),
}
resp, err := f.vk.Request("fave.getUsers", params)
if err != nil {
return FaveUsers{}, 0, err
return FaveUsers{}, err
}
nextResp, quantity, err := f.vk.ResponseWithCount(resp)
var users FaveUsers
err = json.Unmarshal(nextResp, &users)
err = json.Unmarshal(resp, &users)
if err != nil {
return FaveUsers{}, 0, err
return FaveUsers{}, err
}

return users, quantity, nil
return users, nil
}

// GetLinks returns a list of links that the current user has bookmarked.
// Also this func return count of all bookmarked links.
func (f *Fave) GetLinks(offset, count uint) (FaveLinks, uint, error) {
func (f *Fave) GetLinks(offset, count uint) (FaveLinks, error) {
params := map[string]string{
"offset": fmt.Sprint(offset),
"count": fmt.Sprint(count),
}
resp, err := f.vk.Request("fave.getLinks", params)
if err != nil {
return FaveLinks{}, 0, err
return FaveLinks{}, err
}
nextResp, quantity, err := f.vk.ResponseWithCount(resp)
var links FaveLinks
err = json.Unmarshal(nextResp, &links)
err = json.Unmarshal(resp, &links)
if err != nil {
return FaveLinks{}, 0, err
return FaveLinks{}, err
}

return links, quantity, nil
return links, nil
}

// GetPhotos returns a list of photos that the current user has bookmarked.
// Also this func return count of all bookmarked photos.
func (f *Fave) GetPhotos(offset, count uint) (FavePhotos, uint, error) {
func (f *Fave) GetPhotos(offset, count uint) (FavePhotos, error) {
params := map[string]string{
"offset": fmt.Sprint(offset),
"count": fmt.Sprint(count),
"photo_sizes": "0",
}
resp, err := f.vk.Request("fave.getPhotos", params)
if err != nil {
return FavePhotos{}, 0, err
return FavePhotos{}, err
}
nextResp, quantity, err := f.vk.ResponseWithCount(resp)
var links FavePhotos
err = json.Unmarshal(nextResp, &links)
err = json.Unmarshal(resp, &links)
if err != nil {
return FavePhotos{}, 0, err
return FavePhotos{}, err
}

return links, quantity, nil
return links, nil
}
19 changes: 9 additions & 10 deletions easyvk/photos-response.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,25 @@ package easyvk
type WallUploadServer struct {
Response struct {
UploadURL string `json:"upload_url"`
AlbumID int `json:"aid"`
UserID int `json:"mid"`
AlbumID int `json:"album_id"`
UserID int `json:"user_id"`
} `json:"response"`
}

// SavedWallPhoto describes info about
// saved photo on wall after being uploaded.
type SavedWallPhoto struct {
Response []struct {
Pid int `json:"pid"`
ID string `json:"id"`
Aid int `json:"aid"`
ID int `json:"id"`
AlbumID int `json:"album_id"`
OwnerID int `json:"owner_id"`
Src string `json:"src"`
SrcBig string `json:"src_big"`
SrcSmall string `json:"src_small"`
SrcXbig string `json:"src_xbig"`
Photo75 string `json:"photo_75"`
Photo130 string `json:"photo_130"`
Photo604 string `json:"photo_604"`
Photo807 string `json:"photo_807"`
Width int `json:"width"`
Height int `json:"height"`
Text string `json:"text"`
Created int `json:"created"`
Date int `json:"date"`
} `json:"response"`
}
22 changes: 3 additions & 19 deletions easyvk/vk.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const apiURL = "https://api.vk.com/method/"
// working with VK API.
type VK struct {
AccessToken string
Version string
Account Account
Fave Fave
Photos Photos
Expand All @@ -29,6 +30,7 @@ type VK struct {
func WithToken(token string) VK {
vk := VK{}
vk.AccessToken = token
vk.Version = "5.63"
vk.Account = Account{&vk }
vk.Fave = Fave{&vk }
vk.Photos = Photos{&vk }
Expand All @@ -50,6 +52,7 @@ func (vk *VK) Request(method string, params map[string]string) ([]byte, error) {
query.Set(k, v)
}
query.Set("access_token", vk.AccessToken)
query.Set("v", vk.Version)
u.RawQuery = query.Encode()

resp, err := http.Get(u.String())
Expand All @@ -72,25 +75,6 @@ func (vk *VK) Request(method string, params map[string]string) ([]byte, error) {
return body, nil
}

// ResponseWithCount helps to work with specific response type
func (vk *VK) ResponseWithCount(body []byte) ([]byte, uint, error) {
var response struct {
Response []interface{}
}

err := json.Unmarshal(body, &response)
if err != nil {
return nil, 0, err
}

byteArray, err := json.Marshal(response.Response[1:])
if err != nil {
return nil, 0, err
}

return byteArray, uint(response.Response[0].(float64)), nil
}

type response struct {
Response int `json:"response"`
}

0 comments on commit 719d8fa

Please sign in to comment.