Skip to content

Commit

Permalink
Merge pull request #8 from Dobefu/feature/api-routes
Browse files Browse the repository at this point in the history
Feature/api routes
  • Loading branch information
Dobefu authored Dec 22, 2024
2 parents ec0d025 + 7e2aceb commit 445e162
Show file tree
Hide file tree
Showing 7 changed files with 207 additions and 17 deletions.
21 changes: 21 additions & 0 deletions cmd/api/get-content-type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package api

import (
"fmt"

"github.com/Dobefu/csb/cmd/cs_sdk"
)

func GetContentType(contentType string) (map[string]interface{}, error) {
data, err := cs_sdk.Request(
fmt.Sprintf("content_types/%s", contentType),
"GET",
nil,
)

if err != nil {
return nil, err
}

return data, nil
}
32 changes: 32 additions & 0 deletions cmd/api/get-content-type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package api

import (
"os"
"testing"

"github.com/Dobefu/csb/cmd/init_env"
"github.com/stretchr/testify/assert"
)

func TestGetContentType(t *testing.T) {
var contentType map[string]interface{}
var err error

init_env.Main("../../.env.test")

oldApiKey := os.Getenv("CS_API_KEY")
os.Setenv("CS_API_KEY", "bogus")

_, err = GetContentType("basic_page")
assert.NotEqual(t, nil, err)

os.Setenv("CS_API_KEY", oldApiKey)

contentType, err = GetContentType("basic_page")
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, contentType)

contentType, err = GetContentType("bogus")
assert.NotEqual(t, nil, err)
assert.NotEqual(t, nil, contentType)
}
1 change: 1 addition & 0 deletions cmd/server/handle-routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func HandleRoutes(mux *http.ServeMux, apiPath string) {
apiRoute(mux, apiPath, "/get-entry-by-url", "GET", v1.GetEntryByUrl)
apiRoute(mux, apiPath, "/get-entry-by-uid", "GET", v1.GetEntryByUid)
apiRoute(mux, apiPath, "/content-types", "GET", v1.GetContentTypes)
apiRoute(mux, apiPath, "/content-type", "GET", v1.GetContentType)
}

func apiRoute(
Expand Down
27 changes: 27 additions & 0 deletions cmd/server/handle-routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,33 @@ func TestHandleRoutes(t *testing.T) {
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, body["data"])
assert.Equal(t, nil, body["error"])

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/content-type"),
true,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.Contains(t, body["error"], "missing required query params")

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/content-type?content_type=basic_page"),
true,
)
assert.Equal(t, nil, err)
assert.NotEqual(t, nil, body["data"])
assert.Equal(t, nil, body["error"])

body, err = request(
"GET",
fmt.Sprintf("%s/%s", server.URL, "/content-type?content_type=bogus"),
true,
)
assert.Equal(t, nil, err)
assert.Equal(t, nil, body["data"])
assert.NotEqual(t, nil, body["error"])
}

func request(method string, path string, withAuthToken bool) (body map[string]interface{}, err error) {
Expand Down
44 changes: 44 additions & 0 deletions cmd/server/routes/v1/get-content-type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v1

import (
"encoding/json"
"fmt"
"net/http"

"github.com/Dobefu/csb/cmd/api"
"github.com/Dobefu/csb/cmd/server/utils"
"github.com/Dobefu/csb/cmd/server/validation"
)

func GetContentType(w http.ResponseWriter, r *http.Request) {
params, err := validation.CheckRequiredQueryParams(
r,
"content_type",
)

if err != nil {
utils.PrintError(w, err, false)
return
}

contentTypeName := params["content_type"].(string)

contentType, err := api.GetContentType(contentTypeName)

if err != nil {
utils.PrintError(w, err, false)
return
}

output := utils.ConstructOutput()
output["data"] = contentType

json, err := json.Marshal(output)

if err != nil {
utils.PrintError(w, err, true)
return
}

fmt.Fprint(w, string(json))
}
15 changes: 14 additions & 1 deletion docs/content/api-server/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ description = "An overview of all the API endpoints"

[++RoutableEntryResponse++](response-types#routableentryresponse)

## Get entry by UID
## Get all content types

`/api/v1/content-types`

Expand All @@ -44,4 +44,17 @@ description = "An overview of all the API endpoints"

### Response type

[++ContentTypesResponse++](response-types#contenttypesresponse)

## Get single content type

`/api/v1/content-type`

### Parameters

- `content_type` ^(required)^
- The UID of the content type

### Response type

[++ContentTypeResponse++](response-types#contenttyperesponse)
84 changes: 68 additions & 16 deletions docs/content/api-server/response-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ description = "A list of possible response types"
}
```

## ContentTypeResponse
## ContentTypesResponse

```typescript
{
Expand Down Expand Up @@ -82,21 +82,19 @@ description = "A list of possible response types"
url_prefix: string,
// additional options for any other fields
},
schema: [
{
data_type: string,
display_name: string,
field_metadata: {
_default: boolean,
version: number,
},
mandatory: boolean,
multiple: boolean,
non_localizable: boolean,
uid: string,
unique: boolean,
}[]
],
schema: {
data_type: string,
display_name: string,
field_metadata: {
_default: boolean,
version: number,
},
mandatory: boolean,
multiple: boolean,
non_localizable: boolean,
uid: string,
unique: boolean,
}[],
title: string,
uid: string,
updated_at: string, // Timestamp string
Expand All @@ -105,3 +103,57 @@ description = "A list of possible response types"
error?: string // Will be null unless there's an error
}
```

## ContentTypeResponse

```typescript
{
data: { // Will be null if there's an error
content_type: {
DEFAULT_ACL: unknown,
SYS_ACL: unknown,
_version: number,
abilities: {
create_object: boolean,
delete_all_objects: boolean,
delete_object: boolean,
get_all_objects: boolean,
get_one_object: boolean,
update_object: boolean,
},
created_at: string, // Timestamp string
description: string,
inbuilt_class: boolean,
last_activity: unknown,
maintain_revisions: boolean,
options: {
is_page: boolean,
publishable: boolean,
singleton: boolean, // Whether or not the content type supports multiple entries
sub_title: string[],
title: string,
url_pattern: string,
url_prefix: string,
// additional options for any other fields
},
schema: {
data_type: string,
display_name: string,
field_metadata: {
_default: boolean,
version: number,
},
mandatory: boolean,
multiple: boolean,
non_localizable: boolean,
uid: string,
unique: boolean,
}[],
title: string,
uid: string,
updated_at: string, // Timestamp string
},
}
error?: string // Will be null unless there's an error
}
```

0 comments on commit 445e162

Please sign in to comment.