apidemic

package module
v0.0.0-...-1a70781 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2015 License: MIT Imports: 10 Imported by: 3 modules, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

apidemic Build Status

Apidemic is a service for generating fake JSON response. You first register the sample JSON response, and apidemic will serve that response with random fake data.

This is experimental, so take it with a grain of salt.

Motivation

I got bored with hardcoding the sample json api response in tests. If you know golang, you can benefit by using the library, I have included a router that you can use to run disposable servers in your tests.

Instalation

You can download the binaries for your respective operating system Download apidemic

Then put the downloaded binary somewhere in your system path.

Alternatively, if you have golang installed

go get github.com/gernest/apidemic/cmd/apidemic

Now you can start the service like this

apidemic start

This will run a service at localhost default port is 3000, you can change the port by adding a flag --port=YOUR_PORT_NUMBER

How to use

Lets say you expect a response like this

{
  "name": "anton",
  "age": 29,
  "nothing": null,
  "true": true,
  "false": false,
  "list": [
      "first",
      "second"
    ],
  "list2": [
    {
      "street": "Street 42",
      "city": "Stockholm"
      },
    {
      "street": "Street 42",
      "city": "Stockholm"
      }
    ],
  "address": {
    "street": "Street 42",
    "city": "Stockholm"
  },
  "country": {
    "name": "Sweden"
  }
}

If you have alreasy started apidemic server you can register that response by making a POST request to the /register path. Passing the json body of the form.

{
  "endpoint": "test",
  "payload": {
    "name: first_name": "anton",
    "age: digits_n,max=2": 29,
    "nothing:": null,
    "true": true,
    "false": false,
    "list:word,max=3": [
      "first",
      "second"
    ],
    "list2": [
      {
        "street:street": "Street 42",
        "city:city": "Stockholm"
      },
      {
        "street": "Street 42",
        "city": "Stockholm"
      }
    ],
    "address": {
      "street:street": "Street 42",
      "city:city": "Stockholm"
    },
    "country": {
      "name:ountry": "Sweden"
    }
  }

}

See the annotation tags on the payload. Example if I want to generate full name for field name I will just do "name:full_name".

I f your post request is submitted you are good to ask for the response with fake values. Just do a get request for the endppint you registered

So, every GET call to /api/test will return the api response with fake data.

Routes

Apidemic server has only three http routes

/

This is the home path. It only renders information about the apidemic server

/register

This is where you register endpoints. You POST the annotated sample json here. The request body should be a json object of signature.

{
	"endpoint":"my_endpoint",
	"payload": { ANNOTATED__SAMPLE_JSON_GOES_HERE },
}
/api/{REGISTERED_ENDPOINT_GOES_HERE}

Every GET request on this route will render a fake json object for the sample registered in this endpoint

Tags

Apidemic uses tags to annotate what kind of fake data to generate and also control different requrements of fake data.

You add tags to object keys. For instance if you have a json object { "user_name": "gernest"}. If you want to have fake username then you can annotate the key by addimg user_name tag like this { "user_name:user_name": "gernest"}.

So, json keys can be annotated by adding the : symbol then followed by comma separated list of tags. The firs entry after : is for the tag type, the following entries are in the form key=value which will be the extra information to fine tune your fake data. Please see the example above to see how tags are used.

Apidemic comes shiped with a large number of tags, meaning it is capable to geerate a wide range of fake information.

These are different tags that generate different fake data

Tag Details( data generated)
brand brand
character character
characters characters
characters_n characters of maximum length n
city city
color color
company company
continent continent
country country
credit_card_num credit card number
currency currency
currency_code currency code
day day
digits digits
digits_n digits of maximum number n
domain_name domain name
domain_zone domain zone
email_address email address
email_body email body
female_first_name female first name
female_full_name female full name
female_full_name_with_prefix female full name with prefix
female_full_name_with_suffix female full name with suffix
female_last_name female last name
female_last_name_pratronymic female last name pratronymic
first_name first name
full_name full name
full_name_with_prefix full name with prefix
full_name_with_suffix full name with suffix
gender gender
gender_abrev gender abrev
hex_color hex color
hex_color_short hex color short
i_pv_4 i pv 4
industry industry
job_title job title
language language
last_name last name
latitude_degrees latitude degrees
latitude_direction latitude direction
latitude_minutes latitude minutes
latitude_seconds latitude seconds
latitude latitude
longitude longitude
longitude_degrees longitude degrees
longitude_direction longitude direction
longitude_minutes longitude minutes
longitude_seconds longitude seconds
male_first_name male first name
male_full_name_with_prefix male full name with prefix
male_full_name_with_suffix male full name with suffix
male_last_name male last name
male_pratronymic male pratronymic
model model
month month
month_num month num
month_short month short
paragraph paragraph
patagraphs patagraphs
patagraphs_n patagraphs of maximum n
password password
patronymic patronymic
phone phone
product product
product_name product name
sentence sentence
sentences sentences
sentences_n sentences of maximum n
simple_pass_word simple pass word
state state
state_abbrev state abbrev
street street
street_address street address
title title
top_level_domain top level domain
user_name user name
week_day week day
week_day_short week day short
week_day_num week day num
word word
words words
words_n words of maximum n
year year
zip zip

Contributing

Start with clicking the star button to make the author and his neighbors happy. Then fork the repository and submit a pull request for whatever change you want to be added to this project.

If you have any questions, just open an issue.

Author

Geofrey Ernest

Twitter : @gernesti

Licence

This project is released under the MIT licence. See LICENCE for more details.

Documentation

Index

Constants

View Source
const Version = "0.2"

Version is the version of apidemic. Apidemic uses semver.

Variables

View Source
var ErrTagNotFound = errors.New("apidemic: Tag not found")

Functions

func GetEndpoint

func GetEndpoint(w http.ResponseWriter, r *http.Request)

GetEndpoint renders registered endpoints.

func Home

func Home(w http.ResponseWriter, r *http.Request)

Home renders hopme page. It renders a json response with information about the service.

func NewResponse

func NewResponse(message string) interface{}

NewResponse helper for response JSON message

func NewServer

func NewServer() *mux.Router

NewServer returns a new apidemic server

func RegisterEndpoint

func RegisterEndpoint(w http.ResponseWriter, r *http.Request)

RegisterEndpoint receives API objects and registers them. The payload from the request is transformed into a self aware Value that is capable of faking its own attribute.

func RenderJSON

func RenderJSON(w http.ResponseWriter, code int, value interface{})

RenderJSON helder for rndering JSON response, it marshals value into json and writes it into w.

Types

type API

type API struct {
	Endpoint string                 `json:"endpoint"`
	Payload  map[string]interface{} `json:"payload"`
}

API is the struct for the json object that is passed to apidemic for registration.

type Object

type Object struct {
	Data map[string]Value
}

func NewObject

func NewObject() *Object

func (*Object) Load

func (o *Object) Load(src map[string]interface{}) error

func (*Object) MarshalJSON

func (v *Object) MarshalJSON() ([]byte, error)

func (*Object) Set

func (o *Object) Set(key string, val Value)

type Tags

type Tags map[string]string

Tags stores metadata about values

func (Tags) Bool

func (t Tags) Bool(key string) (bool, error)

Bool returns a boolean value for tag key

func (Tags) Get

func (t Tags) Get(key string) (string, bool)

Get returns the value for tag key.

func (Tags) Int

func (t Tags) Int(key string) (int, error)

Int returns an in value for tag key.

func (Tags) Load

func (t Tags) Load(src string)

Load parses src and extacts tags from it. The src is a string with comma separated content.

Example "character_n,max=30"

The first tag, is the value type information, the rest is extra information to fine tune the generated fake value.

For instance in the example above, the value is characters, where max=30 limits the number of characters to the maximum size of 30.

type Value

type Value struct {
	Tags Tags
	Data interface{}
}

func NewValue

func NewValue(val interface{}) Value

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

func (Value) Update

func (v Value) Update() Value

Directories

Path Synopsis
cmd
apidemic command

Jump to

Keyboard shortcuts

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