Notes Postman

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

OAuth

On the past to have access to some services inside an app our user had to share
its password with that app.

With OAuth we share our password with a OAuth server and the server
sends a token to the service to access the customer requiared data.

Postman
Is an API client. Lets us develop, test, document and share APIs. We can send
HTTP requests to APIs using Postman.
To our request we can add parameters that can filter our searches.

In Postman we can save our request but to do it we have to add them to a


collection. Collections are used to group and share requests.

Inside collections we can create folders to better organize our requests.

When we communicate with APIs we do not usually give our usernames and
password but instead we use tokens that sometimes give us limited access to
our accounts.

Variables enable you to store and reuse values in Postman. By storing a


value as a variable, you can reference it throughout your collections, environ-
ments, requests, and test scripts. Variables help you work efficiently, collaborate
with teammates, and set up dynamic workflows.

Environments is a set of variables that can be used in our Postman re-


quests.

API Testing with Postman

We can have scripts that can test the responses to our requests.
Pre-request Scripts are Javascript rutines that are run before our request
are sent.

Using Postman from Command Line


To use Postman from the command line we can use a tool called Newman. To
install Newman we first have to install Node JS.

Once we have Newman, to run our requests we can first import the collection
that have them, then from a terminal go to where the collection was imported

1
and then run the command
newman run name of the imported collection.

Another option is from Postman generate a link for your collection and use
the run command of Postman with that link to access the collection.

To get help relating the run command you can execute the command
newman run -h

Using JSON file to pass data to Postman to use this feature we have
to set an enviroment in our request then create a JSON file with the data we
want to pass. Finally use the option to run the collection and import the JSON
file with your data.
To pass a file from a terminal we use newman run collection name -d data
source location

Lambok
Is a Java library that reduce code by offering annotations that creates that code
for us.

@ToString Generates an implementation for the toString method inherited


by all objects, consisting of printing the values of relevant fields.

@AllArgsConstructor Generates an all-args constructor. An all- args


constructor requires one argument for every field in the class.

@NoArgsConstructor Generates a no-args constructor. Will generate an


error message if such a constructor cannot be written due to the existence of
final fields.

@EqualsAndHashCode Generates implementations for the equals and


hashCode methods inherited by all objects, based on relevant fields.
@Slf4j Creates private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(LogExample.cla
@Data Generates getters for all fields, a useful toString method, and hash-
Code and equals implementations that check all non-transient fields. Will also
generate setters for all non-final fields, as well as a constructor.
What is OpenFeign?

Java Records
classes that contain data not meant to be altered and only the most funda-
mental methods such as constructors and accessors.

A record acquires these members automatically:


ˆ A private final field for each of its components

ˆ A public read accessor method for each component with the same name
and type of the component.

2
ˆ A public constructor whose signature is derived from the record compo-
nents list. The constructor initializes each private field from the corre-
sponding argument.
ˆ Implementations of the equals() and hashCode() methods, which specify
that two records are equal if they are of the same type and their corre-
sponding record components are equal.
ˆ An implementation of the toString() method that includes the string rep-
resentation of all the record’s components, with their names.

Lambok and Hibernate: How to avoid common pitfalls


We should not use Lomboks @EqualsAndHashCode because its implementa-
tion of equals returns true even when the id of our entities are both null. We
should avoid @Data, @ToString and @EqualsAndHashCode. We try to avoid
@ToString because we could have association fields that require another request
to be fetch and that can slow down our queries.
Open API
What is Open API?
Is a standard for describing APIs.

What is an Open API definition?


Is a file that describes the API which tipically has extension yaml or json and
is readable by humans and machines.

How do I create an Open API definition for my REST API?

1. Add the Open API dependency for Spring to your project.


2. Add @OpenAPIDefinition to the class with your main method.

What is Swagger? Set of tools build around OpenAPI specification. We


have the swagger editor, UI, core and more open source project. Acccelerate
the implementation of the OpenAPI specification. Generates a webpage that
has the documentation of our API with its endpoints and requirements to make
use of them. Below and example page.

3
@Operation(summary = ”controller description”) can be used to an-
notate controller method and display a message describing the method’s roll in
the documentation generated by swagger. Example below

@ApiResponses Allow us to add discriptions to the possible responses


of a controller method.To do it we can list in its parameter value different
@ApiResponse. @ApiResponse has the parameters reponseCode to spec-
ify the response code we want to describe and description to add a description
to the response code.@ApiResponse has another parameter content that allow
us to specify the type of content we were expecting or sending and also the
entity we are using.
1 @Operation ( summary = " Gets user information , stores it in the
database , and returns the data of the user saved .")
2 @ApiResponses ( value = {
3 @ApiResponse ( responseCode = "201" , description = " The
user was created " ,
4 content = { @Content ( mediaType = " application / json " ,
5 schema = @Schema ( im plementa tion = Users . class ) ) }) ,
6 @ApiResponse ( responseCode = "400" , description = " Check
the user information " ,
7 content = { @Content ( mediaType = " application /
json " ,
8 schema = @Schema ( im plement ation = Users . class ) )
} ),
9
10 })

4
What is the difference between @ApiOperation and Operation?

CRUDE HTTP Verbs


Create Post
Read Get
Update Put(Update with replace)
Delete Delete

To do partial update we use the HTTP verb Patch.


A request has header, operation, endpoint, parameters or body.

OpenAPI Specification 3.0 OAS 3.0 SwaggerHub? API design and doc-
umentation platform.
Creating API Definition with SwaggerHub
1 openapi : 3.0.0
2 info :
3 version : 2.0.0
4 title : SmartBear HR API
5 description : Employees manager
6 terms OfServi ce : https :// smartbear . com / terms - of - use
7 contact :
8 name : Jeyson
9 url : smartbear . com
10 email : keshav . v a s u d e v a n @ s m a r t b e a r . com
11 license :
12 name : SmartBear License
13 url : hrrp :// license . foo . com
14
15 servers :
16 - url : https :// dev . foo . com / v1

5
17 description : Dev Server
18
19 - url : https :// prod . foo . com / v1
20 description : Prod Server
21
22 paths :
23 / employees :
24 get :
25 description : Get information about employees from the HR
database
26 parameters :
27 - name : bodyLimit
28 in : query
29 description : The amount of employees returned
30 schema :
31 type : integer
32 minimum : 10
33 maximum : 20
34 example : 15
35
36 - name : pageLimit
37 in : query
38 description : The pages to return employees info
39 schema :
40 type : integer
41 minimum : 1
42 maximum : 5
43 example : 2
44
45 responses :
46 200:
47 description : Successful pull of employee info
48 content :
49 application / json :
50 schema :
51 type : array
52 items :
53 $ ref : ’#/ components / schemas / Employees ’
54 post :
55 description : Creates a new employee in the database
56 requestBody :
57 required : true
58 content :
59 application / json :
60 schema :
61 type : object
62 $ ref : ’#/ components / schemas / Employee ’
63
64 responses :
65 200:
66 description : Succesfully created a new employee
67
68 / employees /{ id }:
69 get :
70 description : Given an id obtain information of the employee
with that id .
71 parameters :
72 - in : path
73 name : id
74 required : true
75 description : The ID of the employee
76 schema :

6
77 type : integer
78 example : 54
79
80 responses :
81 200:
82 description : Success
83 content :
84 application / json :
85 schema :
86 type : object
87 $ ref : ’#/ components / schemas / Employee ’
88 application / xml :
89 schema :
90 type : object
91 $ ref : ’#/ components / schemas / Employee ’
92 components :
93 schemas :
94 Employees :
95 description : Array of employees information
96 type : array
97 items :
98 $ ref : ’#/ components / schemas / Employee ’
99
100
101 Employee :
102 description : Model containing employee information
103 properties :
104 id :
105 type : integer
106 example : 4
107
108 employee name :
109 type : string
110 example : John Doe
111
112 employee title :
113 type : string
114 example : Software developer

You might also like