Last active
August 29, 2015 14:19
-
-
Save cjolly/6dcc82c03199506e857b to your computer and use it in GitHub Desktop.
Example ServeManager API Client. Full docs at https://www.servemanager.com/api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# A Ruby HTTP Client Library | |
# https://github.com/jnunemaker/httparty | |
require 'httparty' | |
## | |
# "Getting Started" ServeManager API Client | |
# | |
# Our API is accessible from any language, of course. Hopefully a concrete | |
# working code example helps you get the basics built in the language of your | |
# choice. | |
# | |
# We're a ruby shop, so here's an extremely basic ruby client that demonstrates | |
# | |
# Successful authentication | |
# Successful GET request | |
# Successful POST request (required Content-Type header and JSON encoded data) | |
# | |
class ServeManager | |
include HTTParty | |
base_uri 'https://www.servemanager.com' # https *required* | |
basic_auth ENV.fetch('SM_API_KEY'), nil # Correct Auth: API key as username, empty password. | |
def self.account | |
get('/api/account') | |
end | |
def self.create_company(data) | |
params = { | |
body: data.to_json, # JSON encoded data | |
headers: {'Content-Type' => 'application/json'} # Correct Content-Type header | |
} | |
post('/api/companies', params) | |
end | |
end | |
account_response = ServeManager.account | |
puts "account GET response.code should be 200: #{account_response.code}" | |
valid_company_data = { | |
data: { | |
type: 'company', # type key is *required* on all POSTed data | |
name: 'Test Company' | |
} | |
} | |
company_post_response = ServeManager.create_company(valid_company_data) | |
puts "company POST response.code should be 201: #{company_post_response.code}" | |
## | |
## | |
## Once you have a GET and a POST working, you're off to the races. | |
## Wire up the rest of the endpoints you need access to. | |
## https://www.servemanager.com/api | |
## | |
## If you're still having trouble, some of the common pain points are | |
## demonstrated below. | |
## | |
## | |
# Example of a misconfigured client and the resulting response codes and errors | |
# Don't do these things! | |
# Incorrect Authentication Headers on any request | |
# Missing or incorrect Content-Type Headers on POST | |
# Request body MUST be JSON Encoded | |
# Missing data[type] value on POST | |
# Missing required fields on POST | |
# | |
class DontDoThis | |
include HTTParty | |
base_uri 'https://www.servemanager.com' | |
VALID_AUTH = {username: ENV['SM_API_KEY'], password: nil} # Correct way to auth | |
INVALID_AUTH = {username: 'wrong-key', password: 'dont-set-a-password'} # Incorrect way to Auth | |
VALID_CONTENT_TYPE = {'Content-Type' => 'application/json'} # Content-Type Header *required* for POST | |
INVALID_CONTENT_TYPE = {'Content-Type' => 'application/x-www-form-urlencoded'} # json type *required* for POST | |
def self.wrong_key | |
get('/api/account', {basic_auth: INVALID_AUTH}) | |
end | |
def self.company_create_wrong_content_type(data) | |
params = { | |
body: data, # Data NOT JSON encoded | |
headers: INVALID_CONTENT_TYPE, # Wrong content type | |
basic_auth: VALID_AUTH | |
} | |
post('/api/companies', params) | |
end | |
def self.company_create_without_data_type_key(data) | |
data[:data].delete(:type) # type is *required* and value *must* be singular name of resource being created(. | |
params = { | |
body: data.to_json, | |
headers: VALID_CONTENT_TYPE, | |
basic_auth: VALID_AUTH | |
} | |
post('/api/companies', params) | |
end | |
def self.company_with_validation_errors(data) | |
params = { | |
body: data.to_json, | |
headers: VALID_CONTENT_TYPE, | |
basic_auth: VALID_AUTH | |
} | |
post('/api/companies', params) | |
end | |
end | |
unauthed_req = DontDoThis.wrong_key | |
puts "unauthenticated request should be 401: #{unauthed_req.code}" | |
company_unacceptable_post_response = DontDoThis.company_create_wrong_content_type(valid_company_data) | |
puts "company POST with unacceptable Content-Type should be 406: #{company_unacceptable_post_response.code}" | |
company_create_without_data_type_key = DontDoThis.company_create_without_data_type_key(valid_company_data) | |
puts "company POST with incorrect data[type] value should be 409: #{company_create_without_data_type_key.code}" | |
invalid_company_data = { | |
data: { | |
type: 'company', # correct type | |
name: nil # Name is *required* to create a company | |
} | |
} | |
company_with_validation_errors = DontDoThis.company_with_validation_errors(invalid_company_data) | |
puts "company POST without required fields code should be 422: #{company_with_validation_errors.code}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ServeManager API Users!
Github gists don't notify us of comments, unfortunately. We don't want to miss any comments or questions so please use our contact form.
Official ServeManager API Documentation