Giter Club home page Giter Club logo

pspring-rest-client's Introduction

pspring-rest-client

This framework is member of pspring based family of frameworks. It provides a means to create rest clients with minimal code. requests library is used internally for http requests.

Annotations / Decorators that come along with this framework are listed below

  • @RestClient(url="") This decorator will add few boiler plate code that is required to interact with any rest endpoint. The methods significant are send, getUrl, addHeader.

    The send method would accept all arguments that requests.request method would. This method will throw PayloadException for all not HTTP 200 responses. The PayloadException has response and statusCode attributes which can be further used for error handling. The addHeader(name,value) method would add an header The getUrl will get the url that is configured along with @RestClient decorator. The handleError method is exists on the object will be invoked when received a non http 200 response.

  • @Mapping(method="",url="") This decorator will take care of replacing parameters in the url from arguments passed, send the request and return the response

Useful classes in this framework

RegExResponseMapper - this class can be used to tranform one dictionary to another using regex. An example is shown below. The source dictionary fields can be accessed using $ notation. In the below example message is a field inside the `response dictionary.

regexmapper = RegExResponseMapper({
    ".*Customer does not exist.*" :  {
        "statusCode" : "404",
        "code" : "APS-1001",
        "message" : "$response.message"
    },
    ".*" : {
        "statusCode" : "500",
        "code" : "APS-1002",
        "message" : "$response.message"
    }
})

@RestClient(url="https://myapi.com")
class MyRestClient():

  @Mapping(method="GET",url="/user/{firstName}")
  def getCustomer(self,firstName):
    pass

  @Mapping(method="POST",url="/user/",data=json)
  def saveCustomer(self):
    pass

  @Mapping(method="POST",url="/user/")
  def saveCustomer(self,customer):
    self.json=customer

  def handleError(self,response):
        return regexmapper.map(response)

To do:

  • To return objects based on the return type from the response received.

pspring-rest-client's People

Contributors

vasudevan-palani avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.