|
| 1 | +// Copyright (c) Dropbox, Inc. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | +// THE SOFTWARE. |
| 20 | + |
| 21 | +package contacts |
| 22 | + |
| 23 | +import ( |
| 24 | + "bytes" |
| 25 | + "encoding/json" |
| 26 | + "io/ioutil" |
| 27 | + "net/http" |
| 28 | + |
| 29 | + "github.com/dropbox/dropbox-sdk-go-unofficial/dropbox" |
| 30 | +) |
| 31 | + |
| 32 | +// Client interface describes all routes in this namespace |
| 33 | +type Client interface { |
| 34 | + // DeleteManualContacts : Removes all manually added contacts. You'll still |
| 35 | + // keep contacts who are on your team or who you imported. New contacts will |
| 36 | + // be added when you share. |
| 37 | + DeleteManualContacts() (err error) |
| 38 | + // DeleteManualContactsBatch : Removes manually added contacts from the |
| 39 | + // given list. |
| 40 | + DeleteManualContactsBatch(arg *DeleteManualContactsArg) (err error) |
| 41 | +} |
| 42 | + |
| 43 | +type apiImpl dropbox.Context |
| 44 | + |
| 45 | +//DeleteManualContactsAPIError is an error-wrapper for the delete_manual_contacts route |
| 46 | +type DeleteManualContactsAPIError struct { |
| 47 | + dropbox.APIError |
| 48 | + EndpointError struct{} `json:"error"` |
| 49 | +} |
| 50 | + |
| 51 | +func (dbx *apiImpl) DeleteManualContacts() (err error) { |
| 52 | + cli := dbx.Client |
| 53 | + |
| 54 | + headers := map[string]string{} |
| 55 | + if dbx.Config.AsMemberID != "" { |
| 56 | + headers["Dropbox-API-Select-User"] = dbx.Config.AsMemberID |
| 57 | + } |
| 58 | + |
| 59 | + req, err := (*dropbox.Context)(dbx).NewRequest("api", "rpc", true, "contacts", "delete_manual_contacts", headers, nil) |
| 60 | + if err != nil { |
| 61 | + return |
| 62 | + } |
| 63 | + dbx.Config.LogInfo("req: %v", req) |
| 64 | + |
| 65 | + resp, err := cli.Do(req) |
| 66 | + if err != nil { |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + dbx.Config.LogInfo("resp: %v", resp) |
| 71 | + defer resp.Body.Close() |
| 72 | + body, err := ioutil.ReadAll(resp.Body) |
| 73 | + if err != nil { |
| 74 | + return |
| 75 | + } |
| 76 | + |
| 77 | + dbx.Config.LogDebug("body: %v", body) |
| 78 | + if resp.StatusCode == http.StatusOK { |
| 79 | + return |
| 80 | + } |
| 81 | + if resp.StatusCode == http.StatusConflict { |
| 82 | + var apiError DeleteManualContactsAPIError |
| 83 | + err = json.Unmarshal(body, &apiError) |
| 84 | + if err != nil { |
| 85 | + return |
| 86 | + } |
| 87 | + err = apiError |
| 88 | + return |
| 89 | + } |
| 90 | + err = dropbox.HandleCommonAPIErrors(resp, body) |
| 91 | + return |
| 92 | +} |
| 93 | + |
| 94 | +//DeleteManualContactsBatchAPIError is an error-wrapper for the delete_manual_contacts_batch route |
| 95 | +type DeleteManualContactsBatchAPIError struct { |
| 96 | + dropbox.APIError |
| 97 | + EndpointError *DeleteManualContactsError `json:"error"` |
| 98 | +} |
| 99 | + |
| 100 | +func (dbx *apiImpl) DeleteManualContactsBatch(arg *DeleteManualContactsArg) (err error) { |
| 101 | + cli := dbx.Client |
| 102 | + |
| 103 | + dbx.Config.LogDebug("arg: %v", arg) |
| 104 | + b, err := json.Marshal(arg) |
| 105 | + if err != nil { |
| 106 | + return |
| 107 | + } |
| 108 | + |
| 109 | + headers := map[string]string{ |
| 110 | + "Content-Type": "application/json", |
| 111 | + } |
| 112 | + if dbx.Config.AsMemberID != "" { |
| 113 | + headers["Dropbox-API-Select-User"] = dbx.Config.AsMemberID |
| 114 | + } |
| 115 | + |
| 116 | + req, err := (*dropbox.Context)(dbx).NewRequest("api", "rpc", true, "contacts", "delete_manual_contacts_batch", headers, bytes.NewReader(b)) |
| 117 | + if err != nil { |
| 118 | + return |
| 119 | + } |
| 120 | + dbx.Config.LogInfo("req: %v", req) |
| 121 | + |
| 122 | + resp, err := cli.Do(req) |
| 123 | + if err != nil { |
| 124 | + return |
| 125 | + } |
| 126 | + |
| 127 | + dbx.Config.LogInfo("resp: %v", resp) |
| 128 | + defer resp.Body.Close() |
| 129 | + body, err := ioutil.ReadAll(resp.Body) |
| 130 | + if err != nil { |
| 131 | + return |
| 132 | + } |
| 133 | + |
| 134 | + dbx.Config.LogDebug("body: %v", body) |
| 135 | + if resp.StatusCode == http.StatusOK { |
| 136 | + return |
| 137 | + } |
| 138 | + if resp.StatusCode == http.StatusConflict { |
| 139 | + var apiError DeleteManualContactsBatchAPIError |
| 140 | + err = json.Unmarshal(body, &apiError) |
| 141 | + if err != nil { |
| 142 | + return |
| 143 | + } |
| 144 | + err = apiError |
| 145 | + return |
| 146 | + } |
| 147 | + err = dropbox.HandleCommonAPIErrors(resp, body) |
| 148 | + return |
| 149 | +} |
| 150 | + |
| 151 | +// New returns a Client implementation for this namespace |
| 152 | +func New(c dropbox.Config) Client { |
| 153 | + ctx := apiImpl(dropbox.NewContext(c)) |
| 154 | + return &ctx |
| 155 | +} |
0 commit comments