Skip to content

Commit 826071c

Browse files
Add support for AdminContext
1 parent 193f654 commit 826071c

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

api/admin/asset.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type AssetResult struct {
9595
Usage interface{} `json:"usage"`
9696
OriginalFilename string `json:"original_filename"`
9797
Context AssetContextResult `json:"context"`
98+
AdminContext []AssetAdminContextResult `json:"admin_context"`
9899
Error api.ErrorResp `json:"error,omitempty"`
99100
Response interface{}
100101
}
@@ -181,6 +182,11 @@ func (m *AssetContextResult) UnmarshalJSON(data []byte) error {
181182
return nil
182183
}
183184

185+
type AssetAdminContextResult struct {
186+
Name string `json:"name"`
187+
Value interface{} `json:"value"`
188+
}
189+
184190
// UpdateAssetParams are the parameters for UpdateAsset.
185191
type UpdateAssetParams struct {
186192
AssetType api.AssetType `json:"-"`
@@ -202,6 +208,7 @@ type UpdateAssetParams struct {
202208
NotificationURL string `json:"notification_url,omitempty"`
203209
Tags api.CldAPIArray `json:"tags,omitempty,omitempty"`
204210
Context api.CldAPIMap `json:"context,omitempty"`
211+
AdminContext []api.AdminContext `json:"admin_context,omitempty"`
205212
FaceCoordinates api.Coordinates `json:"face_coordinates,omitempty"`
206213
CustomCoordinates api.Coordinates `json:"custom_coordinates,omitempty"`
207214
AccessControl interface{} `json:"access_control,omitempty"`

api/admin/asset_acceptance_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,47 @@ func getAssetTestCases() []AdminAPIAcceptanceTestCase {
176176
ExpectedCallCount: 1,
177177
})
178178

179+
assetWithAdminContext := admin.AssetResult{
180+
AssetID: "1",
181+
PublicID: cldtest.PublicID,
182+
AdminContext: []admin.AssetAdminContextResult{
183+
{
184+
Name: "key1",
185+
Value: []interface{}{"value1", "value2"},
186+
},
187+
},
188+
}
189+
responseJsonAdminContext, _ := json.Marshal(map[string]interface{}{
190+
"asset_id": "1",
191+
"public_id": cldtest.PublicID,
192+
"admin_context": []map[string]interface{}{
193+
{"name": "key1", "value": []string{"value1", "value2"}},
194+
},
195+
})
196+
197+
testCases = append(testCases, AdminAPIAcceptanceTestCase{
198+
Name: "Asset response with admin context",
199+
RequestTest: func(api *admin.API, ctx context.Context) (interface{}, error) {
200+
return api.Asset(ctx, admin.AssetParams{PublicID: cldtest.PublicID})
201+
},
202+
ResponseTest: func(response interface{}, t *testing.T) {
203+
v, ok := response.(*admin.AssetResult)
204+
if !ok {
205+
t.Errorf("Response should be type of AssetResult, %s given", reflect.TypeOf(response))
206+
}
207+
v.Response = nil // omit raw response comparison
208+
if !reflect.DeepEqual(*v, assetWithAdminContext) {
209+
t.Errorf("Response asset should be %+v\n%+v given", assetWithAdminContext, *v)
210+
}
211+
},
212+
ExpectedRequest: cldtest.ExpectedRequestParams{
213+
Method: "GET",
214+
URI: "/resources/image/upload/" + cldtest.PublicID,
215+
},
216+
JsonResponse: string(responseJsonAdminContext),
217+
ExpectedCallCount: 1,
218+
})
219+
179220
return testCases
180221
}
181222

api/api.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ type Metadata map[string]interface{}
171171
// HookExecution is the result of a hook execution.
172172
type HookExecution map[string]interface{}
173173

174+
type AdminContextOp string
175+
176+
const (
177+
AdminContextOpAdd AdminContextOp = "+"
178+
AdminContextOpRemove AdminContextOp = "-"
179+
AdminContextOpReplace AdminContextOp = "="
180+
)
181+
182+
type AdminContextType string
183+
184+
const AdminContextTypeArray AdminContextType = "array"
185+
186+
// AdminContext is the Cloudinary admin contextual metadata payload
187+
type AdminContext struct {
188+
Name string `json:"name"`
189+
Value []string `json:"value"`
190+
Type AdminContextType `json:"type"`
191+
Op AdminContextOp `json:"op"`
192+
}
193+
174194
// AutoTranscription represents the auto transcription params.
175195
type AutoTranscription struct {
176196
Translate []string `json:"translate,omitempty"`

0 commit comments

Comments
 (0)