Skip to content

Commit

Permalink
feat: add file upload support
Browse files Browse the repository at this point in the history
  • Loading branch information
koladilip committed Apr 19, 2023
1 parent 3c55407 commit feae1c9
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "main.go",
}
]
}
101 changes: 100 additions & 1 deletion bingads/bulk.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
package bingads

import (
"bytes"
"encoding/json"
"encoding/xml"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"
"strconv"
)

Expand Down Expand Up @@ -29,7 +36,20 @@ type GetBulkUploadUrlResponse struct {
RequestId string `xml:"RequestId"`
}

// GetBulkCampaignsByAccountId
type GetBulkUploadStatusRequest struct {
XMLName xml.Name `xml:"GetBulkUploadStatusRequest"`
NS string `xml:"xmlns,attr"`
RequestId string `xml:"RequestId"`
}

type GetBulkUploadStatusResponse struct {
XMLName xml.Name `xml:"GetBulkUploadStatusResponse"`
NS string `xml:"xmlns,attr"`
PercentComplete int64 `xml:"PercentComplete"`
RequestStatus string `xml:"RequestStatus"`
}

// GetBulkUploadUrl
func (c *BulkService) GetBulkUploadUrl() (*GetBulkUploadUrlResponse, error) {
accountId, _ := strconv.ParseInt(c.Session.AccountId, 10, 64)
req := GetBulkUploadUrlRequest{
Expand All @@ -49,3 +69,82 @@ func (c *BulkService) GetBulkUploadUrl() (*GetBulkUploadUrlResponse, error) {
}
return &getBulkUploadUrlResponse, nil
}

// GetBulkUploadUrl
func (c *BulkService) GetBulkUploadStatus(requestId string) (*GetBulkUploadStatusResponse, error) {
req := GetBulkUploadStatusRequest{
NS: BingNamespace,
RequestId: requestId,
}

resp, err := c.Session.SendRequest(req, c.Endpoint, "GetBulkUploadStatus")
if err != nil {
return nil, err
}

var getBulkUploadStatusResponse GetBulkUploadStatusResponse
if err = xml.Unmarshal(resp, &getBulkUploadStatusResponse); err != nil {
return nil, err
}
return &getBulkUploadStatusResponse, nil
}

type UploadBulkFileResponse struct {
TrackingId string `json:"TrackingId"`
RequestId string `json:"RequestId"`
}

func (c *BulkService) UploadBulkFile(url string, filename string) (*UploadBulkFileResponse, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()

payload := &bytes.Buffer{}
writer := multipart.NewWriter(payload)
part1, err := writer.CreateFormFile("", filepath.Base(filename))
if err != nil {
return nil, err
}
_, err = io.Copy(part1, file)
if err != nil {
return nil, err
}

if err = writer.Close(); err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, payload)
if err != nil {
return nil, err
}

token, err := c.Session.TokenSource.Token()
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", writer.FormDataContentType())
req.Header.Set("AuthenticationToken", token.AccessToken)
req.Header.Add("DeveloperToken", c.Session.DeveloperToken)
req.Header.Add("CustomerId", c.Session.CustomerId)
req.Header.Add("AccountId", c.Session.AccountId)

resp, err := c.Session.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var uploadBulkFileResponse UploadBulkFileResponse

if err = json.Unmarshal(body, &uploadBulkFileResponse); err != nil {
return nil, err
}

return &uploadBulkFileResponse, nil
}
27 changes: 24 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"time"

"os"
"strings"
Expand Down Expand Up @@ -45,7 +46,7 @@ func generate_access_token(config *oauth2.Config) (*oauth2.Token, error) {

func get_session_config(config *oauth2.Config, token *oauth2.Token) bingads.SessionConfig {
return bingads.SessionConfig{
OAuth2Config: config,
OAuth2Config: config,
OAuth2Token: token,
AccountId: os.Getenv("CUSTOMER_ACCOUNT_ID"),
CustomerId: os.Getenv("CUSTOMER_ID"),
Expand Down Expand Up @@ -78,9 +79,29 @@ func main() {
}
session := bingads.NewSession(get_session_config(oauth2Config, token))
service := bingads.NewBulkService(session)
resp, err := service.GetBulkUploadUrl()
urlResp, err := service.GetBulkUploadUrl()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%#v\n", resp)
fmt.Printf("urlResp %#v\n", urlResp)

uploadBulkFileResp, err := service.UploadBulkFile(urlResp.UploadUrl, "testfile.zip")
if err != nil {
log.Fatal(err)
}

fmt.Printf("uploadBulkFileResp %#v\n", uploadBulkFileResp)
var uploadStatus *bingads.GetBulkUploadStatusResponse
for {
time.Sleep(5 * time.Second)
uploadStatus, err = service.GetBulkUploadStatus(uploadBulkFileResp.RequestId)
if err != nil {
log.Fatal(err)
break
}
fmt.Printf("%#v", uploadStatus)
if uploadStatus.RequestStatus == "Completed" {
break
}
}
}

0 comments on commit feae1c9

Please sign in to comment.