webflowAPI

package module
v0.0.0-...-751ec1a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2019 License: MIT Imports: 9 Imported by: 0

README

webflow

Status

Not ready for public consumption. Please see the todo section below.

Description

Go (golang) Webflow API client. It attempts retries, with an exponential backoff, when it encounters rate limiting or server errors. Thanks to pester package for the simplified retry logic.

Currently supports:

  • General GET method requests.
  • Get all collections.
  • Get collection by name.
  • Get all items in collection by collection ID.
  • Get all items in collection by collection name.

Examples

Get all items from a collection named "posts":

  import (
    "encoding/json"
    "fmt"

    "github.com/redeemed2011/webflowAPI"
  )

  type myItem struct {
    Name      string `json:"name"`
    Body      string `json:"body"`
    Something string `json:"something"`
    ID        string `json:"_id"`
  }

  func getItems() error {
    api := webflowAPI.New("my token", "my site ID")

    itemsJSON, err := api.GetAllItemsInCollectionByName("posts", 10)

    if err != nil {
      return fmt.Errorf("Error getting collection items: %+v\n", err)
    }

    items := []mockItem{}
    for _, itemJSON := range itemsJSON {
      tmpItems := &[]mockItem{}
      if err2 := json.Unmarshal(itemJSON, tmpItems); err2 != nil {
        return fmt.Errorf("API did not return the proper collection items type. Error %+v", err2)
      }

      items = append(items, *tmpItems...)
    }


    fmt.Printf("collection items: %+v\n", items)
  }

Todo

So much. :)

Features are added as needed for a personal project however please know you may open a pull request for features if you like.

The code was under great flux at the time this was published.

Items of interest:

  • Evaluate returning lists of items as raw JSON to simplify this pkg's api. At the time of this writing, getting all items in a collection requires the caller to provide a method to decode the JSON. This does not sit well with me and seems to be more complicated than necessary. If we instead return raw JSON the process is simpler and the caller can then decode the JSON however is desired.
  • Provide methods to get filtered collection items by ID or name. This may be accomplished by decoding only the id & name fields for the filter then returning the full raw JSON on match.
  • Allow custom structs for API interactions. Update: may not be worth the investment with the above changes.
  • Replace MethodGet() with something more general since the internally used HTTP pkgs support all request methods.
  • Perhaps make MethodGet() private rather than exported.
  • Implement Go Modules support for this pkg--specifically versioning of this pkg.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(token, siteID string, hc *http.Client) *apiConfig

New Create a new configuration struct for the Webflow API object.

Types

type Collection

type Collection struct {
	ID           string    `json:"_id"`
	LastUpdated  time.Time `json:"lastUpdated"`
	CreatedOn    time.Time `json:"createdOn"`
	Name         string    `json:"name"`
	Slug         string    `json:"slug"`
	SingularName string    `json:"singularName"`
}

Collection API contract for CMS collection.

type CollectionItem

type CollectionItem struct {
	Archived    bool   `json:"_archived"`
	Draft       bool   `json:"_draft"`
	Name        string `json:"name"`
	PostBody    string `json:"post-body"`
	PostSummary string `json:"post-summary"`
	Slug        string `json:"slug"`
	Author      string `json:"author"`
	Cid         string `json:"_cid"`
	ID          string `json:"_id"`
}

CollectionItem API contract for item(s) in a given collection.

type CollectionItems

type CollectionItems struct {
	// Delay parsing until we know the type.
	Items  json.RawMessage `json:"items"`
	Count  int             `json:"count"`
	Limit  int             `json:"limit"`
	Offset int             `json:"offset"`
	Total  int             `json:"total"`
}

CollectionItems API contract for retrieving collection items.

type Collections

type Collections []Collection

Collections List of Collection.

type GeneralError

type GeneralError struct {
	Msg  string `json:"msg"`
	Code int    `json:"code"`
	Name string `json:"name"`
	Path string `json:"path"`
	Err  string `json:"err"`
}

GeneralError API response struct when there is an error.

type Interface

type Interface interface {
	MethodGet(uri string, queryParams map[string]string, decodedResponse interface{}) error
	GetAllCollections() (*Collections, error)
	GetCollectionByName(name string) (*Collection, error)
	GetCollectionBySlug(slug string) (*Collection, error)
	GetAllItemsInCollectionByID(ID string, maxPages int) ([][]byte, error)
	GetAllItemsInCollectionByName(name string, maxPages int) ([][]byte, error)
	GetAllItemsInCollectionBySlug(slug string, maxPages int) ([][]byte, error)
	GetItem(cName, cSlug, cID, iName, iID string) ([]byte, error)
}

Interface Interface for this package's method. Created primarily for testing your code that depends on this package.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL